惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
V
Visual Studio Blog
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
L
LangChain Blog
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
P
Proofpoint News Feed
博客园_首页
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Check Point Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - ㄓㄤㄑㄧㄤ

sdkman同时存在多个jdk的方式 com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Sat Apr 20 09:55:48 CST 2024 $elemMatch使用 python部署在后台执行 nginx流量复制将请求同时发送到正式和测试环境 一个注解优雅的实现 接口数据脱敏 centOS7 开放/移除指定IP访问指定端口 mac 系统升级到12.4后 mvn 命令 报错:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? mac Zip 常用命令 Mac下php服务部署 更新setuptools和pip时报错Command "python setup.py egg_info" failed with error code 1 in 解决 CentOS 7 执行 firewall-cmd 防火墙命令遇到的 ModuleNotFoundError: No module named ‘gi’ 最新Centos7安装python3并与python2共存 springcloud2.0以上版本_eureka控制台显示_找不到${spring.cloud.client.ipAddress}_没有显示成IP地址 RestTemplate 统一添加 Header 删除centos7中自带有python2.7 Git 常用命令,持续更新 python操作mongodb查询出现Object of type 'ObjectId' is not JSON serializable解决方法 sqlserver按年、季度、月份、周统计订单销量
SpringBoot获取当前运行环境三种方式
ㄓㄤㄑㄧㄤ · 2022-08-25 · via 博客园 - ㄓㄤㄑㄧㄤ

综合现有方案,有三种:

注解直接获取
配置Configuration
实现ApplicationContextAware
1、注解直接获取

@Value("${spring.profiles.active}")
private String env;

2、配置Configuration

@Configuration
public class ProfileConfig {

@Autowired
private ApplicationContext context;

public String getActiveProfile() {
return context.getEnvironment().getActiveProfiles()[0];
}
}

3、实现ApplicationContextAware

@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
if (SpringUtils.applicationContext == null) {
SpringUtils.applicationContext = applicationContext;
}
}

public static ApplicationContext getApplicationContext() {
return applicationContext;
}


public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}


public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}

public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}

/**
* 获取当前环境
*/
public static String getActiveProfile() {
return context.getEnvironment().getActiveProfiles()[0];
}
}

-----------------------------------
SpringBoot获取当前运行环境三种方式
https://blog.51cto.com/u_3664660/3212705