我对PropertyPlaceholderConfigurer(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer)与我的pom.xml中定义的普通过滤器之间的区别有疑问。
我一直在查看示例,似乎即使在pom.xml中默认定义了过滤器并将其标记为活动,它们仍然在Spring的applicationContext.xml中使用PropertyPlaceholderConfigurer。
这意味着pom.xml引用了filter-LOCAL.properties,而applicationContext.xml引用了application.properties,它们都包含相同的设置。
为什么?这是应该怎么做的?我能够运行目标mvn jetty:在没有application.properties的情况下运行,但是如果我将application.properties添加到不同于filter-LOCAL.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文件,还是在部署到生产服务器时会出现问题?
答案 0 :(得分:2)
您应该使用Maven来选择要使用的Spring属性文件,具体取决于您正在构建的环境。
当您在IDE中进行测试时,您应该从测试中启动Spring容器,而不是使用Maven来管理依赖项。
答案 1 :(得分:2)
为了记录,以下是OP在this comment写的博客系列的作者:
我曾经是Spring的粉丝
PropertyPlaceholderConfigurer
但永远 自从我开始使用maven后,我没有 发现它像maven的过滤器一样有用, 使用过滤器文件作为 在这里解释,或通过多个 在pom中为不同的配置文件 每个配置文件的部署层 指定属性。我最大的抱怨
PropertyPlaceholderConfigurer
就是这样 你只能有一个PropertyPlaceholderConfigurer
bean。 而且没有详细记录。使用maven的过滤器文件,您可以拥有 你想要多少。
我更喜欢maven的另一个原因 过滤器是他们可以做到的 'mvn package'然后捅了一下 目标目录和眼球 过滤配置文件,看看它是什么 没有。随着春天的
PropertyPlaceholderConfigurer
你 不知道被替换了什么 直到应用程序启动。
我提出了这个意见,并且更喜欢使用过滤器方法,而不是在运行我的测试时使用PropertyPlaceholderConfigurer
和Antrun插件将test.properties
复制到application.properties
。所有主要的IDE(Eclipse,IntelliJ,NetBeans)都很好地支持使用过滤后的资源,所以我不明白为什么我不应该使用它。