Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring@Value无法识别整数属性值_Java_Spring - Fatal编程技术网

Java Spring@Value无法识别整数属性值

Java Spring@Value无法识别整数属性值,java,spring,Java,Spring,我正在为邮件服务配置创建一个组件>> @Component @PropertySource("classpath:mail.properties") public class Mail { @Value("${email.config.host}") private String host; @Value("${email.config.port}") private Integer port; @Value("${email.config.username}")

我正在为邮件服务配置创建一个组件>>

@Component
@PropertySource("classpath:mail.properties")
public class Mail {

  @Value("${email.config.host}")
  private String host;

  @Value("${email.config.port}")
  private Integer port;

  @Value("${email.config.username}")
  private String username;
}
我的mail.properties文件看起来像>>

email.config.host=smtp.gmail.com
email.config.port=587
email.config.username=idontknowmyname@gmail.com
email.config.password=password
我试图获取端口值,但遇到问题>

java.lang.NumberFormatException: For input string: "${email.config.port}"
是的,我知道这应该是一个整数值,但我的@value注释已将**转换为字符串。所以我试了一下:

@Value("#{ T(java.lang.Integer).parseInt('${email.config.port}')}")
。。。也一样

主机、用户名、密码等加载良好! 如何获取端口值?
为什么PropertySource没有自动转换此参数?

添加
@Configuration
注释以处理外部化值。

如果字符串表示整数,Spring可以将这些值从字符串转换为整数

但是如果spring无法在属性中找到属性,那么它会将其值设置为其(缺少)键。这将导致一个字符串,它不是可解析的数字


因此,Spring可能无法找到您的
email.config.port
属性您可以证明这一点,购买带有
@Value(“${email.config.port}”)

注释的字符串字段时,您为什么认为
@PropertySource(“classpath:mail.properties”)
属于那里?所以您尝试了
parseInt(“${email.port}”)
?为什么?我的意思是,属性名为
email.config.port
,而不是
email.port
,对吗?是的!设置${email.config.port}时出错。很抱歉如我第一次回答中所述。我发布了错误的@Value参数。我现在就把这东西换掉。事实是,正确的参数(键)不能使这项测试起作用。@davidwillianx:您尝试过建议的基于字符串的测试吗?您的意思是将端口变量设置为字符串?如何将此值设置为我的框架配置对象,因为它是allow int only。@davidwillianx:不,我的意思是try just:
@value(${email.config.port})String portAsStringTest
@PostConstruct post(){System.out.println(“portAsStringTest:+portAsStringTest)}
字符串类型非常适合!您是否试图指示我将此字符串值转换为@postcontuct方法的Int/Integer值?获取了相同的错误>>java.lang.NumberFormatException:对于输入字符串:“${email.config.port}”