读取属性配置文件
2014-10-30 11:09
435 查看
读取properties配置文件
一:利用java.util.Properties读取
package com.ailk.common.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 配置文件
* Title: mail-box <br>
* Description: <br>
* Date: 2013-6-4 <br>
* Copyright (c) 2013 AILK <br>
*
* @author supeng
*/
public class PropertiesUtil {
private static Properties props = null;
// private transient static Log log = LogFactory.getLog(PropertiesUtil.class);
/**
* 根据文件名读取配置文件
* @param fileName
* @return
* @throws Exception
* @author supeng
*/
public static Properties getProperties(String fileName) {
Properties props = new Properties();
try {
InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
props.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return props;
}
synchronized public static String getConfiguration(String properties,String configName){
try {
props = PropertiesUtil.getProperties(properties);
} catch (Exception e) {
e.printStackTrace();
// log.error(e);
}
return props.getProperty(configName);
}
public static void main(String[] args) {
Properties p = PropertiesUtil.getProperties("ssoservice.properties");
System.out.println(p.getProperty("sso.socket.timeout"));
}
}
二:利用spring读取properties 文件
利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件 //bean节点配置的class类
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");
System.out.println(helloBean.getHelloWorld());
一:利用java.util.Properties读取
package com.ailk.common.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 配置文件
* Title: mail-box <br>
* Description: <br>
* Date: 2013-6-4 <br>
* Copyright (c) 2013 AILK <br>
*
* @author supeng
*/
public class PropertiesUtil {
private static Properties props = null;
// private transient static Log log = LogFactory.getLog(PropertiesUtil.class);
/**
* 根据文件名读取配置文件
* @param fileName
* @return
* @throws Exception
* @author supeng
*/
public static Properties getProperties(String fileName) {
Properties props = new Properties();
try {
InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
props.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return props;
}
synchronized public static String getConfiguration(String properties,String configName){
try {
props = PropertiesUtil.getProperties(properties);
} catch (Exception e) {
e.printStackTrace();
// log.error(e);
}
return props.getProperty(configName);
}
public static void main(String[] args) {
Properties p = PropertiesUtil.getProperties("ssoservice.properties");
System.out.println(p.getProperty("sso.socket.timeout"));
}
}
二:利用spring读取properties 文件
利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件 //bean节点配置的class类
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");
System.out.println(helloBean.getHelloWorld());
相关文章推荐
- 编写一个文件,读取 src 下面day12包 下面的my.properties 文件,文件内容如下, className=day12.User 结合 IO、反射知识,完成 配置文件中对象的创建和方法调用。并编写方法,列举出 Student 类中所有的属性、
- php读取ini配置文件属性
- springMvc 之 【javaBean读取属性配置文件中的值】---非注解方式
- Spring配置文件中读取properties文件的属性
- java读取属性配置(properties)文件
- 使用java程序读取配置文件中的相关属性值-asp.net关注
- 读取 jpa 的配置文件.添加自己的属性.
- 依据不同的操作系统读取配置文件/java读取属性文件代码
- JAVA 读取属性配置文件
- 基于maven管理的项目中spring+mybatis配置数据源读取不到properties属性文件的问题
- java中经常用到的读写配置文件的信息properties属性的写入,读取例子
- 根据不同的操作系统读取配置文件/java读取属性文件代码
- 读取 jpa 的配置文件.添加自己的属性.
- java读取属性配置文件工具类
- java中经常用到的读写配置文件的信息properties属性的写入,读取例子
- Linux读取属性配置文件注意事项
- java读取和设置配置文件中的属性参数
- java读取配置文件属性
- java web 读取配置文件属性
- 解决Spring配置文件无法读取properties属性问题