如何在@ConditionalOnProperty中引用String Bean
我需要根据从数据库获取的其他一些 bean 值来初始化配置, 我们如何在@ConditionalOnProperty值属性中引用bean?
或者有没有其他方法可以达到同样的效果。
// Bean that will query database
@Bean
public String checkAndCreate()
{
// Logic to Query DB
return "true";
}
想要在值中引用 bean checkAndCreate
@ConditionalOnPropery(value = "", havingValue = "true")
@Configuration
public class MyConfiguration
{
// Some configuration Code
}
I need to initialize a Configuration based on some other bean value that I am fetching from database,
how can we refer the bean in @ConditionalOnProperty value attribute ?
Or is there any other way to achieve the same.
// Bean that will query database
@Bean
public String checkAndCreate()
{
// Logic to Query DB
return "true";
}
Want to Refer the bean checkAndCreate in value
@ConditionalOnPropery(value = "", havingValue = "true")
@Configuration
public class MyConfiguration
{
// Some configuration Code
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用
@ConditionalOnProperty
,而是使用@Conditional
和自定义条件。ConditionContext
应该允许您访问此 bean。您可能需要使用 @DependsOn 来确保提供字符串 bean 的类已经初始化。Don't use
@ConditionalOnProperty
, use@Conditional
with a custom condition instead. TheConditionContext
should give you access to this bean. You may need to use@DependsOn
on to ensure that the class providing the string bean is already initialized.