一、原理:
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的相關配置截圖