spring启动错误Singleton bean creation not allowed while the singletons of this factory are indestruction

 更新时间:2023年07月12日 11:30:50   作者:西木风落  
本文主要介绍了spring启动错误Singleton bean creation not allowed while the singletons of this factory are indestruction,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、问题描述

最近在使用线程池做spring的任务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 'jade.datasourceFactory': 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 com.xiaomi.mifi.scf.accountcenter.service.gateway.SapPayDetailService.getSapPayDetailList(SapPayDetailService.java:45)
    at com.xiaomi.mifi.scf.accountcenter.task.reconcile.SapReconcile.sapPaydetailClean(SapReconcile.java:70)
    at com.xiaomi.mifi.scf.accountcenter.task.reconcile.SapReconcile.lambda$reconcile$0(SapReconcile.java:58)
    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。为什么会发生这个问题呢?

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

  @Test
    public void testReconcile(){
        sapReconcile.reconcile("123",2);
    }
 public TRResponse reconcile(String taskId, int type) {
        log.info("当日对账处理|taskId:{}, type:{}, startTime:{}", taskId, type, DateUtil.getNowStr());
        List<Long> times = getReconcileTime(type);
        executor.execute(() -> sapPaydetailClean(times.get(0),times.get(1)));
        executor.execute(() -> paydetailClean(times.get(0),times.get(1)));
        log.info("当日对账处理|endTime:{}", DateUtil.getNowStr());
        return new TRResponse().setCode(ResponseCodeEnum.SUCCESS.getCode());
    }

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

三、解决方案

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

 @Test
    public void test2(){
        sapReconcile.reconcile("123",2);
        while (sapReconcile.executor.getActiveCount() > 0){
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

3.2 利用屏障等同步工具,等待线程执行完成后再退出主线程。

到此这篇关于spring启动错误Singleton bean creation not allowed while the singletons of this factory are indestruction的文章就介绍到这了,更多相关spring启动错误内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java异常处理throws完成异常抛出详解

    java异常处理throws完成异常抛出详解

    这篇文章主要介绍了java异常处理中throws完成异常抛出示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家学有所得多多进步
    2021-10-10
  • Java常用线程池原理及使用方法解析

    Java常用线程池原理及使用方法解析

    这篇文章主要介绍了Java常用线程池原理及使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Java使用jacob将微软office中word、excel、ppt转成pdf

    Java使用jacob将微软office中word、excel、ppt转成pdf

    这篇文章主要为大家详细介绍了Java使用jacob将微软office中word、excel、ppt转成pdf,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • 如何在Intellij中安装LeetCode刷题插件方便Java刷题

    如何在Intellij中安装LeetCode刷题插件方便Java刷题

    这篇文章主要介绍了如何在Intellij中安装LeetCode刷题插件方便Java刷题,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • mybatis实现批量插入并返回主键(xml和注解两种方法)

    mybatis实现批量插入并返回主键(xml和注解两种方法)

    这篇文章主要介绍了mybatis实现批量插入并返回主键(xml和注解两种方法),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • java jvm两种存储区的类型知识点讲解

    java jvm两种存储区的类型知识点讲解

    在本篇文章里小编给大家整理的是一篇关于java jvm两种存储区的类型知识点讲解内容,有兴趣的朋友们可以学习下。
    2021-03-03
  • 关于Java错误提示之找不到或无法加载主类的问题及正确处理方法

    关于Java错误提示之找不到或无法加载主类的问题及正确处理方法

    当我们在初学Java的是时候,类文件中是不设定包名(package)的,这种情况下注意classpath,基本上没有问题, 本文主要说明classpath和系统环境变量PATH都没问题的情况下出错原因和正确处理方法,感兴趣的朋友一起看看吧
    2022-01-01
  • JavaScript的基本类型值-String类型

    JavaScript的基本类型值-String类型

    String类型用于表示由零或多个16位Unicode字符组成的字符序列,即字符串。在JavaScript中没有单个的字符型,都是字符串。这篇文章主要介绍了JavaScript的基本类型值String类型,需要的朋友可以参考下
    2017-02-02
  • 关于MyBatis中SqlSessionFactory和SqlSession简解

    关于MyBatis中SqlSessionFactory和SqlSession简解

    这篇文章主要介绍了MyBatis中SqlSessionFactory和SqlSession简解,具有很好的参考价值,希望大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • Spring AOP的概念与实现过程详解

    Spring AOP的概念与实现过程详解

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,可通过运行期动态代理实现程序功能的统一维护的一种技术。AOP是 Spring框架中的一个重要内容
    2023-02-02

最新评论