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

2023-08-06 0 4,735

目录

一、问题描述

最近在使用线程池做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 利用屏障等同步工具,等待线程执行完成后再退出主线程。

资源下载此资源下载价格为1小猪币,终身VIP免费,请先
由于本站资源来源于互联网,以研究交流为目的,所有仅供大家参考、学习,不存在任何商业目的与商业用途,如资源存在BUG以及其他任何问题,请自行解决,本站不提供技术服务! 由于资源为虚拟可复制性,下载后不予退积分和退款,谢谢您的支持!如遇到失效或错误的下载链接请联系客服QQ:442469558

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 spring启动错误Singleton bean creation not allowed while the singletons of this factory are indestruction http://www.20zxx.cn/806232/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务