sam*_*333 4 java security spring annotations spring-boot
我在属性文件中定义了一个属性: property=true
然后我有SomeClass.javaclass ,它应该只在属性property设置为 true 时创建一个属性配置 bean 。
这是我的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);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在matchIfMissing = true里面添加@ConditionalOnProperty,只是为了试验并看看 的值是什么property,那么 bean确实被创建,我看到 的值property是true。
如果我删除了matchIfMissing = true,留下的条件作为@ConditionalOnProperty(name="${property}", havingValue="true")和属性更改property=true来自true于false豆,从未创建(既不true也不对false)。
根据属性有条件地创建 bean,我应该怎么做?
请@Configuration在您的班级上方添加。
组件扫描不包括您的类,为此需要@Component在您的类之上添加一个或任何其他继承的注释。那么你@Bean应该从春天创建。
希望这可以帮助。