@Value注释类型从String转换为Integer

Web*_*net 54 java spring annotations casting property-placeholder

我正在尝试将值的输出转换为整数:

@Value("${api.orders.pingFrequency}")
private Integer pingFrequency;
Run Code Online (Sandbox Code Playgroud)

以上抛出错误

org.springframework.beans.TypeMismatchException: 
    Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; 
nested exception is java.lang.NumberFormatException: 
    For input string: "(java.lang.Integer)${api.orders.pingFrequency}"
Run Code Online (Sandbox Code Playgroud)

我也试过了 @Value("(java.lang.Integer)${api.orders.pingFrequency}")

谷歌似乎没有多说这个话题.我想总是处理一个整数,而不是必须在它使用的任何地方解析这个值.

解决方法

我意识到一个解决方法可能是使用setter方法为我运行转换,但如果Spring可以做到这一点,我宁愿学习一些关于Spring的东西.

Sot*_*lis 46

假设您的类路径中包含一个属性文件

api.orders.pingFrequency=4
Run Code Online (Sandbox Code Playgroud)

我在里面试了一下 @Controller

@Controller
public class MyController {     
    @Value("${api.orders.pingFrequency}")
    private Integer pingFrequency;
    ...
}
Run Code Online (Sandbox Code Playgroud)

我的servlet上下文包含:

<context:property-placeholder location="classpath:myprops.properties" />
Run Code Online (Sandbox Code Playgroud)

它工作得很好.

因此,您的属性不是整数类型,您没有正确配置属性占位符,或者您使用了错误的属性键.

我尝试使用无效的属性值运行4123;.我得到的例外是

java.lang.NumberFormatException: For input string: "4123;"
Run Code Online (Sandbox Code Playgroud)

这让我觉得你的财产的价值是

api.orders.pingFrequency=(java.lang.Integer)${api.orders.pingFrequency}
Run Code Online (Sandbox Code Playgroud)


小智 23

我在互联网上寻找答案,我发现了以下内容

@Value("#{new java.text.SimpleDateFormat('${aDateFormat}').parse('${aDateStr}')}")
Date myDate;
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下你可以试试这个

@Value("#{new Integer('${api.orders.pingFrequency}')}")
private Integer pingFrequency;
Run Code Online (Sandbox Code Playgroud)

  • 这给了我SpelEvaluationException.你确定它有效吗? (2认同)
  • 代替“ new Integer.parseInt”,使用“ new Integer”为我工作。 (2认同)

aja*_*jay 7

如果您想将转换属性到一个整数从属性文件中有2级的解决方案,我发现:

给定方案:customer.properties包含customer.id = 100作为字段,并且您希望在spring配置文件中将其作为整数访问.属性customerId在Bean Customer中声明为int类型

解决方案1:

<property name="customerId" value="#{T(java.lang.Integer).parseInt('${customer.id}')}" />
Run Code Online (Sandbox Code Playgroud)

在上面的行中,属性文件中的字符串值将转换为int类型.

解决方案2:使用一些其他扩展就地化子性质.对于Ex.-如果你的属性文件名是 customer.properties然后使它customer.details和配置文件中使用下面的代码

<property name="customerId"  	value="${customer.id}" />
Run Code Online (Sandbox Code Playgroud)


thi*_*his 7

我有完全相同的情况.这是因为Spring上下文中没有PropertySourcesPlaceholderConfigurer,它@Value根据类内部的注释解析值.

包含一个属性占位符来解决问题,不需要对整数使用Spring表达式(如果使用,属性文件不必存在ignore-resource-not-found="true"):

<context:property-placeholder location="/path/to/my/app.properties"
    ignore-resource-not-found="true" />
Run Code Online (Sandbox Code Playgroud)


小智 6

如果您正在使用 @Configuation 然后在静态 bean 下实例化。如果不是静态的 @Configutation 很早就被实例化,并且负责解析 @Value、@Autowired 等注释的 BeanPostProcessors 无法对其进行操作。参考这里

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
   return new PropertySourcesPlaceholderConfigurer();
}
Run Code Online (Sandbox Code Playgroud)