gpt4 book ai didi

java - BeanCurrentlyInCreationException 请求的 bean 当前正在创建 : Is there an unresolvable circular reference?

转载 作者:行者123 更新时间:2023-12-01 21:31:31 28 4
gpt4 key购买 nike

在开发springboot项目的时候,就遇到了上面的错误。奇怪的是,项目在IDEA中运行良好,却报出运行为jar的异常。更奇怪的是,我的同事竟然可以打包一个无异常的jar!所以我认为是我自己的电脑环境有问题。

这是我电脑的工具版本:Maven 3.6.2Java版本:1.8.0_181

这是 pom 文件:

<parent>
<groupId>com.tct.bigdata</groupId>
<artifactId>teye-analyze</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>report</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>report</name>
<description>Report picture and chart to web page</description>


<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>com.tct.bigdata</groupId>
<artifactId>data-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<!--Redis 数据库读写依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!--spring2.0集成redis所需common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>

<!--fastJson支持-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>

</dependencies>

<build>
<finalName>bigdata-report</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<!-- 释放打包时过滤 -->
<!--<configuration>
<layout>ZIP</layout>
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>-->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
</plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.tct.bigdata.report.ReportApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>-->
</plugins>
</build>

详细错误信息:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hibernateConfiguration': Unsatisfied dependency expressed through field 'readDataSource01'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readDataSource01' defined in class path resource [com/tct/bigdata/report/config/DataSourceConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?

这是我的配置类:

@Configuration
public class DataSourceConfiguration {

private static Logger log = LoggerFactory.getLogger(DataSourceConfiguration.class);

@Value("${mysql.datasource.type}")
private Class<? extends DataSource> dataSourceType;

/**
* 写库 数据源配置
* @return
*/
@Bean(name = "writeDataSource")
@ConfigurationProperties(prefix = "mysql.datasource.write")
public DataSource writeDataSource() {
log.info("-------------------- writeDataSource init ---------------------");
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder = dataSourceBuilder.type(dataSourceType);
DataSource dataSource = dataSourceBuilder.build();

return dataSource;
}

/**
* 有多少个从库就要配置多少个
* @return
*/
@Bean(name = "readDataSource01")
@ConfigurationProperties(prefix = "mysql.datasource.read01")
public DataSource readDataSourceOne() {
log.info("-------------------- read01 DataSourceOne init ---------------------");
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder = dataSourceBuilder.type(dataSourceType);
return dataSourceBuilder.build();
// return DataSourceBuilder.create().type(dataSourceType).build();
}
}








@Configuration
@AutoConfigureAfter(DataSourceConfiguration.class)
public class HibernateConfiguration {

private static Logger log = LoggerFactory.getLogger(HibernateConfiguration.class);

@Value("${mysql.datasource.readSize}")
private String readDataSourceSize;

@Autowired
@Qualifier("writeDataSource")
private DataSource writeDataSource;

@Autowired
@Qualifier("readDataSource01")
private DataSource readDataSource01;

/**
* 把所有数据库都放在路由中
* @return
*/
@Primary
@Bean(name="dataSource")
public AbstractRoutingDataSource roundRobinDataSouceProxy() {

Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
//把所有数据库都放在targetDataSources中,注意key值要和determineCurrentLookupKey()中代码写的一至,
//否则切换数据源时找不到正确的数据源
targetDataSources.put(DataSourceType.write.getType(), writeDataSource);
targetDataSources.put(DataSourceType.read.getType()+"1", readDataSource01);

final int readSize = Integer.parseInt(readDataSourceSize);

//路由类,寻找对应的数据源
AbstractRoutingDataSource proxy = new AbstractRoutingDataSource(){
private AtomicInteger count = new AtomicInteger(0);
/**
* 这是AbstractRoutingDataSource类中的一个抽象方法,
* 而它的返回值是你所要用的数据源dataSource的key值,有了这个key值,
* targetDataSources就从中取出对应的DataSource,如果找不到,就用配置默认的数据源。
*/
@Override
protected Object determineCurrentLookupKey() {
String typeKey = DataSourceContextHolder.getReadOrWrite();

if(typeKey == null){
return DataSourceType.write.getType();
}

if (typeKey.equals(DataSourceType.write.getType())){
return DataSourceType.write.getType();
}

//读库, 简单负载均衡
int number = count.getAndAdd(1);
int lookupKey = number % readSize;
log.info("使用数据库read-"+(lookupKey+1));
return DataSourceType.read.getType()+(lookupKey+1);
}
};

proxy.setDefaultTargetDataSource(writeDataSource);//默认库
proxy.setTargetDataSources(targetDataSources);
return proxy;
}
}

最佳答案

请显示您的配置类。

顺便说一下,maven 中有一些重复项:data-redis 是重复的。另外我怀疑您是否需要 data-jpa 和 data-jdbc,但这取决于您

根据您所描述的内容,由于依赖项重复,问题可能出在您的“真实”类路径(由 jar 中的 maven 构建,而不是 IDEA 构建)

关于java - BeanCurrentlyInCreationException 请求的 bean 当前正在创建 : Is there an unresolvable circular reference?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58813928/

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