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


spring&pom两种获取profile的方式的更多相关文章
- 两种获取connectionString的方式
两种获取connectionString的方式 1. public static string connectionString = ConfigurationManager.ConnectionSt ...
- Hibernate中两种获取Session的方式
转自:https://www.jb51.net/article/130309.htm Session:是应用程序与数据库之间的一个会话,是hibernate运作的中心,持久层操作的基础.对象的生命周期 ...
- Spring两种实现AOP的方式
有两种实现AOP的方式:xml配置文件的方式和注解的形式 我们知道通知Advice是指对拦截到的方法做什么事,可以细分为 前置通知:方法执行之前执行的行为. 后置通知:方法执行之后执行的行为. 异常通 ...
- struts2和spring的两种整合方式
首先,来看看如何让Spring 来管理Action. 在struts.xml中加入 <constant name="struts.objectFactory" value=& ...
- 流式思想概述和两种获取Stream流的方式
流式思想概述 整体来看,流式思想类似于工厂车间的生产流水线 当需要对多个元素进行操作(特别是多步操作)的时候,考虑到性能及便利性,我们应该首先拼好一个模型步骤方案,然后再按照方法去执行他 这张图中展示 ...
- Spring中三种配置Bean的方式
Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...
- Request三种获取数据的方式
今天在做ajax请求后台代码时,发现ajax的方法都对,但就是请求不了后台代码,后来在同事帮助下才发现前台定义了两个相同参数导致请求出错. 下面记录一下request三种获取数据的方式: 1. Req ...
- flask框架--设置配置文件的几种方式 与Flask两种配置路由的方式
设置配置文件的几种方式 ==========方式一:============ app.config['SESSION_COOKIE_NAME'] = 'session_lvning' #这种方式要把所 ...
- OC中两种单例实现方式
OC中两种单例实现方式 写在前面 前两天探索了一下C++ 的单例,领悟深刻了许多.今天来看看OC中的单例又是怎么回事.查看相关资料,发现在OC中一般有两种实现单例的方式,一种方式是跟C++ 中类似的常 ...
随机推荐
- 史上最全的音视频SDK包分享给大家
史上最全的音视频SDK包分享给大家 概述一下SDK功能: 项目 详情视频通信 支持多种分辨率的视频通信语音通信 提供语音通信,可支持高清宽带语音动态创建房间 可以根据需要,随时创建房间H5 支持 ...
- 1-RocketMq 学习 中文文档(一)
原文:https://blog.csdn.net/weixin_40533111/article/details/84451096 1.基本概念及优势 rocketmq是一个基于发布订阅队列模型的消息 ...
- Python笔记:装饰器
装饰器 1.特点:装饰器的作用就是为已存在的对象添加额外的功能,特点在于不用改变原先的代码即可扩展功能: 2.使用:装饰器其实也是一个函数,加上@符号后放在另一个函数“头上”就实现了装饰 ...
- 设计模式之(八)组合模式(COMPOSITE)
初始印象 在开发中存在很多整体和部分的关系,这个方式最大的体现就是树形结构.组合模式就是为了更好地解决这类业务场景的问题.先看下组合模式的定义: 将对象组合成树形结构以表示“整体—部分”的层次关系.组 ...
- 【开发笔记】- Grails框架定义一个不是数据库字段得属性
实体类 class Book{ String name String author // myfiled 我不想他在数据库中生成book表的字段 String myfield } 添加声明 class ...
- HTML 统一资源定位器
URL 也被称为网址. URL 可以由单词组成,比如 “w3school.com.cn”,或者是因特网协议(IP)地址:192.168.1.253.大多数人在网上冲浪时,会键入网址的域名,因为名称比数 ...
- 微信小程序开发--flex详细解读
一.结构:flex布局 是由一个大的容器加上多个子元素组成. <view class="container"> <view </view> <v ...
- Centos7允许使用密码登录
现在使用云主机比较多,所以一般都是使用秘钥登录,当做一个集群的时候需要几台机器之间免密登录时,就需要修改他的配置文件了,刚做运维那会儿,很熟练,现在忘得差不多了,特此记录一下,下次又这个需求时就不 ...
- c# MVC5(一) 初步认识以及新建mvc
一:MVC5初始 1:广义MVC(Model--View-Controller): V是界面 : M是数据和逻辑 : C是控制,把M和V链接起来: 是程序设计模式,一种设计理念,可以有效的分离界面和业 ...
- CAS 集群部署
业务场景 单点登录服务器如果压力过大的情况,那么可以使用集群分担压力,但是cas 默认不支持session同步. 所以可以需要做session同步,可以使用j2cache 实现session同步.另外 ...