我有这段扫描 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
}
(我从 here 复制了 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
变量,但我收到一个错误:
Could not resolve placeholder 'file' in value "${file}"
(即使我没有传递任何配置文件,它也应该可以工作,因为 application.yml 的默认配置文件设置为 p1)
如果我从 application.yml
中删除所有配置文件配置,它工作正常:
file: file1.txt
所以,这意味着上下文扫描没有读取配置文件变量。
此外,如果我“以编程方式”设置 Activity 配置文件,它也不会解析属性:
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 文件中的属性集,而不是针对特定配置文件。
即
spring:
profiles:
active: p1
---
我认为在 YamlPropertyLoaderFactory
中获取 Activity 配置文件名称并不容易,尽管您可以尝试类似...
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 文件中有 Activity 配置文件名称时,您可以使用 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 月改回 (YamlPropertySourceLoader blame view in Git repo)。它现在返回一个 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);
}
}
关于java - 从 application.yml 读取属性时忽略 Spring 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53117490/