一、原理:
1、实现ApplicationContextAware(当一个类实现了ApplicationContextAware这个接口之后,这个类就可以通过setApplicationContext方法获得ApplicationContext中的上下文),获取context。通过方法:context.getEnvironment().getActiveProfiles()获取激活的profile。
2、通过service中成员变量上的注解:@Value("${spring.profiles.active}"),获取yaml中的profile
二、上代码:
通过context获取:
package com.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
@Service
public class SpringContextUtil implements ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger(SpringContextUtil.class);
private static final String PRODUCTION_PROFILE = "production";
private static final String STAGE_PROFILE = "stage";
private static ApplicationContext context = null;
public static <T> T getBean(String beanName) {
return (T) context.getBean(beanName);
}
public static String[] getActiveProfileList() {
return context.getEnvironment().getActiveProfiles();
}
/**
* 判断当前环境是否是线上环境:production或stage
* @return
*/
public static boolean isProfileActived() {
String[] profiles = context.getEnvironment().getActiveProfiles();
if (profiles == null || profiles.length == 0) {
return false;
}
for (String val : profiles) {
logger.info("current profile from context is: {}", val);
if (val.equalsIgnoreCase(PRODUCTION_PROFILE) || val.equalsIgnoreCase(STAGE_PROFILE)) {
return true;
}
}
return false;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.context = applicationContext;
}
}
通过yaml(或properties)文件获取
package com.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class SpringProfileService {
private static final Logger logger = LoggerFactory.getLogger(SpringProfileService.class);
private static final String PRODUCTION_PROFILE = "production";
private static final String STAGE_PROFILE = "stage";
@Value("${spring.profiles.active}")
private String profile;
public boolean isProfileActived() {
logger.info("current profile from yaml is: {}", profile);
if (profile.equalsIgnoreCase(PRODUCTION_PROFILE) || profile.equalsIgnoreCase(STAGE_PROFILE)) {
return true;
}
return false;
}
}
单元测试代码:
package com.**.service;
import com.**.Application;
import com.test.SpringContextUtil;
import com.test.SpringProfileService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = Application.class)
@RunWith(SpringRunner.class)
public class ServiceTest {
private static final Logger logger = LoggerFactory.getLogger(ServiceTest.class);
@Test
public void test1() {
Boolean actived = SpringContextUtil.isProfileActived();
logger.info(actived.toString());
}
@Autowired
SpringProfileService springProfileService;
@Test
public void test2() {
Boolean actived = springProfileService.isProfileActived();
logger.info(actived.toString());
}
}
其他。pom的相关配置截图,yaml的相关配置截图