对于每个配置文件,我都有一个单独的spring yaml配置文件。
我需要从CommandLineRunner
子类读取配置文件并执行适当的脚本。
有没有从java获取环境配置文件名称的方法?
我已经尝试过了:
environment.getActiveProfiles();
但它只返回一个空字符串数组。
发布于 2017-09-11 22:45:33
这个方法很适合我。
private static final String SPRING_PROFILES_ACTIVE = "SPRING_PROFILES_ACTIVE";
protected void setSpringProfile(ServletContext servletContext) {
if(null!= System.getenv(SPRING_PROFILES_ACTIVE)){
profile=System.getenv(SPRING_PROFILES_ACTIVE);
}else if(null!= System.getProperty(SPRING_PROFILES_ACTIVE)){
profile=System.getProperty(SPRING_PROFILES_ACTIVE);
}else{
profile="local";
}
log.info("***** Profile set ---> "+ profile);
}
我同时使用System.getenv和System.getProperty,因为我观察到在我的本地系统中,System.getProperty可以工作,但在我的停靠容器上,System.getenv()可以工作。
此外,我将默认配置文件为“本地”配置文件,以防没有设置任何内容。
https://stackoverflow.com/questions/42692645
复制相似问题