2010-05-03 53 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