2014-04-03 30 views
0

我是Spring中的新成員,現在嘗試連接到數據庫。 我配置的JDBC-config.xml中:爲什麼彈簧<util:properties>不起作用

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

和進口性質根-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> 

和同時應用部署在服務器我有以下錯誤:

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

但是一切都很好,如果我用

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

代替

<util:properties ... 

有人能解釋我 - 爲什麼?

回答

2

util:properties不做屬性解析,它只是創建一個Properties bean。請參閱documentation

A PropertyPlaceholderConfigurer bean然而確實做屬性分辨率。這是一個BeanFactoryPostProcessor,它操縱bean定義來解析任何屬性佔位符。

+0

另一個問題:如果我試圖使用它也不起作用。 – Scim

+0

哦,我解決了這個問題。一切正常。關鍵是,我試圖在上下文加載所有屬性之前獲取屬性值。剛把屬性佔位符從servlet-context移到了root-config.xml中。 – Scim