- 浏览: 88331 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (66)
- log4j (3)
- hibernate (6)
- oracle (7)
- axis (1)
- tomcat (9)
- web (10)
- JS (7)
- Java web (12)
- jsp (5)
- myeclipse (2)
- struts1 (1)
- java (11)
- jbmp4 (2)
- 连接池 spring (1)
- java JBPM6 (1)
- websewvice (3)
- java spring AOP (1)
- JS,ajax (1)
- activiti5 (2)
- Java web soapUi (2)
- jstl (1)
- applet (1)
- lintCode刷题开始 (0)
- LintCode刷题 (1)
- kindeditor (2)
最新评论
一、使用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文件
[java] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;">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);
}
}
</span>
需要注意的是config.getString(“Account.name”)中的参数是Account.name,这个参数是XPath格式的,而且不能包含xml中的根元素。
使用到的config.xml内容如下:
[html] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;"><?xml version="1.0" encoding="gbk"?>
<Accounts>
<Account type="by0003">
<code>100001</code>
<pass>123</pass>
<name>李四</name>
<money>1000000.00</money>
</Account>
</Accounts></span>
2、读取properties文件
[java] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;">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);
}
}
</span>
使用到的config.properties文件内容如下:
[plain] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;">threads.max=50threas.min=2
timout=15.52
interactive=true
color=red
speed=50
name=Default User</span>
二、使用java.util.Properties读取
[java] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;">package com.styspace;
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"));
}
}
</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
[java] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;"> 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();</span>
2、使用上下文(Context)
上下文更加高级:提供文本信息解析工具,包括对国际化支持;提供载入文件资源的通用方法,如图片;可以向注册为监听器的bean发送事件。
在很少的情况下,使用BeanFactory。
[java] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;"> //从文件系统
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();</span>
3、在web应用中使用
3.1、使用XmlWebApplicationContext
[java] view plaincopy
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工具类
[java] view plaincopy
//直接采用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
感谢http://blog.csdn.net/stypace/article/details/38414871 经测试 前面第二种 读properties文件可用 注意jar包版本 注意properties文件的位置
需要使用的是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文件
[java] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;">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);
}
}
</span>
需要注意的是config.getString(“Account.name”)中的参数是Account.name,这个参数是XPath格式的,而且不能包含xml中的根元素。
使用到的config.xml内容如下:
[html] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;"><?xml version="1.0" encoding="gbk"?>
<Accounts>
<Account type="by0003">
<code>100001</code>
<pass>123</pass>
<name>李四</name>
<money>1000000.00</money>
</Account>
</Accounts></span>
2、读取properties文件
[java] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;">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);
}
}
</span>
使用到的config.properties文件内容如下:
[plain] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;">threads.max=50threas.min=2
timout=15.52
interactive=true
color=red
speed=50
name=Default User</span>
二、使用java.util.Properties读取
[java] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;">package com.styspace;
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"));
}
}
</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
[java] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;"> 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();</span>
2、使用上下文(Context)
上下文更加高级:提供文本信息解析工具,包括对国际化支持;提供载入文件资源的通用方法,如图片;可以向注册为监听器的bean发送事件。
在很少的情况下,使用BeanFactory。
[java] view plaincopy
<span style="font-family:Microsoft YaHei;font-size:12px;"> //从文件系统
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();</span>
3、在web应用中使用
3.1、使用XmlWebApplicationContext
[java] view plaincopy
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工具类
[java] view plaincopy
//直接采用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
感谢http://blog.csdn.net/stypace/article/details/38414871 经测试 前面第二种 读properties文件可用 注意jar包版本 注意properties文件的位置
发表评论
-
java程序中没有错,但是项目上面显示一个红叉的解决办法
2017-11-27 09:51 3068java程序中没有错,但是项目上面显示一个红叉的解决办法 ... -
setCharacterEncoding未定义
2017-08-22 09:44 667setCharacterEncoding未定义 ... -
xml型字符串解析时存在& < >符号时的解决方案
2015-07-30 15:08 1235问题产生: 在接口调用得出一个xml型字符串 ... -
SoapUI的简单应用
2015-07-01 14:55 745几个好用的学习链接 百度文库 使用soapUI对Web ... -
nosuchMethodError org.hibernate.lockmode
2015-06-09 08:28 626昨天复制项目jar整理后项目跑不起来,一直出现 nosuch ... -
JS展开折叠效果
2015-04-29 14:27 997亲手试验过 经过删减 某些代码 很好用 很精简的代码 很 ... -
JVM terminated. Exit code=-1
2015-04-07 11:13 783启动Eclipse时,启不起来JVM terminated. ... -
Java 中与时间有关的几个问题
2014-12-29 16:44 699Java 中与时间有关的几个问题 1. Date # 需 ... -
Java中的List按照元素的属性进行排序
2014-08-21 10:52 1055主要用到: Collections.sort()方法: 1 ... -
Clob2String代码
2014-04-03 14:30 593//oracle.sql.Clob类型转换成String类型 ...
相关推荐
java读取配置文件 在经常更换的一些东西 就可以用配置文件
在web项目中读取yml配置文件的工具类.可以实现将 server : port : portnumber : 8081 转换为 key为"server.port.portnumber",值为"8081"的Map,String>集合
利用java可以读取配置文件信息,可以流来处理这个问题
java如何读取properties配置文件
使用Java读取XML配置文件
java读取properties文件的工具类,传入配置文件名字和其中的key就可以读取
在项目中我们经常要把某些常量放在配置文件中,这样修改起来会比较方便,这个工具类可以帮我们直接在java类中读取配置文件内容
该资源详细介绍了Java如何读取各种配置文件。如xml ini 等等
java项目读取配置文件的实例代码,使用class.getClassLoader().getResourceAsStream方法。
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
纯java底层读取配置文件,生成map,可以根据key模糊匹配查询,封装好了方法。比如getVal("spring.dubbo*url")
这个工程是使用 java 来使用反射的原理读取 *.proproies 资源文件中的一些配置信息。利用此方法可以在没有使用框架的情况下可以让你的程序更加灵活易于修改。如:可以将一些数据库配置信息,重要信息写入到资源文件...
JAVA读取配置文件(资源文件)源代码;JAVA读取配置文件(资源文件)源代码;JAVA读取配置文件(资源文件)源代码
在java类里直接读取配置文件中的信息,用到的方法是this.getClass().getClassLoader().getResourceAsStream
Java中XML配置文件的读取(sax)
最近写一个小程序,需要读取保存在配置文件中的配置信息。 这是一个简单的例子,可以以这个程序为基础写一个完整的类出来。
java读取、更新ini配置文件,可以分块配置