zoukankan      html  css  js  c++  java
  • spring&pom两种获取profile的方式

    一、原理:

      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的相关配置截图

      

         

  • 相关阅读:
    【2012百度之星资格赛】G:聊天就是Repeat
    使用testNG进行并发性能测试
    关于处理高并发,防止库存超卖的问题
    关于java内存溢出的异常处理
    关于微服务架构
    关于MySQL绿色版的安装
    G点营销看来也不是@z营销的专利啊
    matlab+中文字体设计,有搞头没有? 有搞头
    字王字型系列命名草案
    国外字体设计师也是蛮重视数学的
  • 原文地址:https://www.cnblogs.com/zhenyuyaodidiao/p/12029479.html
Copyright © 2011-2022 走看看