2014-11-24 85 views
11

美好的一天,我正在使用Spring 4.1.1.RELEASE开发一个Web应用程序。所有Spring的配置与注解来实现,它工作得很好,除了一两件事:@Value - >无法将类型'java.lang.String'的值转换为所需类型'java.lang.Integer'

  • 我与这些行项目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; 
    
        ... 
    } 
    

属性“过滤器”的值是从属性资源中成功注入的。但我的财产“计数”得到一个例外:

 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] 
    ... 

当我改变属性字符串“计数”的类型就开始工作:

 @Value("${cases.caseList.numberOfCasesPerPage}") 
     private String count; 

我相信春天是能够转换成字符串当使用@Value将属性资源的值注入到Integer属性中时为Integer。我发现了一些人们没有抱怨的例子。你有什么想法,为什么它不适合我?

非常感谢。

+2

它工作还是刚启动没有错误。在这种情况下,我希望这个值字面上是'$ {cases.caseList.numberOfCasesPerPage}'。确保你已经将'PropertySourcesPlaceHolderConfigurer'注册为'public static' bean。 – 2014-11-24 13:44:59

回答

19

如果您尝试使用@Value("")批注访问属性值,则应声明PropertySourcesPlaceholderConfigurer Bean。

尝试在您的配置类中添加以下代码片段。

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

如果你不想把它声明,尝试用org.springframework.core.env.Environment类在你的类自动装配它,得到的属性值。

@Autowired 
private Environment environment; 

public void readValues() { 
    System.out.println("Some Message:" 
      + environment.getProperty("<Property Name>")); 

} 
+0

尝试运行集成测试时遇到完全相同的问题。我已经按照你对我的'ApplicationTest'类所说的那样添加了bean,它被标记为'@ Configuration',并且通过我的单元测试使用'@SpringApplicationConfiguration'(classes = ApplicationTest.class)''作为配置类。我在该方法上有断点,并验证配置器是否已创建。 没有任何区别。我仍然得到由'NumberFormatException'引起的'TypeMismatchException',就像Vojtech – kumetix 2016-01-11 09:22:45

+0

我的不好,我用%而不是$。抱歉 – kumetix 2016-01-11 11:29:27

相关问题