BeanCurrentlyInCreationException Spring Boot

时间:2016-03-31 19:19:17

标签: java spring spring-boot jetty

如何修复此异常?

没有Spring Boot,一切正常。

所以问题出现在“App”部分,我是对的吗?

抓到异常

BeanCurrentlyInCreationException: Error creating bean with name 'pooledConnectionFactory': 
Requested bean is currently in creation: Is there an unresolvable circular reference?

应用

@ComponentScan
@EnableAutoConfiguration
@ImportResource("classpath:contexts/bean-locations.xml")
public class MyApp{
public static void main(String[] args) throws Exception {
    new SpringApplication(MyApp.class).run(args);
}
}

通过导入使用Bean

<import resource="classpath*:contexts/database/datasource/base-data-source.xml"/>
<import resource="classpath*:contexts/database/datasource/pooled-data-source.xml"/>

导入的第一个文件:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.springframework.org/schema/beans"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<bean id="baseDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${connection.driverClassName}"/>
    <property name="username" value="${connection.userName}"/>
    <property name="password" value="${connection.password}"/>
    <property name="url" value="${connection.url}"/>
</bean>

导入的第二个文件:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.springframework.org/schema/beans"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<bean id="pool" class="org.apache.commons.pool.impl.GenericObjectPool">
    <property name="minEvictableIdleTimeMillis"><value>300000</value></property>
    <property name="timeBetweenEvictionRunsMillis"><value>60000</value></property>
</bean>

<bean id="dataSourceConnectionFactory" class="org.apache.commons.dbcp.DataSourceConnectionFactory">
    <constructor-arg><ref bean="baseDataSource"/></constructor-arg>
</bean>

<bean id="pooledConnectionFactory" class="org.apache.commons.dbcp.PoolableConnectionFactory">
    <constructor-arg index="0"><ref bean="dataSourceConnectionFactory"/></constructor-arg>
    <constructor-arg index="1"><ref bean="pool"/></constructor-arg>
    <constructor-arg index="2"><null/></constructor-arg>
    <constructor-arg index="3"><null/></constructor-arg>
    <constructor-arg index="4"><value>false</value></constructor-arg>
    <constructor-arg index="5"><value>true</value></constructor-arg>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.PoolingDataSource"
      depends-on="pooledConnectionFactory">
    <constructor-arg><ref bean="pool"/></constructor-arg>
</bean>

1 个答案:

答案 0 :(得分:3)

Spring Boot具有Database initialization功能,与您的DataSource配置冲突。见DataSourceInitializer课程。它尝试在创建之前获取数据源。

要修复您的应用程序,您可以

  1. 使用属性

    禁用初始化程序
    spring.datasource.initialize=false
    
  2. 排除数据源自动配置,因为您有自己的配置

    @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})