簡體   English   中英

是否有無法解決的循環參考

[英]Is there an unresolvable circular reference

當我使用springboot啟動應用程序時,會發生異常。 我對此一無所知。

@Bean
@ConfigurationProperties(prefix="master.datasource")
public DataSource master() {
  return new org.apache.tomcat.jdbc.pool.DataSource();
}

@Bean
@ConfigurationProperties(prefix = "slave1.datasource")
public DataSource slave1() {
  return new org.apache.tomcat.jdbc.pool.DataSource();
}

@Bean
public DynamicDataSource dataSource() {
  DynamicDataSource dataSource = new DynamicDataSource();
  dataSource.setMaster(master());
  List<DataSource> slaves = new ArrayList<DataSource>();
  slaves.add(slave1());
  dataSource.setSlaves(slaves);
  return dataSource;
}

這是DynamicDataSource類的結構

public class DynamicDataSource extends AbstractRoutingDataSource {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private AtomicInteger counter = new AtomicInteger();
    private DataSource master;
    private List<DataSource> slaves;

造成原因:

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:347)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:220)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:356)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1066)
        at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.init(DataSourceInitializer.java:69)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:354)
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:305)
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)

嘗試關閉Spring Boot的DataSources自動配置。 將以下內容添加到您的主@Configuration類中:

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2025 STACKOOM.COM