2010-05-03 56 views
2

我對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文件,或意志給部署到生產服務器時,我發出?

回答

2

您應該使用Maven來選擇要使用哪個Spring屬性文件,具體取決於您正在構建的環境。

當你在你的IDE中測試時,你應該從測試中啓動Spring容器,而不是使用Maven來管理你的依賴關係。

+0

你有什麼我應該谷歌找出如何使用Maven按照你的建議選擇彈簧屬性的建議嗎? – John 2010-05-03 11:46:47

+0

@John:就個人而言,我更喜歡Ivy和Ant,但校長應該是一樣的。取決於我爲哪個環境構建,我複製不同的屬性文件並將其重命名爲例如application.properties。然後,我導入PropertyPlaceholderConfigurer的Spring應用程序上下文僅知道application.properties。如果這是從test.properties複製的,或者production.properties對於Spring容器是未知的。只有我的測試上下文知道我的JUnit測試的測試屬性。 – Espen 2010-05-03 11:52:30

+1

-1 - 爲每個環境構建可部署的單獨實例是個不錯的主意。它不能很好地擴展,你最終必須破解公開的戰爭,以查看哪些屬性包含在內。看到我的答案在這裏的另一種方法:http://stackoverflow.com/questions/1311360/property-placeholder-location-from-another-property/1312341#1312341 – Pablojim 2010-05-03 18:44:57

2

爲了記錄在案,這裏是什麼的博客系列的OP在this comment下面寫的作者:

我曾經是Spring的 PropertyPlaceholderConfigurer一個大風扇,但永遠 自從我開始使用maven使用一個過濾器文件 這裏解釋我不 找到它作爲Maven的過濾器是有用的, ,或通過具有多個在POM 型材的不同 部署層,每個配置文件指定 的屬性。

最大的抱怨我與 PropertyPlaceholderConfigurer是 你只能有一個 PropertyPlaceholderConfigurer豆。 它沒有很好的記錄。

使用maven的過濾文件,你可以有多達 。

的另一個原因,我更喜歡Maven的 過濾器是與他們,你可以做一個 「MVN包」,然後閒逛在 目標目錄和眼球 過濾配置文件,看看它 做。隨着春天的 PropertyPlaceholderConfigurer你 找不到什麼代替 ,直到應用程序啓動。

我第二這個意見,更喜歡使用PropertyPlaceholderConfigurer和Antrun插件運行我的測試中,當複製說test.propertiesapplication.properties過濾器的方法。所有主要的IDE(Eclipse,IntelliJ,NetBeans)都支持使用過濾的資源,所以我不明白爲什麼我不應該使用它。

+0

我更喜歡使用Spring的元素,因爲我不是全局屬性的忠實粉絲。 – Espen 2010-05-05 16:53:48