@Value -> 无法将“java.lang.String”类型的值转换为所需类型“java.lang.Integer”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27106055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@Value -> Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'
提问by Vojtech
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:
美好的一天,我正在使用 Spring 4.1.1.RELEASE 开发 Web 应用程序。所有 Spring 配置都是通过注释完成的,它工作正常,除了一件事:
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; ... }
我在项目中有一个 config.properties 文件,其中包含这些行
cases.caseList.filter=test cases.caseList.numberOfCasesPerPage=50
我有一个配置类
@Configuration @ComponentScan(basePackageClasses={CaseConfig.class}) @PropertySource(value = "classpath:config.properties") public class CasesModuleTestContextConfig { ... }
还有另一堂课
@Component public class HttpRequestParamsToPaginationParams extends AbstractConverter<Map<String, String>, PaginationParams> { @Value("${cases.caseList.filter}") private String filter; @Value("${cases.caseList.numberOfCasesPerPage}") private Integer count; ... }
Value of property 'filter' is successfuly injected from the property resource. But I'm getting an exception on property 'count':
属性“过滤器”的值已成功从属性资源注入。但是我在属性“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]
...
When I change type of property 'count' to String it start working:
当我将属性 'count' 的类型更改为 String 时,它开始工作:
@Value("${cases.caseList.numberOfCasesPerPage}")
private String count;
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 将属性资源中的值注入到 Integer 属性中时,Spring 能够将 String 转换为 Integer。我找到了人们使用而不抱怨的例子。你有什么想法为什么它对我不起作用?
Thanks a lot in advance.
非常感谢。
采纳答案by Lovababu
If you are trying to access the property values using @Value("")
annotation, you should declare PropertySourcesPlaceholderConfigurer
Bean.
如果您尝试使用@Value("")
注释访问属性值,则应声明PropertySourcesPlaceholderConfigurer
Bean。
Try to add below snippet of code in your configuration class.
尝试在您的配置类中添加以下代码片段。
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
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.
如果您不想声明它,请尝试org.springframework.core.env.Environment
通过在您的类中自动装配类来获取属性值。
@Autowired
private Environment environment;
public void readValues() {
System.out.println("Some Message:"
+ environment.getProperty("<Property Name>"));
}