在 Spring、Maven 和 Eclipses 中处理 XML 文件中的属性值的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13297157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best ways to deal with properties values in XML file in Spring, Maven and Eclipses
提问by techsjs2012
I am working on a Spring WebFlow project which has a lot of property values in XML files, as any Spring programmer knows. I have database user names, password, URLs, etc.
我正在研究一个 Spring WebFlow 项目,它在 XML 文件中有很多属性值,任何 Spring 程序员都知道。我有数据库用户名、密码、URL 等。
We are using Eclipse with Spring WebFlow and Maven. We are trying to have an SA do the builds but the SA does not want to go into the XML files to change the values, but on the other hand, we don't know the production values. How do we work with this?
我们将 Eclipse 与 Spring WebFlow 和 Maven 一起使用。我们试图让 SA 进行构建,但 SA 不想进入 XML 文件来更改值,但另一方面,我们不知道生产值。我们如何处理这个问题?
回答by yorkw
Most SA are more willing and confident to deal with .propertiesfile rather than .xml.
大多数 SA 更愿意和自信地处理.properties文件而不是.xml.
Spring provide PropertyPlaceholderConfigurerto let you define everything into one or several .propertiesfile and substitute the placeholder in applicationContext.xml.
Spring 提供了PropertyPlaceholderConfigurer来让你将所有内容定义到一个或多个.properties文件中,并替换applicationContext.xml.
Create a app.propertiesunder src/main/resources/folder:
创建一个app.properties下src/main/resources/文件夹:
... ...
# Dadabase connection settings:
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/app_db
jdbc.username=app_admin
jdbc.password=password
... ...
And use PropertyPlaceholderConfigurer in applicationContext.xmllike so:
并applicationContext.xml像这样使用 PropertyPlaceholderConfigurer :
... ...
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>app.properties</value>
</property>
</bean>
... ...
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
Check out Spring PropertyPlaceholderConfigurer Examplefor more details.
查看Spring PropertyPlaceholderConfigurer 示例以获取更多详细信息。
In addition, from application deployment perspective, we usually package app in some executable format and the .properties files are usually packed inside the executable war or ear file. A simple solution is to configure your PropertyPlaceholderConfigurer bean to resolve properties from multiple location in a pre-defined order, so in the deployment environment, you can use a fixed location or environment variable to specify the properties file, also note that in order to simplify the deploy/configure task for SA, we usually use a single external .properties file define all runtime configuration, like so:
另外,从应用部署的角度来看,我们通常将app打包成某种可执行格式,.properties文件通常打包在可执行war或ear文件中。一个简单的解决方案是配置你的PropertyPlaceholderConfigurer bean,按照预定义的顺序从多个位置解析属性,所以在部署环境中,可以使用固定位置或者环境变量来指定属性文件,另外注意为了简化SA 的部署/配置任务,我们通常使用单个外部 .properties 文件定义所有运行时配置,如下所示:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- Default location inside war file -->
<value>classpath:app.properties</value>
<!-- Environment specific location, a fixed path on server -->
<value>file:///opt/my-app/conf/app.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>
Hope this helps.
希望这可以帮助。
回答by Oleksandr Bondarchuk
Also you can define a propertyConfigurerprogrammatically in configuration class:
您也可以propertyConfigurer在配置类中以编程方式定义:
@Configuration
@PropertySource("classpath:application.properties")
public class PropertiesConfiguration {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(Environment env) {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setEnvironment(env);
return configurer;
}
}

