Scim
Scim

Reputation: 5

Why spring <util:properties> does not working

I'm new in Spring and now trying to make a connection to the database. I configured jdbc-config.xml:

<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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<tx:annotation-driven transaction-manager="txManager" />

<bean id="simpleJdbcTemplate"
    class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref="dataSource" />
</bean>

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- the DataSource (parameterized for configuration via a PropertyPlaceHolderConfigurer) -->
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" /> 

    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

</beans>

and imported properties in root-config.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.0.xsd">


  <util:properties id="appProperties" location="/WEB-INF/jdbc.properties" local-override="true"/>


<import resource="jdbc-config.xml" />

</beans>

and while application deploys on server i have the following error:

org.springframework.beans.MethodInvocationException: 
Property 'driverClassName' threw exception; nested exception is
 java.lang.IllegalStateException: Could not load JDBC driver class [${jdbc.driverClassName}]

but all is fine, if i use

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

instead of

<util:properties ...

Can someone explain me - why?

Upvotes: 0

Views: 4249

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280181

util:properties doesn't do property resolution, it just creates a Properties bean. See the documentation.

A PropertyPlaceholderConfigurer bean however does do property resolution. It's a BeanFactoryPostProcessor that manipulates bean definitions to resolve any property placeholders.

Upvotes: 3

Related Questions