我有一个属性文件中定义的属性: property=true
然后,我有SomeClass.java
类, 应创建只有在财产属性配置bean的property
设置为true。
这是我SomeClass
类:
public class SomeClass {
//this is the proerty which I set to true or false
@Value("${property}")
private String property;
//this is the bean which needs to be created conditionally based on the property but at the moment it does not get created
@Bean
@ConditionalOnProperty(name="${property}", havingValue="true")
public PropertyConfiguration propertyConfig() {
// doesnt print anything because the bean does not get created
System.out.print(property);
}
}
如果我添加matchIfMissing = true
里面@ConditionalOnProperty
,只是做实验,看看有什么价值property
,那么这个bean 并获得创建和我看到的价值property
是true
。
如果我删除matchIfMissing = true
,假条件为@ConditionalOnProperty(name="${property}", havingValue="true")
并更改属性property=true
由true
到false
豆,从未创建(既不是true
也不是为false
)。
我应该怎么办ST豆被有条件地创建,基于财产?