@Profile使用及SpringBoot获取profile值
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Fmuma/article/details/82787500
之前开发用过 maven 的环境隔离,现在使用springboot的@Profile功能,发现spring体系真的大到我只是学习了皮毛。相比面试问的 IOC,bean的作用域等,突然觉得很可笑。
官方文档关于 Profile 的使用
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-features-profiles
@Profile注解使用范围:
@Configration 和 @Component 注解的类及其方法,其中包括继承了@Component的注解:@Service、@Controller、@Repository等…
@Profile可接受一个或者多个参数,例如:
@Service
@Profile({"prod","default"})
Demo
参考: https://blog.csdn.net/zknxx/article/details/77906096
先看项目架构体系,只需要看展示的类,其他不用管
在这里插入图片描述
application.properties 配置文件
spring.profiles.active=prod,default
service
public interface ProfileService {
String getMsg();
}
service.impl
@Service
@Profile("dev")
public class DevServiceImpl implements ProfileService {
public DevServiceImpl() {
System.out.println("我是开发环境。。。。。");
}
@Override
public String getMsg() {
StringBuilder sb = new StringBuilder();
sb.append("我在开发环境,").append("我只能吃加班餐:大米饭。。。。");
return sb.toString();
}
}
@Service
@Profile({"prod","default"})
public class ProdServiceImpl implements ProfileService {
public ProdServiceImpl() {
System.out.println("我是生产环境。。。。。");
}
@Override
public String getMsg() {
StringBuilder sb = new StringBuilder();
sb.append("我在生产环境,").append("我可以吃鸡鸭鱼牛羊肉。。。。");
return sb.toString();
}
}
controller
@Profile("dev")
@RestController
public class DevController {
@Autowired
private ProfileService profileService;
@RequestMapping(value = "/")
public String dev(){
return "hello-dev\n"+profileService.getMsg();
}
}
@Profile("prod")
@RestController
public class ProdController {
@Autowired
private ProfileService profileService;
@RequestMapping(value = "/")
public String prod(){
return "hello-prod\n"+profileService.getMsg();
}
}
在application.properties 配置文件使用不同的环境会执行不同的方法。
那么问题来了,这个功能使如何实现的?如果让你设计你会怎么做?面试新题,啊哈哈哈哈
获取profile值
参考来源:一下找不到了…囧
使用profile帮我解决了不同环境使用不同配置的问题,那么如果我们需要在代码中获取这个profile的值怎么处理呢?
@Component
public class ProfileUtil implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
}
// 获取当前环境参数 exp: dev,prod,test
public static String getActiveProfile() {
String []profiles = context.getEnvironment().getActiveProfiles();
if( ! ArrayUtils.isEmpty(profiles)){
return profiles[0];
}
return "";
}
}
————————————————
版权声明:本文为CSDN博主「淘气的二进制」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Fmuma/article/details/82787500
@Profile使用及SpringBoot获取profile值的更多相关文章
- Maven的porfile与SpringBoot的profile结合使用详解
使用maven的profile功能,我们可以实现多环境配置文件的动态切换,可参考我的上一篇博客.但随着SpringBoot项目越来越火,越来越多人喜欢用SpringBoot的profile功能 ...
- Spring Boot入门(二):使用Profile实现多环境配置管理&如何获取配置文件值
在上一篇博客Spring Boot入门(一):使用IDEA创建Spring Boot项目并使用yaml配置文件中,我们新建了一个最原始的Spring Boot项目,并使用了更为流行的yaml配置文件. ...
- Spring Boot入门(二):获取配置文件值
本篇博客主要讲解下在Spring Boot中如何获取配置文件的值. 1. 使用yaml配置文件 Spring Boot默认生成的配置文件为application.properties,不过它也支持ya ...
- SpringBoot获取配置文件,就这么简单。
在讲SpringBoot 获取配置文件之前我们需要对SpringBoot 的项目有一个整体的了解,如何创建SpringBoot 项目,项目结构等等知识点,我在这里就不一一讲述了,没有学过的小伙伴可以自 ...
- springboot 获取控制器参数的几种方式
这里介绍springboot 获取控制器参数有四种方式 1.无注解下获取参数 2.使用@RequestParam获取参数 3.传递数组 4.通过URL传递参数 无注解下获取参数无注解下获取参数,需要控 ...
- Maven 之 profile 与Spring boot 的 profile
一.概述 不同的环境(测试环境.开发环境)有不同的配置,目前希望在打包的时候,就直接打出针对不同环境的包(内含有某个环境的配置).Maven本身在 pom.xml 中就提供了 profile 标签进行 ...
- Java--FutureTask原理与使用(FutureTask可以被Thread执行,可以被线程池submit方法执行,并且可以监控线程与获取返回值)
package com; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; i ...
- jquery 获取一组元素的选中项 - 函数、jquery获取复选框值、jquery获取单选按钮值
做表单提交时,如果现在还在用form提交,用户体验很差,所以一般使用ajax提交. 其中需要获取每个表单输入元素的值,获取的时候像文本框这些还好说,Jquery提供了 .val() 方法,获取很方便, ...
- 获取枚举值上的Description特性说明
/// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...
随机推荐
- bzoj5084 hashit 广义SAM+树链的并
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5084 题解 考虑平常对于静态问题,我们应该如何用 SAM 求本质不同的子串个数. 对于一个常规 ...
- django之请求和响应
一:请求 1. 浏览器向服务器传参的几种方式: 方式一:通过正则的形式进行传递,名字为未命名模式 路由的正则形式: from django.conf.urls import url from . im ...
- B2C自营商城的订单设计方案
B2C自营商城的订单设计方案 2018年06月01日 17:19:00 lkx94 阅读数 1640 去年我们的美妆社区APP,上线了自有商城.之后经过多次版本迭代,商城系统的模块已经基本健全,值 ...
- Django的下载和基本指令
1.下载Django pip3 install django #不写版本号的话,默认使下载最新版的django pip3 install django == 2.1.2 #指定 ...
- Jenkins修改升级配置
更换升级配置如下: http://mirror.esuni.jp/jenkins/updates/update-center.json
- git使用技巧总结
一.把github上的代码下载下来 git clone ...... 二.把修改的代码提交上去 git add .... git commit -m "........" git ...
- 删除c++项目中的类
关闭引擎和vs 在工程目录中删除Source文件夹下相应的cpp和h文件 删除工程目录的saved 文件夹和intermediate文件夹 删除工程目录中binaries文件夹内的所有内容 右键工程文 ...
- eclipse 启动 tomcat 报错:Server mylocalhost was unable to start within 45 seconds
这个专门转载一篇博文也是为了讽刺一下自己二逼的程序员职业,哈哈. eclipse启动tomcat服务器报错:Server mylocalhost was unable to start within ...
- UVA10870 Recurrences (矩阵快速幂及构造方法详解)
题意: F(n) = a1 * F(n-1) + a2 * F(n-2)+ ···· + ad * F(n-d). 求给你的n . 很明显这是一道矩阵快速幂的题目. 题解: [Fn-1, Fn-2, ...
- 432D Prefixes and Suffixes
题目大意 给你一个串 对于一个子串如果它既是前缀又是后缀 输出它的长度以及它在原串中一共出现了多少次 分析 对于既是前缀又是后缀的判断和126B相同 然后我们只需要记录每个不同的z[i]出现了多少次 ...