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

推荐订阅源

D
Docker
Apple Machine Learning Research
Apple Machine Learning Research
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
腾讯CDC
B
Blog RSS Feed
H
Help Net Security
J
Java Code Geeks
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
博客园_首页
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
博客园 - Franky
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 叶小钗
Martin Fowler
Martin Fowler

博客园 - ㄓㄤㄑㄧㄤ

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