我在属性文件中定义了一个属性:property=true
然后我有SomeClass.java
类 , 其中should创建属性配置 beanonly如果财产property
设置为 true。
这是我的SomeClass
class:
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
,然后是豆子does被创造出来,我看到了它的价值property
is true
.
如果我删除matchIfMissing = true
,将条件保留为@ConditionalOnProperty(name="${property}", havingValue="true")
并更改属性property=true
from true
to false
该 bean 从未被创建(也不为true
也不为false
).
我应该做什么bean 是根据属性有条件创建的?
请加@Configuration
高于你的班级。
组件扫描不包括您的类,为此需要添加一个@Component
或类之上的任何其他继承注释。那么你的@Bean
应该从 spring 创建。
希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)