根据我的经验,我曾经需要构建一个基于Spring的Web应用并在许多不同的环境中部署,因此我试图向您展示对我有效的解决方案。
我找不到将此任务移交给Maven的方法,因此我决定外部化配置,并发现利用Spring可以仅执行两个步骤即可实现:
1. 通过读取包含环境名称(例如DEV、TEST、PRODUCTION)的文件,使应用程序“知道”它被部署到的环境。
2. 根据找到的环境值,设置系统属性,然后加载放置在相应文件夹中的配置文件。
有了Tomcat,我们将配置文件夹结构设置在shared/classes下,像这样:
config structure
然后我们将application-config.properties文件放置在application-config文件夹中,其中包含环境的描述。
application-config.env=DEV
需要在每个子文件夹下配置相同的配置文件,以满足相关环境的要求。
之后,我们需要向Spring应用程序上下文添加另一个application-config-context.xml
,其中包含一些bean定义,以便与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>
我不得不编写一个类来覆盖Spring接口org.springframework.beans.factory.config.BeanFactoryPostProcessor
的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文件。
希望这对您有所帮助,并且也能帮助其他人。