SpringBoot项目意外出现 循环依赖和注入的对象意外是Null的问题 Requested bean is currently in creation: Is there an unresolvable circular reference? 或 nested exception is java.lang.NullPointerException
1.Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mybatisPlusConfig': Invocation of init method failed; nested exception is java.lang.NullPointerException
2.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?
以上2个情况均是指 迫切的注入依赖 导致
出现上面2种情况,无非就是启动时通过注入的Mapper或Service对象去初始化数据;前提是项目是真的没有循环依赖的情况下..
我这边的情况是 @Service接口和使用均没有手动声明循环依赖;排查了半天,只能妥协了;从代码里没有找到循环依赖的地方,想来可能是CGlib和JDK代理的时候导致的问题吧
通过 setMethod进行注入依然失败: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?
@Autowired
public void setSysDatabaseService(ISysDatabaseService sysDatabaseService)
第一种解决方法就是:使用以下语句在项目启动后进行初始化数据,执行的时机可以通过 org.springframework.boot.SpringApplication#run(java.lang.String...) 看到

@Bean
// public ApplicationRunner initIgnoreTenantTables(ISysDatabaseTenantService sysDatabaseTenantService){
// return args -> {
// // 启动时初始化无租户的列表
// List<String> notTenantTableNameList = sysDatabaseTenantService.findAllNotTenantTable().stream().map(SysDatabaseTenant::getTableName).collect(Collectors.toList());
// ignoreTenantTables.addAll(notTenantTableNameList);
// };
// }

经实验,加 @Lazy注解无法应用到这个场景里;无法有效的解决此问题
我出现这个问题的位置是:
@Configuration
// @Configuration(proxyBeanMethods = false)
class AConfig { // Service是正常的结构,没有任何问题;在Controller层调用均没有问题;出现这个的原因,可能就是 急切 加载导致的 private XXXService xxxService; @PostConstruct // 无效
public void init(){
xxxService.myMethod1();
} @Autowired
@Lazy
public void setXXXService(){ // 无效
xxxService.myMethod1();
} @Bean // 有效
public ApplicationRunner initIgnoreTenantTables(ISysDatabaseTenantService sysDatabaseTenantService){
return args -> {
xxxService.myMethod1();
};
} }
抛异常的位置:
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject

org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveDependency ;这里进行抛异常的,具体位置忘了,有大能知道原因,麻烦告知下

SpringBoot项目意外出现 循环依赖和注入的对象意外是Null的问题 Requested bean is currently in creation: Is there an unresolvable circular reference? 或 nested exception is java.lang.NullPointerException的更多相关文章
- Spirng 循环依赖报错:Requested bean is currently in creation: Is there an unresolvable circular reference?
1:前言 最近在项目中遇到了一次循环依赖报错的问题,虽然解决的很快,但是有些不明白的地方,特此记录. 在此我把 bean 的结构和 注入方式单独拎出来进行演示 1.1:报错提示 1.2:错误日志 Ex ...
- springboot 异常: Requested bean is currently in creation: Is there an unresolvable circular reference?
2018-07-31 11:56:18.812 WARN 10316 --- [ main] ConfigServletWebServerApplicationContext : Exception ...
- ?--Porg.springframework.beans.MethodInvocationException: Property 'username' threw exception; nested exception is java.lang.NullPointerException
使用BoneCP作为连接池,在启动Tomcat报出以下异常: 一月 02, 2016 2:12:17 下午 org.apache.tomcat.util.digester.SetPropertiesR ...
- HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException
HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException type ...
- 14- Servlet.service() for servlet [mvc-dispatcher] in context with path [/collegeservice] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root caus
有的service没有依赖注入:
- error:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
问题:调用的方法在一个接口类中,但我并没有注入那个被调用的类 解决:在UserEntity前加上@Autowired @Controller public class MainController { ...
- SpringBoot项目启动org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException解决方法
将Pom文件中的SpringBoot版本调低即可. 我的是调成了2.5.6
- 报错:严重: Servlet.service() for servlet [springmvc] in context with path [ ] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
解决:service类或dao类需要@Autowired
- SSM框架报HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException错
如下图 一番排查之后发现原来是server层写漏注释了 粗心大意,一天内出现两次写漏注释,SSM框架有意思.
随机推荐
- AngularJS 打算开始学习
作为朋友推荐给我的一款框架 个人感觉还不错 打算开始学习
- Visual Studio 2017-2019版本创建C#项目时没有创建网站这一选项?
通过了解以后发现Visual Studio 2017之后的版本在新建选项中已经不再有这一选择项了. 解决办法: 1.在创建新项目的面板滑倒最下面,---> 安装多个人工具和功能 2.这时已经打开 ...
- [故障]ceph存储池权限修改错误,导致存储池的业务hang住
描述: 记录一次重大事故:根据IaaS资源业务要求,需要增加某些功能,所以要修改部署代码.修改后重推部署代码,检查发现没有什么异常. 但是一段时间后就收到用户的报障反馈,接连一个电话.2个电话.3个电 ...
- stegsolve.jar压缩包打开和使用方法
1.stegsolve.jar下载 下载地址:http://www.caesum.com/handbook/Stegsolve.jar 2.stegsolve.jar打开方法 (1)需要下载java并 ...
- JabRef:将bibtex格式的参考文献导入EndNote的转换软件
我在写小论文的时候,一直用的都是Overleaf在线latex编辑应用: https://www.overleaf.com/login 这个我感觉还是蛮好用的.只需要从期刊或者出版社的官网下载到lat ...
- C#特性(属性)Attribute
先明确一个概念: 元数据..NET中元数据是指程序集中的命名空间.类.方法.属性等信息.这些信息是可以通过Reflection读取出来的. 再来看个例子: #define BUG //#define ...
- linux系统挂载磁盘
linux系统挂载磁盘 ## 将硬盘挂载到/mnt/disk1下 # 检查磁盘状态,确认分区 fdisk -l # 格式化磁盘(分区) mkfs.ext4 /dev/sda #根据机器上的命名 以sd ...
- Activity的创建及生命周期
- Laravel 自定命令以及生成文件
以创建service层为例子 1.执行命令 php artisan make:command ServiceMakeCommand 2.在app\Console\Commands 下就会多出一个 Se ...
- Control Flow in Tensorflow TF中的控制流解析
写在前面 本文翻译自Tensorflow团队的文章Tensorflow Control Flow Implementation,部分内容加入了笔者自己的理解,如有不妥之处还望各位指教. 目录 概览 控 ...