[英]Spring boot use bean inside configuration
@Configuration
@AutoConfigureOrder()
@ComponentScan("scanPath")
public class AutoConfiguration {
@Autowired
private Factory factory;
@Bean("factory")
@ConditionalOnProperty(prefix = "application.middleware", name = "enabled", havingValue = "true")
public Factory getFactory() {
return new Factory();
}
@Bean("binding")
@DependsOn("factory")
public Binding getMarketingDataSource() {
return factory.getInstance("bindingName");
}
}
I'd like to: 1. init bean factory (could be null if no value found) 2. autowired and used by bean binding 我想:1.初始化bean工厂(如果找不到值,可以为null)2.自动绑定并由bean绑定使用
But right now I get exception 但是现在我得到了例外
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'factory': Requested bean is currently in creation: Is there an unresolvable circular reference?
So what I want is using factory in binding, how to make it? 所以我想要在绑定中使用工厂,如何制作? Thanks! 谢谢!
Use getFactory()
to get the Bean (and remove @Autowired
): 使用getFactory()
获取Bean(并删除@Autowired
):
public Binding getMarketingDataSource() {
return getFactory().getInstance("bindingName");
Wire the factory in the signature, now Spring sees that binding needs factory and sets it for you: 将工厂连接到签名中,现在Spring看到绑定需要工厂并为您设置它:
@Bean("binding")
public Binding getMarketingDataSource(Factory factory) {
return factory.getInstance("bindingName");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.