简体   繁体   English

PropertyPlaceholderConfigurer与过滤器—春季豆

[英]PropertyPlaceholderConfigurer vs Filters — Spring Beans

I've got a question regarding the difference between PropertyPlaceholderConfigurer (org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) and normal filters defined in my pom.xml. 我有一个关于PropertyPlaceholderConfigurer(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer)与我的pom.xml中定义的普通过滤器之间的区别的问题。

I've been looking at examples, and it seems that even though filters are defined and marked to be active by default in the pom.xml they still make use of PropertyPlaceholderConfigurer in Spring's applicationContext.xml. 我一直在看示例,似乎即使在pom.xml中默认定义了过滤器并将其标记为处于活动状态,它们仍会使用Spring的applicationContext.xml中的PropertyPlaceholderConfigurer。

This means that the pom.xml has a reference to a filter-LOCAL.properties while applicationContext.xml has a reference to application.properties and they both contain the same settings. 这意味着pom.xml引用了filter-LOCAL.properties,而applicationContext.xml引用了application.properties,并且它们都包含相同的设置。

Why is that? 这是为什么? Is that how it is supposed to be done? 那是应该怎么做的? I'm able to run the goal mvn jetty:run without the application.properties present, but if I add settings to the application.properties that differ from the filter-LOCAL.properties they don't seem to override. 我可以在不存在application.properties的情况下运行目标mvn jetty:run,但是如果我向application.properties添加与filter-LOCAL.properties不同的设置,它们似乎不会被覆盖。

Here's an example of what I mean: 这是我的意思的示例:

pom.xml 的pom.xml

<profiles>  
        <profile>  
            <id>LOCAL  
            <activation>  
                <activeByDefault>true  
            </activation>   
            <properties>  
                <env>LOCAL  
            </properties>  
        </profile>  
    </profiles>

applicationContext.xml 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>

an example of the content of application.properties and filters-LOCAL.properties application.properties和filter-LOCAL.properties的内容示例

jdbc.driver=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost/shoutbox_dev
jdbc.username=tester
jdbc.password=tester

Can I remove the propertyConfigurer from the applicationContext, create a PROD filter and disregard the application.properties file, or will that give me issues when deploying to the production server? 我可以从applicationContext中删除propertyConfigurer,创建PROD过滤器,而忽略application.properties文件,还是在部署到生产服务器时出现问题?

You should rather use Maven to select which Spring properties file to use depending on which environment you're building for. 您应该宁愿使用Maven根据要构建的环境选择要使用的Spring属性文件。

When you're testing in your IDE, you should just start the Spring container from the test without using Maven for anything else than managing your dependencies. 在IDE中进行测试时,您应该仅从测试中启动Spring容器,而不要使用Maven来管理依赖关系。

For the record, here is what the author of the blog series the OP is following wrote in this comment : 作为记录,这是OP所关注的博客系列的作者在此评论中写道:

I used to be a big fan of Spring's PropertyPlaceholderConfigurer but ever since I started using maven I don't find it as useful as maven's filters, using either a filters file as explained here, or by having multiple profiles in the pom for the different deployment layers with each profile specifying the properties. 我曾经是Spring的PropertyPlaceholderConfigurer忠实拥护者,但是自从我开始使用maven以来,我发现它没有像maven的过滤器一样有用,它要么使用此处说明的过滤器文件,要么在pom中具有多个配置文件以用于不同的部署每个配置文件指定属性的图层。

The biggest gripe I have with the PropertyPlaceholderConfigurer is that you can only have one PropertyPlaceholderConfigurer bean. 我对PropertyPlaceholderConfigurer的最大抱怨是,您只能拥有一个PropertyPlaceholderConfigurer bean。 And it's not well documented. 而且没有很好的记录。

With maven's filter files you can have as many as you like. 使用maven的过滤器文件,您可以拥有任意数量的文件。

The other reason I prefer maven's filters is that with them you can do a 'mvn package' and then poke around in the target directory and eyeball the filtered config files and see what it did. 我更喜欢maven过滤器的另一个原因是,您可以使用它们执行“ mvn程序包”,然后在目标目录中四处浏览,查看过滤后的配置文件并查看其作用。 With Spring's PropertyPlaceholderConfigurer you don't find out what's been substituted until the app is started. 使用Spring的PropertyPlaceholderConfigurer ,直到启动应用程序后,您才能找到被替换的内容。

I second this opinion and prefer the filter approach over using the PropertyPlaceholderConfigurer and the Antrun plugin to copy say test.properties into application.properties when running my tests. 我支持这种观点,并且在运行测试时,更喜欢使用filter方法而不是使用PropertyPlaceholderConfigurer和Antrun插件将test.properties复制到application.properties And using filtered resources is well supported by all major IDEs (Eclipse, IntelliJ, NetBeans) so I don't see why I should not use it. 所有主要的IDE(Eclipse,IntelliJ,NetBeans)都很好地支持使用过滤的资源,因此我不明白为什么不使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM