Spring afterPropertiesSet方法读取系统配置文件实例
2017-08-14 14:35
791 查看
package com.common;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SystemConfig implements InitializingBean {
@Value("${config.properties}")
private String SYSTEM_CONFIG;//系统配置文件名
// 服务查询条件映射
private static Properties sysConfig = null;
/**
* 获取系统配置信息
*
* @param name
* @return
*/
public static String getPropertity(String name) {
return sysConfig.getProperty(name);
}
/**
* 判断系统配置信息中存在
*
* @return
*/
public static boolean isExists(String name) {
return sysConfig.getProperty(name) != null;
}
@Override
public void afterPropertiesSet() throws Exception {
InputStream stream = SystemConfig.class.getClassLoader().getResourceAsStream(SYSTEM_CONFIG);
sysConfig = new Properties();
try {
sysConfig.load(stream);
} catch (IOException e) {
throw new ExceptionInInitializerError("读取系统配置文件失败");
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SystemConfig implements InitializingBean {
@Value("${config.properties}")
private String SYSTEM_CONFIG;//系统配置文件名
// 服务查询条件映射
private static Properties sysConfig = null;
/**
* 获取系统配置信息
*
* @param name
* @return
*/
public static String getPropertity(String name) {
return sysConfig.getProperty(name);
}
/**
* 判断系统配置信息中存在
*
* @return
*/
public static boolean isExists(String name) {
return sysConfig.getProperty(name) != null;
}
@Override
public void afterPropertiesSet() throws Exception {
InputStream stream = SystemConfig.class.getClassLoader().getResourceAsStream(SYSTEM_CONFIG);
sysConfig = new Properties();
try {
sysConfig.load(stream);
} catch (IOException e) {
throw new ExceptionInInitializerError("读取系统配置文件失败");
}
}
}
相关文章推荐
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
- Spring中配置和读取多个Properties文件的方式方法
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
- spring读取properties和xml配置文件的方法
- spring中afterPropertiesSet方法与init-method配置描述
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的区别联系
- spring List,Set,Map,Properties,array的使用配置文件注入实例
- 如何读取.properties文件配置的两种方法
- 如何在spring中读取properties配置文件里面的信息
- 多个Spring配置文件时,读取方法
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- 读取Spring配置文件 main方法里面测试实现功能
- 关于系统读取properties配置文件的路径问题,包括打成jar包的运行文件
- java读取配置文件properties的方法(很多不会用框架s2sh的开发者,喜欢用此方法)
- Java读取properties配置文件时,中文乱码解决方法
- Spring读取自定义properties配置文件
- 配置文件读取的一种方法config.properties
- 通过Spring读取properties配置文件的信息 Spring 读取properties
- spring 读取properties配置文件