@Lazy(false)
@Service
public class BcmSettings extends BaseSettings {
/**
* 合作机构编码
*/
public static final String CORG_CODE = "bcm";
public BcmSettings() throws IOException {
super(CORG_CODE);
}
}
public class BaseSettings {
private static Logger logger =Logger.getLogger(BaseSettings.class);
private String confRootPath;
private Properties properties;
private String corgName;
private String confCorgPath;
private String configFileName;
public BaseSettings(String corgName) throws IOException {
this.corgName = corgName;
File rootFile = Settings.getPrefFolder();
if(rootFile==null){
Settings.setPrefFolder("src"+File.separator+"test"+File.separator+"resources"+File.separator);
}
confRootPath = Settings.getPrefFolder().getAbsolutePath()+File.separator;
confCorgPath = confRootPath+corgName+File.separator;
configFileName = corgName+".properties";
}
public String getConfRootPath(){
return confRootPath;
}
public Object get(String key) {
return properties.get(key);
}
public String getString(String key) {
return (String) properties.get(key);
}
@PostConstruct
public void init () {
File settingsFile = new File(confCorgPath+configFileName);
if(settingsFile.exists()){
properties = new Properties();
InputStream inputStream = null;
try{
inputStream = new FileInputStream(settingsFile);
properties.load(inputStream);
}catch (Exception e){
logger.error("主框架配置加载初始化异常",e);
}finally {
if(inputStream!=null){
try {
inputStream.close();
}catch (Exception e){
logger.error("主框架配置加载流关闭异常",e);
}
}
}
}
}
}
调用
@Autowired
BcmSettings bcmSettings;
bcmSettings.getString("配置文件的key名")