当将 @ConfigurationProperties 与 @PropertySource(value = "myconfig.yml") 一起使用时,Springboot 不会将我的属性序列化为对象
如果我将相同的配置放入 application.yml 并删除 @PropertySource(value = "myconfig.yml"),那么它就可以工作
---
testPrefix.simpleProperty: my.property.haha
testPrefix.complexProperties:
-
firstName: 'Clark'
lastName: 'Ken'
-
firstName: 'Roger'
lastName: 'Federer'
@Configuration
@ConfigurationProperties(prefix = "testPrefix")
@PropertySource(value = "testConfigFile.yml")
public class MyTestProperties {
private String simpleProperty;
private List<Person> complexProperties;
getters
setters
@SpringBootApplication
public class App implements CommandLineRunner {
MyTestProperties myProperties;
@Autowired
public App(MyTestProperties properties) {
this.properties = properties;
}
public static void main(String[] args) {
SpringApplication app = new SpringApplication((App.class));
app.run(args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(myProperties.getSimpleProperty());
myProperties.getComplexProperties.stream.forEach(System.out::println));
}
}
输出:
my.property.haha
最佳答案
据我所知,YAML 属性无法使用 @PropertySource
加载。我会查一下,因为我不确定问题是否同时得到解决。
[编辑] 显然,它 hasn't已修复:
YAML files cannot be loaded by using the @PropertySource annotation. So, in the case that you need to load values that way, you need to use a properties file.
关于java - 在Springboot中,使用@ConfigurationProperties和@PropertySource时如何将属性序列化为对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56367777/