commons-configuration 使用

commons-configuration 使用

  • 博客分类: 
  • Java
 
commons-configuration 
1 pom中的依赖 
<dependency> 
<groupId>commons-configuration</groupId> 
<artifactId>commons-configuration</artifactId> 
</dependency> 

2 applicationContext-config.xml 
Java代码   收藏代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  4.         http://www.springframework.org/schema/beans/spring-beans.xsd   
  5.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">  
  6.   
  7.     <bean name="PropertyPlaceholderConfigurer"  
  8.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  9.         <property name="properties" ref="appConfiguration" />  
  10.     </bean>  
  11.   
  12.     <bean id="appConfiguration" class="cn.focus.dc.focuswap.config.ConfigBean">  
  13.         <property name="configurations">  
  14.             <list>  
  15.   
  16.                 <!-- 本地测试配置文件 -->  
  17.                 <bean class="org.apache.commons.configuration.XMLConfiguration">  
  18.                     <constructor-arg index="0" type="java.net.URL"  
  19.                         value="classpath:config/local.app.config.xml" />  
  20.                 </bean>  
  21.   
  22.                 <!-- 测试环境配置文件 -->  
  23.                 <bean class="org.apache.commons.configuration.XMLConfiguration">  
  24.                     <constructor-arg index="0" type="java.net.URL"  
  25.                         value="classpath:config/test.app.config.xml" />  
  26.                 </bean>  
  27.                   
  28.                 <!-- SCE测试环境配置文件 -->  
  29.                 <bean class="org.apache.commons.configuration.XMLConfiguration">  
  30.                     <constructor-arg index="0" type="java.net.URL"  
  31.                         value="classpath:config/test_sce.app.config.xml" />  
  32.                 </bean>  
  33.   
  34.                 <!-- 生产环境配置文件 -->  
  35.                 <bean class="org.apache.commons.configuration.XMLConfiguration">  
  36.                     <constructor-arg index="0" type="java.net.URL"  
  37.                         value="classpath:config/product.app.config.xml" />  
  38.                 </bean>  
  39.   
  40.             </list>  
  41.               
  42.         </property>  
  43.           
  44.     </bean>  
  45.   
  46.   
  47. </beans>  


3、 
Java代码   收藏代码
  1. package cn.focus.dc.config;  
  2.   
  3. import java.util.Arrays;  
  4.   
  5. import org.apache.commons.configuration.CompositeConfiguration;  
  6. import org.apache.commons.configuration.Configuration;  
  7. import org.apache.commons.configuration.ConfigurationConverter;  
  8. import org.apache.commons.logging.Log;  
  9. import org.apache.commons.logging.LogFactory;  
  10.   
  11. import org.springframework.beans.factory.FactoryBean;  
  12. import org.springframework.beans.factory.InitializingBean;  
  13.   
  14. public class ConfigBean implements InitializingBean, FactoryBean<Object> {  
  15.       
  16.     public static final String APP_ENV_KEY = "spring.profiles.active";  
  17.   
  18.     private static Log log = LogFactory.getLog(ConfigBean.class);  
  19.   
  20.     private CompositeConfiguration configuration;  
  21.   
  22.     private Configuration[] configurations;  
  23.   
  24.     public ConfigBean() {  
  25.     }  
  26.   
  27.     public ConfigBean(Configuration configuration) {  
  28.         this.configuration = new CompositeConfiguration(configuration);  
  29.     }  
  30.   
  31.     public void afterPropertiesSet() {  
  32.         if (configuration == null && (configurations == null || configurations.length == 0)) {  
  33.             throw new IllegalArgumentException("no configuration object or location specified");  
  34.         }  
  35.         if (configuration == null) {  
  36.             configuration = new CompositeConfiguration();  
  37.         }  
  38.         if (configurations != null) {  
  39.               
  40.             // 获取应用运行的环境 本地 测试服务器 还是正式服务器  
  41.             String envType = System.getProperty(APP_ENV_KEY, "local");  
  42.               
  43.             log.info("应用运行环境变量appEnv:" + APP_ENV_KEY + " ============ " + "运行环境envType:" + envType);  
  44.   
  45.             int beginIndex = 0;  
  46.               
  47.             // 测试服务器  
  48.             if ("test".equals(envType)) {  
  49.                 beginIndex = 1;  
  50.             }  
  51.               
  52.             // SCE测试服务器  
  53.             if ("test_sce".equals(envType)) {  
  54.                 beginIndex = 2;  
  55.             }  
  56.               
  57.             // 生产环境正式服务器  
  58.             if ("product".equals(envType)) {  
  59.                 beginIndex = 3;  
  60.             }  
  61.               
  62.             for (int i = beginIndex; i < configurations.length; i++) {  
  63.                 configuration.addConfiguration(configurations[i]);  
  64.             }  
  65.         }  
  66.     }  
  67.   
  68.     public void setConfigurations(Configuration[] cfgs) {  
  69.         if (cfgs == null) {  
  70.             this.configurations = new Configuration[0];  
  71.         } else {  
  72.             this.configurations = Arrays.copyOf(cfgs, cfgs.length);  
  73.         }  
  74.     }  
  75.   
  76.     public Object getObject() {  
  77.         return (configuration != null) ? ConfigurationConverter.getProperties(configuration) : null;  
  78.     }  
  79.   
  80.     public Class<?> getObjectType() {  
  81.         return java.util.Properties.class;  
  82.     }  
  83.   
  84.     public boolean isSingleton() {  
  85.         return true;  
  86.     }  
  87.   
  88.     public Object getProperty(String key) {  
  89.         // TODO List属性的获取可能会有问题,有待改进  
  90.         return configuration.getString(key);  
  91.     }  
  92.   
  93. }  


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  -Dspring.profiles.active=test -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、付费专栏及课程。

余额充值