问题描述
我正在查看此状态:
您实际上尝试混合其行为。
值
不是Spring环境的属性,而是 my-config.values
是。
甚至在<$ c $内部声明c> MyConfig ,例如 @Value( $ {values})
,它不会更改 @ConfigurationProperties
将属性绑定到结构化对象。当然,它不会在Spring环境中创建新属性,这就是 @Value()
寻找解析值表达式的地方。
而解决 $ {values}
的异常。
作为 MyConfig
是组件 @Value
应该是您所需要的:
@Component
公共类MyConfig {
private final List< String>价值观
public MyConfig(@Value( $ {my-config.values})List< String>值){
this.values = ImmutableList.copyOf(values);
}
}
您也可以通过保护设置器来防止可变性进行检查,但这只会在运行时检测到问题:
@ConfigurationProperties( my-config)
public class MyConfig {
private final List< String>价值观
公共列表< String> getValue(){
返回值;
}
public void setValue(List< String> values){
if(this.values!= null){
throw new IllegalArgumentException( ...);
}
this.values = ImmutableList.copyOf(values);
}
}
I was looking at this https://www.baeldung.com/configuration-properties-in-spring-boot and was wondering if it was possible to use constructor injection for these in order to enforce some immutability properties.
For example would it be possible to do this:
@Component
@ConfigurationProperties("my-config")
public class MyConfig {
private final List<String> values;
public MyConfig(@Value("${values}") List<String> values) {
this.values = ImmutableList.copyOf(values);
}
}
And then in my yml config have
my-config.values:
- foo
- bar
But I get this error:
java.lang.IllegalArgumentException: Could not resolve placeholder 'values' in string value "${values}"
The documentation states :
You actually try to mix their behavior.values
is not a property of the Spring environment but my-config.values
is.
Even declared inside MyConfig
such as @Value("${values})"
it doesn't change anything as @ConfigurationProperties
bounds the properties to a structured object. And of course it doesn't create new properties in the Spring environment, that is where @Value()
looks for to resolve the value expression.
Whereas the exception to resolve ${values}
.
As MyConfig
is a component @Value
should be what you need :
@Component
public class MyConfig {
private final List<String> values;
public MyConfig(@Value("${my-config.values}") List<String> values) {
this.values = ImmutableList.copyOf(values);
}
}
You could also prevent the mutability by protecting the setter with a check but this will detect the issue only at runtime :
@ConfigurationProperties("my-config")
public class MyConfig {
private final List<String> values;
public List<String> getValue(){
return values;
}
public void setValue(List<String> values){
if (this.values != null){
throw new IllegalArgumentException("...");
}
this.values = ImmutableList.copyOf(values);
}
}
这篇关于对Spring ConfigurationProperties子类使用构造函数注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!