commons-configuration 使用

commons-configuration
1 pom中的依赖
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
</dependency>

2 applicationContext-config.xml

<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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">

<bean name="PropertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="appConfiguration" />
</bean>

<bean id="appConfiguration" class="cn.focus.dc.focuswap.config.ConfigBean">
<property name="configurations">
<list>

<!-- 本地测试配置文件 -->
<bean class="org.apache.commons.configuration.XMLConfiguration">
<constructor-arg index="0" type="java.net.URL"
value="classpath:config/local.app.config.xml" />
</bean>

<!-- 测试环境配置文件 -->
<bean class="org.apache.commons.configuration.XMLConfiguration">
<constructor-arg index="0" type="java.net.URL"
value="classpath:config/test.app.config.xml" />
</bean>

<!-- SCE测试环境配置文件 -->
<bean class="org.apache.commons.configuration.XMLConfiguration">
<constructor-arg index="0" type="java.net.URL"
value="classpath:config/test_sce.app.config.xml" />
</bean>

<!-- 生产环境配置文件 -->
<bean class="org.apache.commons.configuration.XMLConfiguration">
<constructor-arg index="0" type="java.net.URL"
value="classpath:config/product.app.config.xml" />
</bean>

</list>

</property>

</bean>


</beans>



3、

package cn.focus.dc.config;

import java.util.Arrays;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

public class ConfigBean implements InitializingBean, FactoryBean<Object> {

public static final String APP_ENV_KEY = "spring.profiles.active";

private static Log log = LogFactory.getLog(ConfigBean.class);

private CompositeConfiguration configuration;

private Configuration[] configurations;

public ConfigBean() {
}

public ConfigBean(Configuration configuration) {
this.configuration = new CompositeConfiguration(configuration);
}

public void afterPropertiesSet() {
if (configuration == null && (configurations == null || configurations.length == 0)) {
throw new IllegalArgumentException("no configuration object or location specified");
}
if (configuration == null) {
configuration = new CompositeConfiguration();
}
if (configurations != null) {

// 获取应用运行的环境 本地 测试服务器 还是正式服务器
String envType = System.getProperty(APP_ENV_KEY, "local");

log.info("应用运行环境变量appEnv:" + APP_ENV_KEY + " ============ " + "运行环境envType:" + envType);

int beginIndex = 0;

// 测试服务器
if ("test".equals(envType)) {
beginIndex = 1;
}

// SCE测试服务器
if ("test_sce".equals(envType)) {
beginIndex = 2;
}

// 生产环境正式服务器
if ("product".equals(envType)) {
beginIndex = 3;
}

for (int i = beginIndex; i < configurations.length; i++) {
configuration.addConfiguration(configurations[i]);
}
}
}

public void setConfigurations(Configuration[] cfgs) {
if (cfgs == null) {
this.configurations = new Configuration[0];
} else {
this.configurations = Arrays.copyOf(cfgs, cfgs.length);
}
}

public Object getObject() {
return (configuration != null) ? ConfigurationConverter.getProperties(configuration) : null;
}

public Class<?> getObjectType() {
return java.util.Properties.class;
}

public boolean isSingleton() {
return true;
}

public Object getProperty(String key) {
// TODO List属性的获取可能会有问题,有待改进
return configuration.getString(key);
}

}



4、启动jetty_dev
ps -ef |grep jetty_dev
1083 8099 1 0 Sep06 ? 00:01:24 /opt/java/bin/java -Xmx512m -Xms512m -Xss256k -XX:MaxPermSize=256m -Djava.awt.headless=true [color=red][b]-Dspring.profiles.active=test[/b][/color] -Djetty.state=/opt/jetty_dev/jetty.state -Djetty.home=/opt/jetty_dev -Djava.io.tmpdir=/tmp -jar /opt/jetty_dev/start.jar --daemon

在/etc/init.d/jetty_dev中添加
JAVA_OPTIONS="-Xmx512m -Xms512m -Xss256k -XX:MaxPermSize=256m -Djava.awt.headless=true -Dspring.profiles.active=test"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值