gpt4 book ai didi

java - BeanCurrentlyInCreationException Spring Boot

转载 作者:搜寻专家 更新时间:2023-11-01 03:00:25 33 4
gpt4 key购买 nike

我该如何解决这个异常?

没有 Spring Boot 一切正常。

所以问题出在“应用”部分,对吗?

捕获异常

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>

最佳答案

Spring Boot 有 Database initialization与您的 DataSource 配置冲突的功能。请参见 DataSourceInitializer 类。它试图在创建之前获取数据源。

要修复您的应用,您可以

  1. 使用属性禁用初始化器

    spring.datasource.initialize=false
  2. 排除数据源自动配置,因为您有自己的配置

    @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

关于java - BeanCurrentlyInCreationException Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36341883/

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