2014-04-06 54 views
0

我試圖創建一個使用Spring訪問postgres數據庫的應用程序。當我運行應用程序發生錯誤時。無法解析佔位符'database.dags.driverClassName'

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dagsDataSource' defined in file [F:\Spring_Batch_Project\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SpringBatchAdmin\WEB-INF\classes\META-INF\spring\batch\override\dataSource-context.xml]: Could not resolve placeholder 'database.dags.driverClassName' 

這裏是我的datasource.context.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 

<beans> 

    <bean 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location"> 
      <value>com/dirbi/oracle/resources/dbconfig/database.properties</value> 
     </property> 

    </bean> 



    <!-- DAGs DataSource configuration --> 
    <bean id="dagsDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driver" value="${database.dags.driverClassName}" /> 
     <property name="url" value="${database.dags.url}" /> 
     <property name="username" value="${database.dags.username}" /> 
     <property name="password" value="${database.dags.password}" /> 
    </bean> 

    <!-- Spring Batch DataSource configuration --> 
    <bean id="springBatchDataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driver" value="${database.springbatch.driverClassName}" /> 
     <property name="url" value="${database.springbatch.url}" /> 
     <property name="username" value="${database.springbatch.username}" /> 
     <property name="password" value="${database.springbatch.password}" /> 
    </bean> 

</beans> 

這裏是database.properties

database.dags.driverClassName=org.postgresql.Driver 
database.dags.url=jdbc:postgresql://localhost:5433/testing 
database.dags.username=postgres 
database.dags.password=admin 

database.springbatch.driverClassName=org.postgresql.Driver 
database.springbatch.url=jdbc:postgresql://localhost:5433/springbatchadmindb 
database.springbatch.username=postgres 
database.springbatch.password=admin 

非常感謝你。

回答

1

首先,因爲不推薦使用Spring 3.1 PropertyPlaceholderConfigurer - 請使用PropertySourcesPlaceholderConfigurer代替,或者僅使用<context:property-placeholder/>

第二:com/dirbi/oracle/resources/dbconfig/database.properties是不夠的。 你必須指定resource type prefix。通常爲classpath:,但它取決於您的文件所在的位置。如果它只是在一些目錄中,你應該使用file://。 但它看起來像你的database.properties是在一些包中,所以使用classpath:

順便說一句,爲什麼/oracle/,如果使用PostgreSQL

+0

比蘭茲:首先非常感謝你的回答。我在哪裏寫?爲什麼/ oracle /,如果你使用PostgreSQL?這個軟件包的名字是由我的朋友給出的,我不確切知道哪個數據庫使用。 – T8Z

+0

''而不是'PropertyPlaceholderConfigurer'的bean定義。你的主要問題是'classpath:''location'值的前綴 –

相關問題