从 application.yml 读取属性时忽略 Spring 配置文件

Héc*_*tor 6 java spring

我有这个扫描 Spring 上下文的代码:

public void scan() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

    context.register(SomeConfig.class);
    context.refresh();
}
Run Code Online (Sandbox Code Playgroud)

我需要从application.yml文件中读取属性,所以在SomeConfig课堂上,我有这个:

@Configuration
@PropertySource(value = "classpath:application.yml", factory = YamlPropertyLoaderFactory.class)
public class SomeConfig {
  //some beans
}
Run Code Online (Sandbox Code Playgroud)

(我从这里复制了 YamlPropertyLoaderFactory 类)

application.yml 是一个典型的 Spring Boot 文件,具有一些按配置文件的属性和一个默认配置文件:

spring:
  profiles:
    active: p1

---

spring:
   profiles: p1

file: file1.txt

---

spring:
   profiles: p2

file: file2.txt
Run Code Online (Sandbox Code Playgroud)

在某些 bean 中,我正在file使用@Value.

当我运行我的应用程序时,我正在传递-Dspring.profiles.active=p1变量,但出现错误:

无法解析值“${file}”中的占位符“文件”

(即使我没有传递任何配置文件,它也应该可以工作,因为 application.yml 的默认配置文件设置为 p1)

如果我从 中删除所有配置文件配置application.yml,它工作正常:

file: file1.txt
Run Code Online (Sandbox Code Playgroud)

因此,这意味着上下文扫描未读取配置文件变量。

此外,如果我“以编程方式”设置活动配置文件,它也不会解析属性:

context.getEnvironment().setActiveProfiles("p1");
Run Code Online (Sandbox Code Playgroud)

pco*_*tes 5

YamlPropertyLoaderFactory你指具有下面的代码:

public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        if (resource == null){
            return super.createPropertySource(name, resource);
        }

        return new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource(), null);
    }
}
Run Code Online (Sandbox Code Playgroud)

YamlPropertySourceLoader.load()方法的第三个参数实际上是您想要属性的配置文件名称。由于此示例传入 null,它仅从 yml 文件中返回一组属性,而不是针对特定配置文件。

IE

spring:
  profiles:
    active: p1

---
Run Code Online (Sandbox Code Playgroud)

我认为在YamlPropertyLoaderFactory.

public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        if (resource == null){
            return super.createPropertySource(name, resource);
        }

        String activeProfile = System.getProperty("spring.profiles.active");
        return new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource(), activeProfile);
    }
}
Run Code Online (Sandbox Code Playgroud)

或者因为您在 yml 文件中有活动配置文件名称,您可以YamlPropertySourceLoader().load使用 null调用以获取 spring.profiles.active 属性,然后再次调用它以加载您想要的 yml 文件的实际部分。

public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        if (resource == null){
            return super.createPropertySource(name, resource);
        }
        PropertySource<?> source = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource(), null);
        String activeProfile = source.getProperty("spring.profiles.active");
        return new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource(), activeProfile);
    }
}
Run Code Online (Sandbox Code Playgroud)

YamlPropertySourceLoader已于 2018 年 2 月更改回(Git repo 中的 YamlPropertySourceLoader 责备视图)。它现在返回一个 propertySource 列表,并且在 load 方法上没有第三个参数。

如果您在 yml 文件中有 spring.profiles.active 属性,您就可以使用较新版本的 YamlPropertySourceLoader

public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        if (resource == null){
            return super.createPropertySource(name, resource);
        }
        List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource());
        for (PropertySource<?> checkSource : sources) {
            if (checkSource.containsProperty("spring.profiles.active")) {
                String activeProfile = (String) checkSource.getProperty("spring.profiles.active");
                for (PropertySource<?> source : sources) {
                    if (activeProfile.trim().equals(source.getProperty("spring.profiles"))) {
                        return source; 
                    }
                }
            }
        }
        return sources.get(0);
    }

}
Run Code Online (Sandbox Code Playgroud)