gpt4 book ai didi

java - Spring Boot使用bean内部配置

转载 作者:行者123 更新时间:2023-11-30 01:46:26 25 4
gpt4 key购买 nike

@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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com