@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 ...
随机推荐
- Linux下安装Python,以及环境变量的配置
1.安装环境 centos7 + vmware + xshell 2.安装Python3 2.1下载Python资源包 网址:https://www.python.org/downloads/re ...
- wrapper配置文件详解
参考资料 http://www.tuicool.com/articles/jqMv2q 文件编码,每个配置文件起始位置必须指定该文件的编码格式 encoding=UTF-8 如果包含配置文件出现问题可 ...
- Dubbo学习-6-springboot整合dubbo
1.在前面帖子和工程的基础上,这里使用springboot整合dubbo,首先创建springboot项目: https://start.spring.io/ 进入spring Initializr ...
- K短路模板POJ 2449 Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K Total Submissions:32863 Accepted: 8953 Description &qu ...
- stack2链栈
#include<iostream> using namespace std; template <class Object> class Stack{ private: st ...
- UE4开发PSVR游戏的常见问题
Failed to connect to file server at xxx.xxx.xxx.xxx. RETRYING in 5s解决办法:PS4 Devkit 中 Settings->De ...
- 学习日记7、mvc +easyui datagrid excel上传
1.首先获取datagrid所有行的数据 var rows = $("#List").datagrid("getRows"); 2.进行数据转换转化成JSON格 ...
- 洛谷P1462 通往奥格瑞玛的道路(二分+spfa,二分+Dijkstra)
洛谷P1462 通往奥格瑞玛的道路 二分费用. 用血量花费建图,用单源最短路判断 \(1\) 到 \(n\) 的最短路花费是否小于 \(b\) .二分时需要不断记录合法的 \(mid\) 值. 这里建 ...
- p5471 [NOI2019]弹跳
分析 代码 #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define ...
- E:\Postgresql\pgAdmin4_binaryPath
e Path to the directory containing the EDB Advanced Server utility programs (pg_dump, pg_restore etc ...