[英]ConfigurationProperties in nested classes
使用以下 yml
app:
a:
prop: aaa
b:
prop: bbb
@Component
public abstract class Common {
@Value("${prop}")
private String prop;
@ConfigurationProperties(prefix = "app.a")
@PropertySource("classpath:app.yml")
@Component
public static class A extends Common {
}
@ConfigurationProperties(prefix = "app.b")
@PropertySource("classpath:app.yml")
@Component
public static class B extends Common {
}
}
但是对于a
或b
,这两个类具有相同的值。
我该如何解决这个问题?
我发现了问题。 简单地。 yml
不适用于PropertySource
。
我仍然想相信我是错的。
我将.yml
文件更改为properties
并尝试使用此方法。
@PropertySource("classpath:/vendor.properties")
@EnableConfigurationProperties
public abstract class Common {
@Value("${prop}")
private String prop;
@ConfigurationProperties(prefix = "app.a")
@Component
public static class A extends Common {
}
@ConfigurationProperties(prefix = "app.b")
@Component
public static class B extends Common {
}
}
它奏效了。
您可以使用配置参数列表:
app:
props:
- key: a
value: aaa
- key: b
value: bbb
并在单独的 bean 中以更复杂的方式检索您的价值:
@ConfigurationProperties(prefix = "app")
public class CommonConfiguration {
List<Prop> props;
//Getters and setters
public Prop retreiveSpecificConfiguration(String className) {
//some kind of logic here
}
public static class Prop {
private String key, value;
//Getters and setters
}
}
将它注入您的 Common 类实现中:
@Autowired
CommonConfiguration config;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.