一、问题描述

利用Test测试任务时时,启动服务抛出异常:Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

具体的错误信息如下:

Exception in thread "pool-3-thread-1" Exception in thread "pool-3-thread-2" org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'XXXXXX': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:216)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:534)at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:523)at net.paoding.rose.jade.context.spring.SpringDataSourceFactoryDelegate.getDataSource(SpringDataSourceFactoryDelegate.java:38)at net.paoding.rose.jade.dataaccess.DefaultDataAccessFactory.getDataAccess(DefaultDataAccessFactory.java:46)at net.paoding.rose.jade.statement.SelectQuerier.execute(SelectQuerier.java:60)at net.paoding.rose.jade.statement.SelectQuerier.execute(SelectQuerier.java:56)at net.paoding.rose.jade.statement.JdbcStatement.execute(JdbcStatement.java:112)at net.paoding.rose.jade.context.JadeInvocationHandler.invoke(JadeInvocationHandler.java:143)at com.sun.proxy.$Proxy53.getSapPayDetailList(Unknown Source)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)at java.lang.Thread.run(Thread.java:748)

二、原因分析

经过原因分析,是单例的bean在创建的时候,容器已经处于销毁阶段,生命周期不同,不允许再次创建生产Bean。为什么会发生这个问题呢?

具体看了代码,是因为我的任务提交给了线程池,

    @Testpublic void sendNoReadMsg() {ftpCarService.read();}
    @Async@Overridepublic void read() {ExecutorService cachedThreadPool = Executors.newFixedThreadPool(20);long startTime = System.currentTimeMillis();log.info("<<<<<<<执行ftp:xxx.xxx.xxx.xxx的读写!>>>>>>>>" + Thread.currentThread().getName());FtpUtils ftp = new FtpUtils(ip,userName,userPwd,port,path);List<String> str = null;...................

test主线程运行时,启动了线程池,线程池中的任务会加载bean,但因为异步原因,任务提交给线程池后,主线程结束了,开始销毁bean容器,而线程池任务有需要创建出bean,所以出现上述的异常情况。

三、解决方案

1、 测试用例中增加线程池的任务判断,如果有线程池任务未完成,当前主线程阻塞。

2、 测试时去掉异步。

spring使用Test测试时报错:Singleton bean creation not allowed while singletons of this factory are in destru相关推荐

  1. 解决spring coud打包报Singleton bean creation not allowed while singletons of this factory are in destruct

    解决spring coud打包报Singleton bean creation not allowed while singletons of this factory are in destruct ...

  2. Singleton bean creation not allowed while singletons of this factory are in destruction (Do not requ

    错误详情: org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with na ...

  3. Error creating bean with name 'eurekaClientConfigBean': Singleton bean creation not allowed!

    做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 今天发现一个错误,简单记录一下,运行一个项目一直启动不了,发现控制台报错了. 首先说明一下这是一个Spring boot ...

  4. Junit测试时报错No tests found matching

    Junit测试时报错No tests found matching 错误提示: 错误原因及解决方法: 其它原因: 错误提示: java.lang.Exception: No tests found m ...

  5. jmeter http并发测试时报错

    jmeter http并发测试时报错 错误信息如下:jmeter Response code: Non HTTP response code: java.net.URISyntaxException ...

  6. spring集成kafka运行时报错:Failed to construct kafka producer] with root cause

    spring集成kafka运行时报错:Failed to construct kafka producer] with root cause org.apache.kafka.common.Kafka ...

  7. 在Spring中注册组件时报错:The fully qualified name of the bean's class, except if it serves only as a parent d

    问题:我在注册组件时报错The fully qualified name of the bean's class, except if it serves only as a parent defin ...

  8. springcloud项目启动时报错 The bean ‘dataSource‘, defined in class path resource

    项目启动时报错,如下: Error starting ApplicationContext. To display the conditions report re-run your applicat ...

  9. spring boot: 构建项目时报错Not a managed type

    今天在学习使用Spring Data JPA的时候,将bean和JpaRepository放在了不同的package中,导致无法构建项目,报以下错误: org.springframework.bean ...

最新文章

  1. slf4j+log4j打印日志,控制台无日志输出
  2. NYOJ 35 表达式求值
  3. python list操作说明
  4. GDCM:改变dcm文件的PrivateTag测试程序
  5. Java异常处理和常用类
  6. .NET Framewrok 4.0新增类库
  7. C4D素材背景模板|广泛应用电商场景和展览的运用
  8. 一款可定制的外国jQuery图表插件jqplot
  9. Material Design(二)--色彩样式
  10. nodejs服务器部署unexpected token{
  11. linux如何伪装ip,如何伪装你的IP(二)
  12. excel 宏录制,宏代码查看
  13. java通过调用鼠标模拟自动添加微信好友
  14. 用python自动制作ppt第一讲——了解11种默认布局
  15. [转贴]迅雷十年反思
  16. 分布式认知工业互联网赋能工业企业数字化转型
  17. Github中常见单词使用意思
  18. shell脚本编程学习笔记3(xdl)——Bash变量的运算与测试
  19. 来自北京大学NOIP金牌选手yxc的常用代码模板2,图灵学院和享学课堂
  20. Windows Server 2012 R2桌面化详细设置图解

热门文章

  1. matlab布尔代数运算法则,布尔代数运算法则及基本公式
  2. BAT/头条/小米/京东/滴滴/美团...互联网大厂员工一年究竟能挣多少钱?
  3. NX二次开发-调NX的dxfdwg.exe转换器导出dxf,dwg
  4. iOS之多线程---Runloop和多线程
  5. java中变量 关键字_基于java的voliate关键字详解
  6. 一对一直播app源码实现短信验证码发送功能的详细步骤
  7. VS中 std::cin.get()无法暂停在命令窗
  8. 台式电脑计算机为什么没有EF盘,打开电脑,D、E、F盘不见了,怎么处理?
  9. web2.0图形设计风格指南
  10. 幼儿linux教程,新增20多种小游戏 - 专为3岁儿童设计的Ubuntu 10.10应用体验_Linux教程_Linux公社-Linux系统门户网站...