BeanCurrentlyInCreationException sqlSessionFactory Requested bean is currently in creation: Is there an unresolvable circular reference?

Springboot项目集成h2 databse遇到的异常

 

1.异常现象

Springboot集成h2 database,h2配置如下

复制代码
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:~/test
spring.datasource.username=sa
spring.datasource.password=sa
spring.datasource.schema=classpath:db/*.sql
spring.datasource.initialization-mode=always
spring.datasource.continue-on-error=false

spring.h2.console.path=/h2-console
spring.h2.console.enabled=true
复制代码

当spring.datasource.schema配置脚本时,就会抛出异常,具体异常信息为:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sqlSessionFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?

如果不配置spring.datasource.schema的值,也就不会初始化数据库脚本,也不会抛异常。

 

2.问题解决

由于能力不足,在网上到处搜索,搜索了整整一天终于解决了。这个异常的原因看名字就能大致猜出来:Bean对象当前正在创建异常。

由于公司用的RPC框架的注解的类都有这个异常抛出,普通@Service类都可以正常注入Bean。所以可以断定是公司RPC框架注解的问题,不能在Bean完全创建好后再注入导致的问题。

解决办法就是在注入的属性上添加@Lazy注解,表示暂时不注入对象,等到调用该属性(接口)时再注入Bean。类似下方代码

复制代码
@公司RPC
@Service
public class UserService{

    @Lazy
    @Autowired
    private UserMapper userMapper;


    public User getById(String id){

         return userMapper.getById(id);
    }

}        
复制代码

 

posted @   追极  阅读(1358)  评论(0编辑  收藏  举报
编辑推荐:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示