Java 配置文件组合的Spring boot YAML配置
从Spring引导上的YAML配置: 如果YAML文档包含一个Java 配置文件组合的Spring boot YAML配置,java,spring,spring-boot,configuration,Java,Spring,Spring Boot,Configuration,从Spring引导上的YAML配置: 如果YAML文档包含一个spring.profiles键,那么profiles值(一个以逗号分隔的概要文件列表)将被输入到spring环境.acceptsProfiles()方法中。如果这些配置文件中的任何一个处于活动状态,则该文档将包含在最终合并中 因此,spring.profiles键具有OR逻辑。如果将其设置为test,dev,则当Spring配置文件包含test或dev时,将应用该配置 我想要的是逻辑和逻辑。我有多个机器类型和地区,我想在机器类型和地
spring.profiles
键,那么profiles值(一个以逗号分隔的概要文件列表)将被输入到spring环境.acceptsProfiles()
方法中。如果这些配置文件中的任何一个处于活动状态,则该文档将包含在最终合并中
因此,spring.profiles
键具有OR逻辑。如果将其设置为test,dev
,则当Spring配置文件包含test
或dev时,将应用该配置
我想要的是逻辑和逻辑。我有多个机器类型和地区,我想在机器类型和地区的特定组合上启用一些配置,如欧洲生产
是否可以基于YAML文件中的配置文件组合设置配置?我认为这更适合
应用程序侦听器或环境后处理器:
例如,您的和逻辑可以是:
@Component
public class MyListener {
@EventListener
public void handleContextStart(ApplicationPreparedEvent event) {
ConfigurableEnvironment env = event.getApplicationContext().getEnvironment();
if (env.acceptsProfiles(Profiles.of("test")) && env.acceptsProfiles(Profiles.of("test"))) {
// Do the AND configuration here.
}
}
}
或者,您可以创建自己的@ConfigurationProperties
:
在@PostConstruct
方法中,在那里进行进一步的自定义:
@ConfigurationProperties("myKey")
public class MyProperties implements EnvironmentAware {
private Environment Environment;
private MachineType machineType;
private String region;
@Override
public setEnvironment(Environment environment) {
this.environment = environment;
}
enum MachineType {
MAC_OS,
WINDOWS,
LINUX
}
@PostConstruct
void init() {
if (environment.acceptProfiles(Profiles.of("dev"))) {
// Do some work setting other properties
if (machineType == MachineType.WINDOWS) {
// some other work if it's Windows
}
}
}
}
然后在整个应用程序中使用MyProperties
bean。是的,可以根据YAML中的配置文件组合设置配置
spring:
profiles:
active: "production,Europe"
---
spring:
profiles: production
one: prd one
two: prd two
three: prd three
---
spring:
profiles: Europe
one: EU one
four: EU four
我会给你
one: EU one // <------
two: prd two
three: prd three
four: EU four
one:EU-one//
one: prd one // <------
two: prd two
three: prd three
four: EU four