Spring – Problem with Spring @Configuration class

configurationspring

i use class with @Configuration annotation to configure my spring application:


@Configuration
public class SpringConfiguration {

 @Value("${driver}")
 String driver;

 @Value("${url}")
 String url;

 @Value("${minIdle}")
 private int minIdle;
       // snipp ..  

 @Bean(destroyMethod = "close")
 public DataSource dataSource() {
  DataSource dataSource = new DataSource();
  dataSource.setDriverClassName(driver);
  dataSource.setUrl(url);
  dataSource.setUsername(user);
  dataSource.setPassword(password);
  dataSource.setMinIdle(minIdle);

  return dataSource;
 }



and properties file in CLASSPATH

driver=org.postgresql.Driver
url=jdbc:postgresql:servicerepodb
minIdle=1

I would like to get my DataSource configured object in my DAO class:

ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfiguration.class);   
DataSource dataSource = ctx.getBean(DataSource.class);

But i get the error:


org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'springConfiguration': Injection of autowired dependencies 
failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private int de.hska.repo.configuration.SpringConfiguration.minIdle; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is **java.lang.NumberFormatException: For input string: "${minIdle}"**

Caused by: java.lang.NumberFormatException: For input string: **"${minIdle}"**
 at java.lang.NumberFormatException.forInputString(**Unknown Source**)
 at java.lang.Integer.parseInt(Unknown Source)
 at java.lang.Integer.valueOf(Unknown Source)

It worked with String properties (driver, url), but ${minIdle} (of type int) can't be resolved!
Please help. Thanx in advance!

Best Answer

Updated: With the updated information and your own answer, I removed all the datasource stuff, since I'm quite convinced your problem is related to the the <context:property-placeholder /> element.

Your error message from your answer that I believe is the root of the problem:

Caused by: java.lang.NumberFormatException: For input string: "${minIdle}"

I believe the problem is that PropertyPlaceholderConfigurer isn't initialized. If you configure this with the <context:property-placeholder /> element, all placeholders will be substitued with corresponding values from a properties file in a post process operation at the end of the initialization process and before the Spring container is in the operational phase.

If it was a parse error, and the properties file contained a line like this:

minIdle=demo string

Then, the error message would have used "demo string" instead of "${minIdle}"

When it comes to parsing, Spring will handle the parsing for you with the @Value annotation and your minIdle field works fine on my computer:

 @Value("${minIdle}")
 private int minIdle;

The only way I managed to get your exception message is if I forgot to add the <context:property-placeholder /> element. Then I got the same exception message with the last property accessed in the error message.

Demo app

A little experiment that simulates your problem:

@Value("${a}")
private String a;

@Value("${b}")
private Integer b;

@Bean
public String demo() {
    System.out.println("a: " + a);
    System.out.println("b: " + b);

    return a + ", " + b;
}

Without the property-placeholder element:

Caused by: java.lang.NumberFormatException: For input string: "${b}"

With the property-placeholder it works like expected.