根据我的经验,我还必须构建一次基于 Spring 的 Web 应用程序并部署在许多不同的环境中,因此我试图向您展示适合我的解决方案。
我找不到将这个任务交给 Maven 的方法,所以我决定将配置外部化,并发现利用 Spring 我只需执行两个步骤就可以实现:
- 通过读取包含环境名称(例如 DEV、TEST、PRODUCTION)的文件,使应用程序“了解”其部署到的环境
- 根据找到的环境值,设置系统属性,然后加载对应文件夹下的配置文件。
有了Tomcat,我们在shared/classes下设置配置文件夹结构如下:
我们将包含环境描述的application-config.properties文件放入application-config folder:
application-config.env=DEV
以及每个子文件夹下的相同配置文件正确配置为所需的相关环境。
之后,我们必须向 Spring 应用程序上下文添加另一个带有一些 bean 定义的 application-config-context.xml,以便与 org.springframework.beans.factory.config.PropertiesFactoryBean 交互:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="environmentDetector" class="com.mycompany.myapp.spring.util.PropertiesBasedEnvironmentDetector" init-method="init" >
<property name="properties" ref="environmentDefinitionProperties" />
<property name="environmentDefinitionPropertyName" value="application-config.env"/>
</bean>
<bean id="environmentDefinitionProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:application-config/application-config.properties</value>
</list>
</property>
</bean>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="false" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="properties" ref="applicationExternalProperties" />
</bean>
<bean id="applicationExternalProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean" >
<property name="location" value="${application-config.prefix}application-config/${application-config.env}/application.properties" />
</bean>
</beans>
我必须编写一个类来覆盖接口 org.springframework.beans.factory.config.BeanFactoryPostProcessor 的 Spring 的 postProcessBeanFactory 方法,如下所示:
package com.mycompany.myapp.doc.spring.util;
import java.util.Properties;
import javax.annotation.PostConstruct;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.PriorityOrdered;
public class PropertiesBasedEnvironmentDetector implements BeanFactoryPostProcessor, PriorityOrdered{
private static final Log log = LogFactory.getLog(PropertiesBasedEnvironmentDetector.class);
private Properties properties;
private String environmentDefinitionPropertyName ="env";
private String defaultEnvironment="DEV";
private String environmentSystemPropertyName;
private int order = PriorityOrdered.HIGHEST_PRECEDENCE;
private String prefixSystemPropertyName = "application-config.prefix";
private String prefixDefault = "classpath:";
private String prefix;
public void setProperties(Properties properties) {
this.properties = properties;
}
@PostConstruct
public void init()
{
if (environmentSystemPropertyName == null) {
environmentSystemPropertyName = environmentDefinitionPropertyName;
}
String activeEnvironment = properties.getProperty(environmentDefinitionPropertyName, defaultEnvironment);
prefix = properties.getProperty(prefixSystemPropertyName);
if (prefix == null) {
prefix = prefixDefault;
properties.put(prefixSystemPropertyName, prefix);
}
System.setProperty(environmentSystemPropertyName , activeEnvironment);
System.setProperty(prefixSystemPropertyName , prefix);
log.warn("Initializing Environment: "+activeEnvironment);
}
public String getEnvironmentDefinitionPropertyName() {
return environmentDefinitionPropertyName;
}
public void setEnvironmentDefinitionPropertyName(
String environmentDefinitionPropertyName) {
this.environmentDefinitionPropertyName = environmentDefinitionPropertyName;
}
public String getDefaultEnvironment() {
return defaultEnvironment;
}
public void setDefaultEnvironment(String defaultEnvironment) {
this.defaultEnvironment = defaultEnvironment;
}
public String getEnvironmentSystemPropertyName() {
return environmentSystemPropertyName;
}
public void setEnvironmentSystemPropertyName(String environmentSystemPropertyName)
{
this.environmentSystemPropertyName = environmentSystemPropertyName;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException
{
String activeEnvironment = System.getProperty(environmentSystemPropertyName);
log.warn("PostProcessing ApplicationContext for Environment: "+activeEnvironment+" from "+prefix);
}
@Override
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}
当应用程序启动时,Spring 会加载所有上下文并调用我们的自定义类。
在init() 方法中,首先通过注入的properties 属性加载environmentDefinitionPropertyName,然后将其设置为系统属性,并在bean 定义中设置environmentDefinitionPropertyName 值作为键。
之后,PropertyPlaceholderConfigurer 可以加载属性文件位置,因为它已解析:
<property name="location" value="${application-config.prefix}application-config/${application-config.env}/application.properties" />
进入
<property name="location" value="classpath:application-config/DEV/application.properties" />
这种方法具有以下主要优点:
- 您可以轻松添加更多环境,只需在原始配置文件夹下添加另一个文件夹
- 原始应用程序包在所有环境中都相同
- 属性文件具有“普通”
key=value 格式,就像您的 application.properties 文件一样。
希望这会有所帮助,也可能对其他人有所帮助。