当我使用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)
答案 0 :(得分:6)
尝试关闭Spring Boot的DataSources自动配置。将以下内容添加到主@Configuration
类:
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})