Java读取properties配置文件
2017-06-27 17:36
246 查看
这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取
(一)利用spring读取properties 文件
利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件
构造如下config.properties文件properties代码
userDao.class=com.spring.dao.UserDao
属性文件中的"userDao"名称即是Bean的别名设定,.class用于指定类来源。
然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("config.properties"));
BeanFactory factory = (BeanFactory)reg;
UserDao userDao = (UserDao)factory.getBean("userDao");
(二)利用java.util.Properties读取属性文件
1. String str=File.separator;
InputStream path=this.getServletContext().getResourceAsStream(str+"WEB-INF"+str+"classes"+str+"password.properties");
//InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("password.properties");
/*File filepath=new File(this.getServletContext().getRealPath(str+"WEB-INF"+str+"classes")+str+"password.properties");
InputStream path=new FileInputStream(filepath);*/
Properties pros = new Properties();
try {
pros.load(path);
} catch (IOException ex) {
//System.out.println("file is not exist");
errorMessage="资源文件不存在";
}
System.out.println("username:"+p.getProperty("username")+",password:"+p.getProperty("password"));
2. import org.springframework.core.io.ClassPathResource;
ClassPathResource cr = new ClassPathResource("password.properties");//会重新加载spring框架
Properties pros = new Properties();
try {
pros.load(cr.getInputStream());
} catch (IOException ex) {
//System.out.println("file is not exist");
errorMessage="资源文件不存在";
}
(一)利用spring读取properties 文件
利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件
构造如下config.properties文件properties代码
userDao.class=com.spring.dao.UserDao
属性文件中的"userDao"名称即是Bean的别名设定,.class用于指定类来源。
然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("config.properties"));
BeanFactory factory = (BeanFactory)reg;
UserDao userDao = (UserDao)factory.getBean("userDao");
(二)利用java.util.Properties读取属性文件
1. String str=File.separator;
InputStream path=this.getServletContext().getResourceAsStream(str+"WEB-INF"+str+"classes"+str+"password.properties");
//InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("password.properties");
/*File filepath=new File(this.getServletContext().getRealPath(str+"WEB-INF"+str+"classes")+str+"password.properties");
InputStream path=new FileInputStream(filepath);*/
Properties pros = new Properties();
try {
pros.load(path);
} catch (IOException ex) {
//System.out.println("file is not exist");
errorMessage="资源文件不存在";
}
System.out.println("username:"+p.getProperty("username")+",password:"+p.getProperty("password"));
2. import org.springframework.core.io.ClassPathResource;
ClassPathResource cr = new ClassPathResource("password.properties");//会重新加载spring框架
Properties pros = new Properties();
try {
pros.load(cr.getInputStream());
} catch (IOException ex) {
//System.out.println("file is not exist");
errorMessage="资源文件不存在";
}
相关文章推荐
- Java Properties 类读取配置文件信息
- Java Properties 类读取配置文件信息
- 使用java读取配置文件(ini、properties)
- java 读取properties配置文件内容乱码 --日文乱码对应方法
- Java Properties 类读取配置文件信息,Java properties文件的操作(zz)
- Java Properties 类读取配置文件信息
- All about JAVA 读取配置文件的方法 (这里是config.properties)
- Java Properties 类读取配置文件信息
- Java Properties 类读取配置文件信息
- Java读取properties配置文件
- java读取.properties配置文件 的几种方式
- Java Properties 类读取配置文件信息
- Java Properties 类读取配置文件信息,Java properties文件的操作(zz)
- Java Properties 类读取配置文件信息
- Java Properties 类读取配置文件信息
- java 读取 properties 配置文件 备忘
- 利用Java的Properties 类读取配置文件信息
- java 读取properties配置文件
- java读取properties配置文件
- Java Properties 类读取配置文件信息