jeff porter jeff porter - 13 days ago 6
Java Question

Spring propertyConfigurer not working

I've got the following problem that it seems that the injection properties from the propertyConfigurer is not working.

The error I get is...

Caused by: java.lang.NumberFormatException: For input string: "${db.maxactive}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.valueOf(Integer.java:554)
at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:155)
at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:115)
at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:434)
at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:406)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:163)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470)


From the stacktrace it is trying to inject the value "${db.maxactive}" into the dbcp driver.
If I turn up the logging I can see the following (this is the stacktrace when I don't inject that property)...

[INFO] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19bd03e: startup date [Thu Jun 27 08:58:08 BST 2013]; root of context hierarchy
[INFO] Loading XML bean definitions from class path resource [conf/spring/applicationContext-StandAlone.xml]
[INFO] Loading XML bean definitions from class path resource [conf/spring/applicationContext-monitoring.xml]
[INFO] Loading XML bean definitions from class path resource [conf/spring/dataAccessContext-local.xml]
[INFO] Loading XML bean definitions from class path resource [conf/spring/applicationContext-jdbc.xml]
[INFO] Loading XML bean definitions from class path resource [conf/spring/applicationContext-beans.xml]
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/porterj/.m2/repository/org/slf4j/slf4j-jdk14/1.6.1/slf4j-jdk14-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/porterj/.m2/repository/org/slf4j/slf4j-nop/1.6.1/slf4j-nop-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.


**** The exception is thrown HERE, before the jdbc.properties is loaded.
[INFO] Loading properties file from class path resource [jdbc.properties]

This make me think the propertyConfigurer is loaded after it is trying to inject the value, hence its injecting the parameter name, NOT the parameter value.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
</list>
</property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="maxActive" value="${db.maxactive}"/>
</bean>


Then my jdbc.properties file...

db.driver=com.ibm.db2.jcc.DB2Driver
db.url=jdbc:db2://dbserver:51000/db
db.username=dm
db.password=pass
db.maxactive=20


The java class...

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext-StandAlone.xml");


Can someone give me some advice, is this because I'm doing this from a standalone app? I've done this many times in a web app and the propertyConfigurer works fine.

Note: Spring 3.1.2.RELEASE

<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>

Answer

I've seen this once before in an application that i was developing; I found the problem was something to do with mybatis and there was an issue with the SqlSessionFactoryBean. I didn't dig too deep but if you want to research a bit more start with this link - might be something similar even if you're not using mybatis.

I could see that my beans were getting instantiated and the properties on them set before the Properties had been loaded from the file.

Instead of using a Spring PropertyPlaceholderConfigurer I ended up having to use the spring "util:properties" mechanism to load the properties - once I moved over to this it all started working. Give something like the code below a try - might work for you...

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

  <util:properties id="dataSourceProps" location="classpath:jdbc.properties"/>

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="#{dataSourceProps['db.driver']}" />
    <property name="url" value="#{dataSourceProps['db.url']}" />
    <property name="username" value="#{dataSourceProps['db.username']}" />
    <property name="password" value="#{dataSourceProps['db.password']}" />
    <property name="maxActive" value="#{dataSourceProps['db.maxactive']}" />
  </bean>

  ....

</beans>