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

一、问题描述

利用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。为什么会发生这个问题呢?

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

    @Test
    public void sendNoReadMsg() {
        ftpCarService.read();
    }
    @Async
    @Override
    public 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、 测试时去掉异步。

引用中提到的异常信息 "Singleton bean creation not allowed while the singletons of this factory are in destruction" 是由于在销毁方法实现中请求了BeanFactorybean,导致无法创建单例bean。这个问题在SpringBoot启动会出现,并且可能与版本有关。中提到的可能原因是版本问题。从Springbean学习开始到MyBatis Plus,有会遇到这个问题。可能是由于sqlSessionFactorybean的创建失败或其他类的bean创建失败。出现这个问题,要检查版本是否兼容。中提到的可能原因是代码问题。可能出现的错误是 "Error creating bean with name 'sqlSessionFactory'",但是进一步检查发现具体的类的bean创建失败。或者出现了SQL语句错误。这可能是由于字段名与关键字冲突导致的。在表的列名(对应类中的字段名)中不要使用关键字,需要将其更改为其他名称。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Singleton bean creation not allowed while singletons of this factory are in destru](https://blog.csdn.net/CMEguagua/article/details/107867332)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Error creating bean with name ‘sqlSessionFactory‘此类问题原因总结](https://blog.csdn.net/VanGotoBilibili/article/details/112702857)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值