我有包含以下内容的custom.yaml文件:
refill-interval-millis: 1000
endpoints:
- path: /account/all
rate-limit: 10
- path: /account/create
rate-limit: 20和我的类来读取该文件:
@Component
@PropertySource("classpath:custom.yaml")
@ConfigurationProperties
public class Properties {
private int refillIntervalMillis;
private List<Endpoint> endpoints = new ArrayList<>();
// getters/setters
public static class Endpoint {
private String path;
private int rateLimit;
// getters/setters
}
}因此,当我运行代码时,refillIntervalMillis设置正确,但endpoints列表为空。得不到为什么?
发布于 2019-12-02 18:55:11
你不能直接对YAML文件使用@PropertySource:YAML Shortcomings
你有两个简单的选择:
默认情况下使用application.yml和Spring Boot loads (您可以使用application-xxx.yml和设置@ActiveProfiles(value = "xxx");
手动加载YAML文件:
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
var propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
var yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
yamlPropertiesFactoryBean.setResources(new ClassPathResource("custom.yaml"));
propertySourcesPlaceholderConfigurer.setProperties(yamlPropertiesFactoryBean.getObject());
return propertySourcesPlaceholderConfigurer;
}https://stackoverflow.com/questions/59129461
复制相似问题