Spring afterPropertiesSet方法读取系统配置文件实例

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


}


}

你可能感兴趣的:(spring)