@Value - >无法将类型'java.lang.String'的值转换为必需类型'java.lang.Integer'
美好的一天,我正在使用Spring 4.1.1.RELEASE开发Web应用程序。所有Spring配置都是用注释完成的,除了一件事情它还可以正常工作:
Good day, I'm working on a web application using Spring 4.1.1.RELEASE. All Spring configuration is done with annotations and it works fine except one thing:
-
我在项目中有一个config.properties文件使用这些行
I have a config.properties file in the project with these lines
cases.caseList.filter=test
cases.caseList.numberOfCasesPerPage=50
我有一个配置类
I have a config class
@Configuration
@ComponentScan(basePackageClasses={CaseConfig.class})
@PropertySource(value = "classpath:config.properties")
public class CasesModuleTestContextConfig { ... }
另一个班级
And another class
@Component
public class HttpRequestParamsToPaginationParams extends AbstractConverter<Map<String, String>, PaginationParams> {
@Value("${cases.caseList.filter}")
private String filter;
@Value("${cases.caseList.numberOfCasesPerPage}")
private Integer count;
...
}
属性过滤器的值是从属性资源中成功注入的。但我在属性'count'上遇到例外:
Value of property 'filter' is successfuly injected from the property resource. But I'm getting an exception on property 'count':
13:58:45.274 [main] WARN o.s.c.s.GenericApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cz.pokus.core.test.config.ConversionServiceTestConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List cz.pokus.core.test.config.ConversionServiceTestConfig.converterList; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'httpRequestParamsToPaginationParams': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.Integer cz.pokus.core.cases.converter.HttpRequestParamsToPaginationParams.count; nested exception is 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: "${cases.caseList.numberOfCasesPerPage}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
...
Caused by: 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: "${cases.caseList.numberOfCasesPerPage}"
...
Caused by: java.lang.NumberFormatException: For input string: "${cases.caseList.numberOfCasesPerPage}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_20]
at java.lang.Integer.parseInt(Integer.java:569) ~[na:1.8.0_20]
at java.lang.Integer.valueOf(Integer.java:766) ~[na:1.8.0_20]
...
当我将属性'count'的类型更改为String时,它开始工作:
When I change type of property 'count' to String it start working:
@Value("${cases.caseList.numberOfCasesPerPage}")
private String count;
我相信Spring能够在使用属性资源将值注入Integer属性时将String转换为Integer @值。我找到了人们在没有抱怨的情况下使用的例子。你有任何想法,为什么它对我不起作用?
I believe Spring is able to convert String to Integer when injecting value from property resource into a Integer property using @Value. I'v found examples where people use without complaining. Do you please have any ideas why it doesn't work for me?
提前多多谢谢。
如果您正在尝试要使用 @Value()
注释访问属性值,您应该声明 PropertySourcesPlaceholderConfigurer
Bean。
If you are trying to access the property values using @Value("")
annotation, you should declare PropertySourcesPlaceholderConfigurer
Bean.
尝试在配置类中添加以下代码段。
Try to add below snippet of code in your configuration class.
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
如果您不想申报,请尝试 org.springframework.core.env.Environment
通过在类中自动装配它来获取属性值。
If you don't want to declare it, Try with org.springframework.core.env.Environment
class by autowiring it in your class, to get the property values.
@Autowired
private Environment environment;
public void readValues() {
System.out.println("Some Message:"
+ environment.getProperty("<Property Name>"));
}