[英]How to get spring profile name from java
I have a separate spring yaml configuration file for each profile. 每个配置文件都有一个单独的spring yaml配置文件。
I need from a CommandLineRunner
descendant class to read the profile and execute the appropriate script. 我需要从
CommandLineRunner
后代类中读取配置文件并执行适当的脚本。
Is there a way from java to get the environment profile name? 从Java是否有办法获取环境配置文件名称?
I have tried this : 我已经试过了:
environment.getActiveProfiles();
but it only return an empty string array. 但它只返回一个空字符串数组。
environment.getProperty("spring.profiles.active")
to get active profiles. environment.getProperty("spring.profiles.active")
来获取活动配置文件。 This method works fine for me 这种方法对我来说很好
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);
}
I am using both System.getenv and System.getProperty because I have observed that in my local system System.getProperty worked but on my docker container System.getenv() worked. 我同时使用System.getenv和System.getProperty,因为我注意到在我的本地系统中System.getProperty可以工作,但是在我的Docker容器上System.getenv()可以工作。
Also, I am defaulting the profile to "local" profile in case nothing has been set. 另外,如果未进行任何设置,我会将配置文件默认为“本地”配置文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.