我對PropertyPlaceholderConfigurer(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer)和我的pom.xml中定義的普通篩選器之間的區別有疑問。PropertyPlaceholderConfigurer vs篩選器 - Spring豆豆
我一直在看示例,它看起來即使過濾器被定義並標記爲在pom.xml中默認激活,但他們仍然使用Spring的applicationContext.xml中的PropertyPlaceholderConfigurer。
這意味着pom.xml引用了filter-LOCAL.properties,而applicationContext.xml引用了application.properties並且它們都包含相同的設置。
這是爲什麼?這是應該如何完成的?我能夠運行目標mvn jetty:運行時不存在application.properties,但是如果我將設置添加到與filter-LOCAL.properties不同的application.properties,它們似乎不會被覆蓋。
這裏是我的意思的例子:
的pom.xml
<profiles> <profile> <id>LOCAL <activation> <activeByDefault>true </activation> <properties> <env>LOCAL </properties> </profile> </profiles>
的applicationContext.xml
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:application.properties </list> </property> <property name="ignoreResourceNotFound" value="true"/> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
application.properties的內容的一個例子和filters-LOCAL.properties
jdbc.driver=org.postgresql.Driver jdbc.url=jdbc:postgresql://localhost/shoutbox_dev jdbc.username=tester jdbc.password=tester
我可以從ApplicationContext中刪除propertyConfigurer,創建PROD過濾器和無視application.properties文件,或意志給部署到生產服務器時,我發出?
你有什麼我應該谷歌找出如何使用Maven按照你的建議選擇彈簧屬性的建議嗎? – John 2010-05-03 11:46:47
@John:就個人而言,我更喜歡Ivy和Ant,但校長應該是一樣的。取決於我爲哪個環境構建,我複製不同的屬性文件並將其重命名爲例如application.properties。然後,我導入PropertyPlaceholderConfigurer的Spring應用程序上下文僅知道application.properties。如果這是從test.properties複製的,或者production.properties對於Spring容器是未知的。只有我的測試上下文知道我的JUnit測試的測試屬性。 – Espen 2010-05-03 11:52:30
-1 - 爲每個環境構建可部署的單獨實例是個不錯的主意。它不能很好地擴展,你最終必須破解公開的戰爭,以查看哪些屬性包含在內。看到我的答案在這裏的另一種方法:http://stackoverflow.com/questions/1311360/property-placeholder-location-from-another-property/1312341#1312341 – Pablojim 2010-05-03 18:44:57