《Spring Boot 3实战案例合集》现已囊括超过60篇精选实战文章,并且此合集承诺将永久持续更新,为您带来最前沿的技术资讯与实践经验。欢迎积极订阅,享受不断升级的知识盛宴!订阅用户将特别获赠合集内所有文章的最终版MD文档(详尽学习笔记),以及完整的项目源码,助您在学习道路上畅通无阻。
1. 简介
application.yml指定
spring:
profiles:
active:
- prod
通过命令行参数指定
java -jar app.jar --spring.profiles.active=prod
灵活性 该属性属于全局设置,一旦设置影响的是整个配置加载;这样显然不够灵活,粒度太粗了。 组织结构 更侧重于通过文件命名约定来组织和区分不同环境的配置。
spring.config.activate.on-profile
2. 处理多文档文件
spring:
application:
name: pack-app
---
logging:
pattern:
dateformat: yyyy-MM-dd HH:mm
---
spring:
profiles:
active:
prod
spring.application.name=pack-app
#---
logging.pattern.dataformat=yyyy-MM-dd HH:mm
spring:
profiles:
active:
- test
- live
---
pack.app:
name: dev app
spring.config.activate:
on-profile:
- dev
---
pack.app:
name: test app
spring.config.activate:
on-profile:
- test
---
pack.app:
name: prod app
spring.config.activate:
on-profile:
- prod
private String appName ;
// 输出
test app
spring.config.activate:
on-cloud-platform: xxx
public enum CloudPlatform {
KUBERNETES {
private static final String KUBERNETES_SERVICE_HOST = "KUBERNETES_SERVICE_HOST";
private static final String KUBERNETES_SERVICE_PORT = "KUBERNETES_SERVICE_PORT";
public boolean isDetected(Environment environment) {
if (environment instanceof ConfigurableEnvironment configurableEnvironment) {
return isAutoDetected(configurableEnvironment);
}
return false;
}
private boolean isAutoDetected(ConfigurableEnvironment environment) {
// 获取当前系统环境信息systemEnvironment
PropertySource<?> environmentPropertySource = environment.getPropertySources()
.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
if (environmentPropertySource != null) {
// 判断是否存在上面定义的两个常量值信息
if (environmentPropertySource.containsProperty(KUBERNETES_SERVICE_HOST)
&& environmentPropertySource.containsProperty(KUBERNETES_SERVICE_PORT)) {
return true;
}
// other
}
return false;
}
}
}
---
pack.app:
name: dev app
spring.config.activate:
on-cloud-platform: kubernetes
on-profile:
- dev
---
以上是本篇文章的全部内容,如对你有帮助帮忙点赞+转发+收藏
推荐文章