首页  :: 新随笔  :: 管理

CompositeConfiguration的用法

Posted on 2016-08-19 14:39  季枫  阅读(1074)  评论(0编辑  收藏  举报

 

public class Mytest {

    private static ApplicationContext applicationContext;

    public static void main(String[] args) throws Exception {
        
        //获取spring上下文
        applicationContext = new ClassPathXmlApplicationContext("applicationContext-config.xml");
        
        CompositeConfiguration configuration = new CompositeConfiguration();
        XMLConfiguration config = (XMLConfiguration) applicationContext.getBean("xmlConfiguration");
        configuration.addConfiguration(config);
        
        String value = configuration.getString("test.url");
        System.out.println(value);//运行结果http://www.baidu.com
    }
}

  

applicationContext-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="xmlConfiguration" class="org.apache.commons.configuration.XMLConfiguration">
          <constructor-arg index="0" type="java.net.URL" value="classpath:local-app.xml"/>
   </bean>
</beans>

  

 

local-app.xml

<?xml version="1.0" encoding="UTF-8"?>
<appconfig>
    <test>
        <url>http://www.baidu.com</url>
    </test>
</appconfig>

  

智读 | 成都会领科技有限公司官网 | 智读App下载 | 每天听本书的博客 | |