spring&pom两种获取profile的方式

本文介绍如何在Spring Boot应用中通过ApplicationContext和YAML/Properties获取并判断活跃环境(生产/阶段),展示了如何在Service中使用注解获取配置并进行单元测试。
摘要由CSDN通过智能技术生成

一、原理:

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

     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值