Java中读取配置文件的方式

来源:互联网 发布:杭州淘宝拍摄 编辑:程序博客网 时间:2024/06/05 00:54


一、使用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 0
原创粉丝点击