我有以下Bean聲明:春天PropertyPlaceholderConfigurer和內容:財產佔位
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/classes/config/properties/database.properties</value>
<value>classpath:config/properties/database.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<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>
現在我想上面PropertyPlaceholderConfigurer更改爲以下格式:
<context:component-scan base-package="org.example.config"/>
<util:properties id="jdbcProperties"
location="classpath:config/properties/database.properties"/>
- ignoreResourceNotFound會忽略的財產,而運行。例如: 當測試應用程序WEB-INF/..路徑將忽略(因爲maven 項目和屬性文件在src/main/resources/..下),而 啓動web應用程序,其他屬性將忽略路徑,我需要 以上述格式實現。
- 應該可以添加多個屬性文件一樣 database.properties,test.properties等
- 在春季3,我可以使用註釋,而不是爲DB 加載這些XML文件,我該怎麼辦呢?因爲我只使用一個xml文件(上面給出的 )加載數據庫的東西。
我正在使用Spring 3框架。
但同樣你需要 @Bean 公共靜態PropertySourcePlaceholderConfigurer PSPC()在Spring 3.1例如 –
這是最好的答案。一個簡單的說明:如果您使用Spring> = 3.1,請使用PropertySourcesPlaceholderConfigurer而不是舊的PropertyPlaceholderConfigurer。它支持Environment和Spring 3.1中引入的所有新東西。 –