您的位置:首页 > 编程语言 > Java开发

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("读取系统配置文件失败");
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: