繁体   English   中英

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

[英]Spring profile is ignored when reading properties from application.yml

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

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

    context.register(SomeConfig.class);
    context.refresh();
}

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

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

(我从这里复制了 YamlPropertyLoaderFactory 类)

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

spring:
  profiles:
    active: p1

---

spring:
   profiles: p1

file: file1.txt

---

spring:
   profiles: p2

file: file2.txt

在某些 bean 中,我正在使用@Value读取file属性。

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

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

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

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

file: file1.txt

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

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

context.getEnvironment().setActiveProfiles("p1");

您引用的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);
    }
}

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

IE

spring:
  profiles:
    active: p1

---

我认为在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);
    }
}

或者,当您在 yml 文件中有活动配置文件名称时,您可以使用 null 调用YamlPropertySourceLoader().load以获取 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);
    }
}

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

如果您在 yml 文件中有 spring.profiles.active 属性,您就可以使用较新版本的YamlPropertySourceLoader执行以下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);
    }

}

@pcoates 我尝试使用较新版本的 YamlPropertySourceLoader 仍然无法加载配置文件。

总是在以下条件下失败,源返回 2 个没有源“spring.profiles.active”的元素,

if (checkSource.containsProperty("spring.profiles.active"))

下面是我的 yaml 和 yaml 属性加载器的配置

  profiles: dev
  excelPath :  /data/excel
  excecutionPath: http://localhost:8080/server/execute
  memberIP: localhost
  profilerPortNum: 3031

--- 

spring:
  profiles: uat
  excelPath :  /data/uat/excel
  excecutionPath: http://localhost:8080/server/execute
  memberIP: localhost
  profilerPortNum: 3032```

@Configuration
@PropertySource(value = "classpath:application.yml", factory = YamlPropertyLoaderFactory.class)
public class ReadYamlProperties {
}


要仅为特定配置文件设置属性,正确的缩进是:

spring:
   profiles: p1
   file: file1.txt

在上述情况下,您可以使用${spring.file} EL 访问file1.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM