Java中读取配置文件的方式

33 篇文章 0 订阅


一、使用org.apache.commons.configuration

       需要使用的jar包:commons-collections-3.2.1.jar、commons-configuration-1.10.jar、commons-lang-2.6.jar和commons-logging-1.2.jar。可以读取的配置文件:xml和properties。

        1、读取.xml文件

Configuration config = new XMLConfiguration("com/styspace/config.xml");  
String name = config.getString("Account.name"); 

        需要注意的是config.getString("Account.name")中的参数是Account.name,这个参数是XPath格式的,而且不能包含xml中的根元素。xml文件格式形如:

<Accounts>   
    <Account type="by0003">    
        <code>100001</code>   
        <pass>123</pass>   
        <name>李四</name>    
        <money>1000000.00</money>    
    </Account>    
</Accounts>

       2、读取.properties文件

Configuration config = new PropertiesConfiguration("com/styspace/config.properties");  
String name = config.getString("name");  

        properties文件格式形如:

interactive=true  
color=red  
speed=50  
name=Default User

二、使用Java.util.Properties读取

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties");
Properties p = new Properties();
p.load(inputStream);
String ip = p.getProperty("ip");

        需要注意的是getClassLoader().getResourceAsStream()的参数是项目根目录下的路径,即使config.properties在相同的目录下,也不能写成getClassLoader().getResourceAsStream("config.properties"),这样程序会报错,得到的InputStream是null值。

        ClassLoader()和URLClassLoader()区别:ClassLoader()只能查找src目录下的文件,而URLClassLoader()则能查找任意目录下的文件。

三、spring读取配置文件

        1、XmlBeanFactory:BeanFactory实现,提供基本的IoC容器功能,可以从classpath或文件系统等获取资源;

            1>用法1

File file = new File(“ApplicationContext.xml”); 
Resource resource = new FileSystemResource(file); 
BeanFactory beanFactory = new XmlBeanFactory(resource);

            2>用法2

Resource resource = new ClassPathResource(“ApplicationContext.xml”); 
BeanFactory beanFactory = new XmlBeanFactory(resource);

        2、ClassPathXmlApplicationContext:ApplicationContext实现,从classpath获取配置文件;

BeanFactory beanFactory = new ClassPathXmlApplicationContext(“ApplicationContext.xml”);

        3、FileSystemXmlApplicationContext:ApplicationContext实现,从文件系统获取配置文件。

BeanFactory beanFactory = new FileSystemXmlApplicationContext(“ApplicationContext.xml”);

四、利用spring中org.springframework.beans.factory.support.PropertiesBeanDefinitionReader读取properties 文件

BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("beanConfig.properties"));
BeanFactory factory = (BeanFactory)reg;
HelloBean helloBean = (HelloBean)factory.getBean("helloBean");

五、在Web应用中读取配置文件

        1、使用XmlWebApplicationContext:

XmlWebApplicationContext context = new XmlWebApplicationContext(); 
//默认的路径/WEB-INF/applicationContext.xml
//applicationContext.xml文件名称 可以任意起
//重新设置路径
//context.setConfigLocations(new String[] {"/WEB-INF/classes/applicationContext.xml"}); 
//设置ServletContext上下文为web应用程序的上下文
context.setServletContext(getServletContext());
//刷新
context.refresh();
//根据id名称获取
HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
//执行helloDao对象的方法
helloDao.sayHello();

        2、使用WebApplicationContextUtils工具类:

//直接采用getWebApplicationContext(getServletContext()) 获取context对象
WebApplicationContext  context= WebApplicationContextUtils.getWebApplicationContext(getServletContext());
//或者context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
helloDao.sayHello()

注意:

        当采用getWebApplicationContext(getServletContext())获取context对象的时候,输出的context对象为null,所以在使用context.getBean("helloDaoImpl", HelloDaoImpl.class);会出现空指针的异常。而当采用getRequiredWebApplicationContext(getServletContext());获取context对象时会出现如下异常:java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered。



参考:http://blog.sina.com.cn/s/blog_99201d8901012dzu.html

           http://blog.csdn.net/stypace/article/details/38414871

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值