Profile-specific@在application.yaml中設(shè)置配置文件后,配置類不起作用

我想從application.properties遷移到application.yaml,并使用配置文件和每個配置文件不同的@Configuration-classes。

預(yù)期行為:TestEnvPrinter.printEnv: env = test

實(shí)際行為:DefaultEnvPrinter.printEnv(): env = default, profile = test

為什么Spring只使用DefaultAppConfig而忽略TestAppConfig @Configuration-class?

一些應(yīng)用程序文件:

application.yaml:

spring:
  profiles:
    active: test
app:
  env: default

application-test.yaml:

app:
  env: test

DefaultAppConfig.java:

@ComponentScan("ru.albert_rayanov")
@Configuration
@PropertySource("classpath:application.yaml")
public class DefaultAppConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer result = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
        yamlFactory.setResources(new ClassPathResource("application.yaml"));
        result.setProperties(Objects.requireNonNull(yamlFactory.getObject()));
        return result;
    }
}

TestAppConfig.java:

@Configuration
@Profile("test")
@PropertySources(value = {@PropertySource("classpath:application-test.yaml")})
public class TestAppConfig {
    @Bean
    public EnvPrinter envPrinter() {
        return new TestEnvPrinter();
    }
}

DefaultEnvPrinter.java:

@Component
@Primary
public class DefaultEnvPrinter implements EnvPrinter {
    @Value("${app.env}")
    private String env;

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

    @Override
    public void printEnv() {
        System.out.println("DefaultEnvPrinter.printEnv(): env = " + env + ", profile = " + profile);
    }
}

TestEnvPrinter.java:

@Component
public class TestEnvPrinter implements EnvPrinter {

    @Value("${app.env}")
    private String env;

    @Override
    public void printEnv() {
        System.out.println("TestEnvPrinter.printEnv: env = " + env);
    }
}

ProfileWorker.java:

@Component
public class ProfileWorker {
    private final EnvPrinter envPrinter;

    public ProfileWorker(EnvPrinter envPrinter) {
        this.envPrinter = envPrinter;
    }

    public void doWork() {
        envPrinter.printEnv();
    }
}

Main.java:

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(DefaultAppConfig.class);
        context.getBean(ProfileWorker.class).doWork();
    }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ru.albert_rayanov</groupId>
    <artifactId>spring_1_7_profiles</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.8</version>
        </dependency>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.33</version>
        </dependency>
    </dependencies>
</project>
? 最佳回答:

Spring Framework's @PropertySource沒有out-of-the-box對YAML的支持。它會將您的YAML文件視為.properties文件,因此無法找到預(yù)期的配置。您可以編寫一個自定義的PropertySourceFactory實(shí)現(xiàn)來添加YAML支持,并使用@PropertySourcefactory屬性指定工廠。

正如Baeldung的這篇文章中所描述的,PropertySourceFactory實(shí)現(xiàn)相當(dāng)簡單:

public class YamlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) throws IOException {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(encodedResource.getResource());
        Properties properties = factory.getObject();
        return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
    }

}
主站蜘蛛池模板: 中文字幕一区二区三区在线播放 | 丰满人妻一区二区三区视频53| 色系一区二区三区四区五区 | 久久久国产一区二区三区| 国产精品一级香蕉一区| 久久精品无码一区二区三区| 波霸影院一区二区| 日本一区中文字幕日本一二三区视频 | 中文字幕精品一区二区| 无码少妇一区二区性色AV | 国产精品成人一区二区| 国产主播一区二区三区在线观看| 亚洲一区二区三区四区在线观看| 精品国产不卡一区二区三区| 日韩精品一区二区三区不卡 | 日韩人妻不卡一区二区三区 | 麻豆国产在线不卡一区二区 | 国产无线乱码一区二三区 | 成人免费视频一区二区三区 | 国产精品一区二区av| 亚无码乱人伦一区二区| 色噜噜狠狠一区二区三区果冻| 久久一区二区三区免费播放| 亚洲一区二区成人| 人妻体内射精一区二区三四| 亚洲熟妇av一区| 亚洲AV成人精品一区二区三区| 亚洲AV无码一区二区三区久久精品 | 精品视频一区在线观看| 午夜无码视频一区二区三区| 精品视频一区二区三区| 三上悠亚国产精品一区| 日韩视频一区二区三区| 无码人妻精品一区二区| 中文字幕AV无码一区二区三区| 国产精品久久久久一区二区三区| 蜜臀AV无码一区二区三区 | 性无码免费一区二区三区在线| 久久国产精品一区免费下载| 国产精品久久一区二区三区| 国产人妖在线观看一区二区 |