spring啟動錯誤Singleton bean creation not allowed while the singletons of this factory are indestruction
一、問題描述
最近在使用線程池做spring的任務(wù)Test時,啟動服務(wù)拋出異常: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)
二、原因分析
經(jīng)過原因分析,是單例的bean在創(chuàng)建的時候,容器已經(jīng)處于銷毀階段,生命周期不同,不允許再次創(chuàng)建生產(chǎn)Bean。為什么會發(fā)生這個問題呢?
具體看了代碼,是因為我的任務(wù)提交給了線程池,
@Test public void testReconcile(){ sapReconcile.reconcile("123",2); } public TRResponse reconcile(String taskId, int type) { log.info("當(dāng)日對賬處理|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("當(dāng)日對賬處理|endTime:{}", DateUtil.getNowStr()); return new TRResponse().setCode(ResponseCodeEnum.SUCCESS.getCode()); }
test主線程運行時,啟動了線程池,線程池中的任務(wù)會加載bean,但因為異步原因,任務(wù)提交給線程池后,主線程結(jié)束了,開始銷毀bean容器,而線程池任務(wù)有需要創(chuàng)建出bean,所以出現(xiàn)上述的異常情況。
三、解決方案
3.1 測試用例中增加線程池的任務(wù)判斷,如果有線程池任務(wù)未完成,當(dāng)前主線程阻塞。
@Test public void test2(){ sapReconcile.reconcile("123",2); while (sapReconcile.executor.getActiveCount() > 0){ try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } }
3.2 利用屏障等同步工具,等待線程執(zhí)行完成后再退出主線程。
到此這篇關(guān)于spring啟動錯誤Singleton bean creation not allowed while the singletons of this factory are indestruction的文章就介紹到這了,更多相關(guān)spring啟動錯誤內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven包沖突導(dǎo)致NoSuchMethodError錯誤的解決辦法
web 項目 能正常編譯,運行時也正常啟動,但執(zhí)行到需要調(diào)用 org.codehaus.jackson 包中的某個方法時,產(chǎn)生運行異常,這篇文章主要介紹了Maven包沖突導(dǎo)致NoSuchMethodError錯誤的解決辦法,需要的朋友可以參考下2024-05-05SpringBoot?Security權(quán)限控制自定義failureHandler實例
這篇文章主要為大家介紹了SpringBoot?Security權(quán)限控制自定義failureHandler實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式
這篇文章主要介紹了Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Java實現(xiàn)正則匹配 “1234567” 這個字符串出現(xiàn)四次或四次以上
文章介紹了如何在Java中使用正則表達(dá)式匹配一個字符串四次或四次以上的出現(xiàn),首先創(chuàng)建正則表達(dá)式,然后使用Pattern和Matcher類進(jìn)行匹配和計數(shù),通過示例代碼展示了如何實現(xiàn)這一功能,并解釋了匹配的整體次數(shù)和精確出現(xiàn)次數(shù)的邏輯,感興趣的朋友一起看看吧2025-02-02Java使用x-www-form-urlencoded發(fā)請求方式
在開發(fā)中經(jīng)常使用JSON格式,但遇到x-www-form-urlencoded格式時,可以通過重新封裝處理,POSTMan和APIpost工具中對此編碼的稱呼不同,分別是x-www-form-urlencoded和urlencoded,分享這些經(jīng)驗希望對他人有所幫助2024-09-09