23

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:

  • 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;
    
        ...
    }
    

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

When I change type of property 'count' to String it start working:

        @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?

Thanks a lot in advance.

Vojtech
  • 2,533
  • 9
  • 34
  • 65
  • 2
    Is it working or is it just starting up without errors. I expect the value to be literally `${cases.caseList.numberOfCasesPerPage}` in that case. Make sure you have registered a `PropertySourcesPlaceHolderConfigurer` as a `public static` bean. – M. Deinum Nov 24 '14 at 13:44
  • [Spring @Value TypeMismatchException:Failed to convert value of type 'java.lang.String' to required type 'java.lang.Double'](https://stackoverflow.com/q/42733067/86967) – Brent Bradburn Mar 19 '18 at 17:17

2 Answers2

24

If you are trying to access the property values using @Value("") annotation, you should declare PropertySourcesPlaceholderConfigurerBean.

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.

@Autowired
private Environment environment;

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

}
Lovababu Padala
  • 2,415
  • 2
  • 20
  • 28
  • I encounter the exact same problem when trying to run an integration test. I've added the bean exactly as you said to my `ApplicationTest` class which is marked as `@Configuration` and used as configuration class by my unit test using `@SpringApplicationConfiguration(classes = ApplicationTest.class)`. I had breakpoint on that method and verified the configurer was created. Didn't make any difference. I'm still getting `TypeMismatchException` caused by `NumberFormatException`, exactly like Vojtech – kumetix Jan 11 '16 at 09:22
  • My bad, I used % instead of $. sorry – kumetix Jan 11 '16 at 11:29
18

In my case I foregot the $ sign into the annotation

@Value("{cases.caseList.filter}")

instead of

@Value("${cases.caseList.filter}")
Roberto Petrilli
  • 711
  • 1
  • 8
  • 22