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

一、使用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文件

  1. <span style="font-family:Microsoft YaHei;font-size:12px;">package com.styspace;
  2. import org.apache.commons.configuration.Configuration;
  3. import org.apache.commons.configuration.ConfigurationException;
  4. import org.apache.commons.configuration.XMLConfiguration;
  5. public class xmlLoaderTest {
  6. public static void main(String[] args) throws ConfigurationException{
  7. Configuration config = new XMLConfiguration("com/styspace/config.xml");
  8. String name = config.getString("Account.name");
  9. System.out.println("name:" + name);
  10. }
  11. }
  12. </span>

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

使用到的config.xml内容如下:

  1. <span style="font-family:Microsoft YaHei;font-size:12px;"><?xml version="1.0" encoding="gbk"?>
  2. <Accounts>
  3. <Account type="by0003">
  4. <code>100001</code>
  5. <pass>123</pass>
  6. <name>李四</name>
  7. <money>1000000.00</money>
  8. </Account>
  9. </Accounts></span>

2、读取properties文件

  1. <span style="font-family:Microsoft YaHei;font-size:12px;">package com.styspace;
  2. import org.apache.commons.configuration.Configuration;
  3. import org.apache.commons.configuration.ConfigurationException;
  4. import org.apache.commons.configuration.PropertiesConfiguration;
  5. public class peropertiesLoaderTest {
  6. public static void main(String[] args) throws ConfigurationException{
  7. Configuration config = new PropertiesConfiguration("com/styspace/config.properties");
  8. String name = config.getString("name");
  9. System.out.println("name:" + name);
  10. }
  11. }
  12. </span>

使用到的config.properties文件内容如下:

  1. <span style="font-family:Microsoft YaHei;font-size:12px;">threads.max=50threas.min=2
  2. timout=15.52
  3. interactive=true
  4. color=red
  5. speed=50
  6. name=Default User</span>

二、使用java.util.Properties读取

  1. <span style="font-family:Microsoft YaHei;font-size:12px;">package com.styspace;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.Properties;
  5. public class PropertiesTest {
  6. public static void main(String[] args){
  7. PropertiesTest pt = new PropertiesTest();
  8. try {
  9. pt.getProperties();
  10. } catch (IOException e) {
  11. // TODO Auto-generated catch block
  12. e.printStackTrace();
  13. }
  14. }
  15. private void getProperties() throws IOException {
  16. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("com/styspace/config.properties");
  17. System.out.println("begin!!!");
  18. Properties properties = new Properties();
  19. try{
  20. properties.load(inputStream);
  21. }catch (IOException ioE){
  22. ioE.printStackTrace();
  23. }finally{
  24. inputStream.close();
  25. }
  26. System.out.println("name:"+properties.getProperty("name"));
  27. }
  28. }
  29. </span>

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

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

三、spring中配置文件的读取

1、ClassPathXmlApplicationContext:从类路径中加载。

2、FileSystemXmlApplicationContext:从文件系统加载。

3、XmlWebApplicationContext:从web系统中加载。

1、使用bean工厂获取bean

  1. <span style="font-family:Microsoft YaHei;font-size:12px;">    BeanFactory factory = null; //声明
  2. ClassPathResource resource = new ClassPathResource("spring.xml");//类路径
  3. factory= new XmlBeanFactory(resource);
  4. FileSystemResource fileSystemResource = new FileSystemResource("D:\\Ncode\\mcode\\sday02\\src\\spring.xml");//文件路径
  5. factory= new XmlBeanFactory(fileSystemResource);
  6. //XmlBeanFactory(参数可以是resource或者fileSystemResource等
  7. //但是不能是 res 原因可以查看:文档Part III. Core Technologies 6. Resources
  8. //中6.2 The Resource interface 有关isOpen方法的说明);
  9. //InputStreamResource res = new InputStreamResource(new FileInputStream("D:\\Ncode\\mcode\\sday02\\src\\spring.xml"));//系统路径
  10. HelloService helloService = factory.getBean("helloServiceImpl", HelloServiceImpl.class);
  11. helloService.sayHello();</span>

2、使用上下文(Context)

上下文更加高级:提供文本信息解析工具,包括对国际化支持;提供载入文件资源的通用方法,如图片;可以向注册为监听器的bean发送事件。

在很少的情况下,使用BeanFactory。

  1. <span style="font-family:Microsoft YaHei;font-size:12px;">    //从文件系统
  2. ApplicationContext context = new FileSystemXmlApplicationContext("file:D:\\Ncode\\mcode\\sday02\\src\\spring.xml");
  3. //从类路径
  4. ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
  5. HelloService helloService =  context.getBean("helloServiceImpl", HelloServiceImpl.class);
  6. helloService.sayHello();</span>

3、在web应用中使用

3.1、使用XmlWebApplicationContext

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

3.2、使用WebApplicationContextUtils工具类

  1. //直接采用getWebApplicationContext(getServletContext()) 获取context对象
  2. WebApplicationContext  context=
  3. WebApplicationContextUtils.getWebApplicationContext(getServletContext());
  4. //context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
  5. System.out.println(context);
  6. HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
  7. helloDao.sayHello()

两者的区别是:

1、当采用getWebApplicationContext(getServletContext())获取context对象的时候,输出的context对象为null  所以在使用

context.getBean("helloDaoImpl", HelloDaoImpl.class);会出现空指针的异常

2、当采用getRequiredWebApplicationContext(getServletContext());获取context对象的时候 会出现如下bug

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered

最新文章

  1. Lind.DDD.Domain领域模型介绍
  2. Maven命令行使用:mvn clean package(打包)
  3. doc2vec使用说明(二)gensim工具包 LabeledSentence
  4. 如何查看前端部署的tracker代码
  5. oracle 导出导入常见问题
  6. 清除浮动2-父元素设置overflow:hidden
  7. 【转】SQLServer XML类型
  8. Python的进程间通信
  9. 百度地图Api 根据两个坐标点计算距离
  10. 关于一些Android冷知识
  11. 最简单实用的JQuery实现banner图中的text打字动画效果!!!
  12. python_如何让字典保持有序?
  13. Android中刷新Invalidate和postInvalidate的区别
  14. mouseover与mouseenter,mouseout与mouseleave的区别
  15. 教你如何使用android studio发布release 版本【转】
  16. ZOJ-3537
  17. java 实现简单的链式栈
  18. lesson4Embedding-fastai
  19. Binutils工具集中的一些比较常用的工具
  20. pyqt5:图片自适应QLabel大小和图片移除

热门文章

  1. ssdb使用笔记
  2. MyBatis基础入门《十七》动态SQL
  3. FileFilter文件过滤器
  4. shadow一键安装
  5. jquery $.ajax $.get $.post的区别?
  6. &lt;meta&gt;标签中,X-UA-Compatible和IE=Edge,chrome=1的作用
  7. MATLAB中文件的读写和数据的导入导出
  8. django migrate无效的解决方法
  9. &lt;转&gt;jmeter(五)JDBC Request
  10. ORM for Net主流框架汇总