@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");
}
}
我愿意: 1. 初始化bean工厂(如果没有找到值可能为null) 2. Autowiring 并通过bean绑定(bind)使用
但现在我遇到了异常(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?
所以我想要的是在绑定(bind)中使用工厂,如何制作呢?谢谢!
最佳答案
使用getFactory()
获取Bean(并删除@Autowired
):
public Binding getMarketingDataSource() {
return getFactory().getInstance("bindingName");
关于java - Spring Boot使用bean内部配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57757714/