java中读取配置文件的方法
来源:互联网 发布:福州市第一中学 知乎 编辑:程序博客网 时间:2024/05/28 20:19
一、使用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文件
package com.styspace; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.XMLConfiguration; public class xmlLoaderTest { public static void main(String[] args) throws ConfigurationException{ Configuration config = new XMLConfiguration("com/styspace/config.xml"); String name = config.getString("Account.name"); System.out.println("name:" + name); } }
使用到的config.xml内容如下:
<?xml version="1.0" encoding="gbk"?> <Accounts> <Account type="by0003"> <code>100001</code> <pass>123</pass> <name>李四</name> <money>1000000.00</money> </Account> </Accounts>
2、读取properties文件
package com.styspace; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; public class peropertiesLoaderTest { public static void main(String[] args) throws ConfigurationException{ Configuration config = new PropertiesConfiguration("com/styspace/config.properties"); String name = config.getString("name"); System.out.println("name:" + name); } }
threads.max=50 threas.min=2 timout=15.52 interactive=true color=red speed=50 name=Default User
二、使用java.util.Properties读取
import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesTest { public static void main(String[] args){ PropertiesTest pt = new PropertiesTest(); try { pt.getProperties(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void getProperties() throws IOException { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("com/styspace/config.properties"); System.out.println("begin!!!"); Properties properties = new Properties(); try{ properties.load(inputStream); }catch (IOException ioE){ ioE.printStackTrace(); }finally{ inputStream.close(); } System.out.println("name:"+properties.getProperty("name")); } }
ClassLoader()和URLClassLoader()区别:ClassLoader()只能查找src目录下的文件,而URLClassLoader()则能查找任意目录下的文件。
三、spring中配置文件的读取
1、ClassPathXmlApplicationContext:从类路径中加载。
2、FileSystemXmlApplicationContext:从文件系统加载。
3、XmlWebApplicationContext:从web系统中加载。
1、使用bean工厂获取bean
BeanFactory factory = null; //声明 ClassPathResource resource = new ClassPathResource("spring.xml");//类路径 factory= new XmlBeanFactory(resource); FileSystemResource fileSystemResource = new FileSystemResource("D:\\Ncode\\mcode\\sday02\\src\\spring.xml");//文件路径 factory= new XmlBeanFactory(fileSystemResource); //XmlBeanFactory(参数可以是resource或者fileSystemResource等 //但是不能是 res 原因可以查看:文档Part III. Core Technologies 6. Resources //中6.2 The Resource interface 有关isOpen方法的说明); //InputStreamResource res = new InputStreamResource(new FileInputStream("D:\\Ncode\\mcode\\sday02\\src\\spring.xml"));//系统路径 HelloService helloService = factory.getBean("helloServiceImpl", HelloServiceImpl.class); helloService.sayHello();
2、使用上下文(Context)
上下文更加高级:提供文本信息解析工具,包括对国际化支持;提供载入文件资源的通用方法,如图片;可以向注册为监听器的bean发送事件。
在很少的情况下,使用BeanFactory。
ApplicationContext context = new FileSystemXmlApplicationContext("file:D:\\Ncode\\mcode\\sday02\\src\\spring.xml"); //从类路径 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml"); HelloService helloService = context.getBean("helloServiceImpl", HelloServiceImpl.class); helloService.sayHello();
3、在web应用中使用
3.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();
3.2、使用WebApplicationContextUtils工具类
//直接采用getWebApplicationContext(getServletContext()) 获取context对象 WebApplicationContext context= WebApplicationContextUtils.getWebApplicationContext(getServletContext()); //context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); System.out.println(context); HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class); 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
- java中读取配置文件的方法
- java中读取配置文件的方法
- java中读取配置文件的方法
- java中读取配置文件的方法
- java中读取配置文件的方法
- java读取配置文件的方法
- java中读取配置文件中数据的具体方法
- java中配置文件的读取
- java中读取配置文件信息的六种方法
- java中读取配置文件信息的六种方法
- java读取项目中配置文件的几种方法
- Java中spring读取配置文件的几种方法
- java中读取配置文件的方法fileSystemXmlapplicationContext与classPathXmlApplicationContext
- java中读取服务器配置文件方法
- java读取配置文件方法
- vc中读取配置文件的方法
- vc中读取配置文件的方法
- Windows服务中读取配置文件的方法
- 移动网络手机抓包分享
- Javascript 基础之Date对象
- ThreadLocal误区解密
- 一代记忆
- 灵犀语音助手太强大了,下雨了果然提醒我带伞
- java中读取配置文件的方法
- 我要寄件 网上寄件
- 金丝皇菊大菊花茶一朵一杯大的菊花贡菊金丝黄菊婺源茶叶花茶批发
- 一台电脑接两个显示器,双屏显示 安装双显示器 电脑后面有两个VGA接口 干什么用的 一台主机两个显示 器怎么连接 HDMI转VGA转换器
- 服务器相关 HTTP 请求错误
- 创意苹果LOGO灯iPhone6 Plus发光灯苹果6荧光灯苹果4.7/5.5LED灯
- note2便携式WLAN热点开启后没连接时自动关闭时长在哪设置
- Win7如何共享文件 Win7如何分享局域网并设置共享文件夹账户和密码
- GitHub入门教程 SVN 主干(trunk)、分支(branch )、标记(tag)