-->

@value注释类型浇铸到整型字符串从(@Value annotation type casting

2019-08-17 14:16发布

我想要一个值的输出转换为一个整数:

@Value("${api.orders.pingFrequency}")
private Integer pingFrequency;

上述引发错误

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}"

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

谷歌似乎并没有说太多关于这个问题的。 我想永远与整数处理,而不必解析这个值无处不在它的使用。

解决方法

我认识一个解决办法可能是使用setter方法来运行转换为我,但如果春天能做到这一点,我宁愿学到一些关于Spring。

Answer 1:

假设你有一个属性上classpath中包含文件

api.orders.pingFrequency=4

我尝试了内部@Controller

@Controller
public class MyController {     
    @Value("${api.orders.pingFrequency}")
    private Integer pingFrequency;
    ...
}

随着包含我的servlet上下文:

<context:property-placeholder location="classpath:myprops.properties" />

它完美地工作。

因此,无论你的财产是不是整数类型,你不必属性占位符配置正确,或者你使用了错误的属性键。

我试图用一个无效的属性值,运行4123; 。 我得到的例外是

java.lang.NumberFormatException: For input string: "4123;"

这使我想起你的财产值

api.orders.pingFrequency=(java.lang.Integer)${api.orders.pingFrequency}


Answer 2:

我一直在寻找互联网上的答案,我发现了以下

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

所以你的情况,你可以用这个尝试

@Value("#{new Integer('${api.orders.pingFrequency}')}")
private Integer pingFrequency;


Answer 3:

如果您想将财产转换为从属性文件中的整数2个解决方案,我发现:

鉴于情况:customer.properties包含customer.id = 100作为一个字段,要访问它在spring配置文件作为整数 .The财产客户ID被声明为在Bean 客户类型为int

解决方案1:

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

另外,在上述线,从属性文件中的字符串值转换为int类型。

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

<property name="customerId"  	value="${customer.id}" />



Answer 4:

我有相同的情况。 它是由不具有引起PropertySourcesPlaceholderConfigurer在Spring上下文,它解决靠在值@Value的类中注释。

包括财产占位符来解决这个问题,没有必要使用Spring表达式整数(属性文件没有存在,如果您使用ignore-resource-not-found="true" ):

<context:property-placeholder location="/path/to/my/app.properties"
    ignore-resource-not-found="true" />


Answer 5:

如果你正在使用@Configuation然后实例如下静态豆。 如果不是静态@Configutation是非常早期的实例化,并与负责解决像@Value,@Autowired等注释的BeanPostProcessor的,不能采取行动。 请参考这里

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
   return new PropertySourcesPlaceholderConfigurer();
}


Answer 6:

我有我使用这个解决同样的问题。 请参阅此Spring MVC的:@Value注释中*属性文件得到定义为int值

@Value(#{propertyfileId.propertyName})

作品



Answer 7:

当你有2个资源使用相同的文件名也会发生此问题; 说内的类路径内配置2个不同的罐或目录路径“configurations.properties”。 例如:

你有你的“configurations.properties”在过程中或Web应用程序(罐子,战争或耳朵)。 但另一种依赖(JAR)在同一路径相同的文件“configurations.properties”。 然后我想,春天不知道(@ _ @?)哪里获得的财产,仅仅发送@Value注释中声明的属性名称。



Answer 8:

在我的情况下,问题是,我POST请求(使用“?.. = ..”使用get参数)和发送到相同的URL作为获取参数有相同的名称作为表单参数。 也许春天将它们合并到一个数组和解析被扔的错误。



Answer 9:

当使用@Value,你应该添加类@PropertySource注释,或在春天的XML文件中指定的属性持有人。 例如。

@Component
@PropertySource("classpath:config.properties")
public class BusinessClass{
   @Value("${user.name}")
   private String name;
   @Value("${user.age}")
   private int age;
   @Value("${user.registed:false}")
   private boolean registed;
}

config.properties

user.name=test
user.age=20
user.registed=true

这个作品!

当然,你可以使用占位符的XML配置,而不是注解。 spring.xml

<context:property-placeholder location="classpath:config.properties"/>


文章来源: @Value annotation type casting to Integer from String