spring啟動(dòng)錯(cuò)誤Singleton bean creation not allowed while the singletons of this factory are indestruction
一、問(wèn)題描述
最近在使用線程池做spring的任務(wù)Test時(shí),啟動(dòng)服務(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!)
具體的錯(cuò)誤信息如下:
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)過(guò)原因分析,是單例的bean在創(chuàng)建的時(shí)候,容器已經(jīng)處于銷毀階段,生命周期不同,不允許再次創(chuàng)建生產(chǎn)Bean。為什么會(huì)發(fā)生這個(gè)問(wèn)題呢?
具體看了代碼,是因?yàn)槲业娜蝿?wù)提交給了線程池,
@Test public void testReconcile(){ sapReconcile.reconcile("123",2); } public TRResponse reconcile(String taskId, int type) { log.info("當(dāng)日對(duì)賬處理|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)日對(duì)賬處理|endTime:{}", DateUtil.getNowStr()); return new TRResponse().setCode(ResponseCodeEnum.SUCCESS.getCode()); }
test主線程運(yùn)行時(shí),啟動(dòng)了線程池,線程池中的任務(wù)會(huì)加載bean,但因?yàn)楫惒皆?,任?wù)提交給線程池后,主線程結(jié)束了,開始銷毀bean容器,而線程池任務(wù)有需要?jiǎng)?chuàng)建出bean,所以出現(xiàn)上述的異常情況。
三、解決方案
3.1 測(cè)試用例中增加線程池的任務(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啟動(dòng)錯(cuò)誤Singleton bean creation not allowed while the singletons of this factory are indestruction的文章就介紹到這了,更多相關(guān)spring啟動(dòng)錯(cuò)誤內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解決Springboot啟動(dòng)報(bào)錯(cuò):類文件具有錯(cuò)誤的版本61.0,應(yīng)為?52.0
- 記一次springboot配置redis項(xiàng)目啟動(dòng)時(shí)的一個(gè)奇怪的錯(cuò)誤
- springboot項(xiàng)目啟動(dòng),但是訪問(wèn)報(bào)404錯(cuò)誤的問(wèn)題
- 解決springboot+activemq啟動(dòng)報(bào)注解錯(cuò)誤的問(wèn)題
- SpringBoot集成SwaggerUi以及啟動(dòng)時(shí)遇到的錯(cuò)誤
- spring boot啟動(dòng)時(shí)mybatis報(bào)循環(huán)依賴的錯(cuò)誤(推薦)
相關(guān)文章
HttpClient 在Java項(xiàng)目中的使用詳解
HttpClient作為訪問(wèn)Http服務(wù)的客戶端訪問(wèn)程序已經(jīng)被廣泛使用,提高了開發(fā)效率,也提高了代碼的健壯性。因此熟練掌握HttpClient是必需的,關(guān)于httpclient感興趣的朋友可以參考本篇文章2015-10-10Maven包沖突導(dǎo)致NoSuchMethodError錯(cuò)誤的解決辦法
web 項(xiàng)目 能正常編譯,運(yùn)行時(shí)也正常啟動(dòng),但執(zhí)行到需要調(diào)用 org.codehaus.jackson 包中的某個(gè)方法時(shí),產(chǎn)生運(yùn)行異常,這篇文章主要介紹了Maven包沖突導(dǎo)致NoSuchMethodError錯(cuò)誤的解決辦法,需要的朋友可以參考下2024-05-05SpringBoot?Security權(quán)限控制自定義failureHandler實(shí)例
這篇文章主要為大家介紹了SpringBoot?Security權(quán)限控制自定義failureHandler實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式
這篇文章主要介紹了Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java實(shí)現(xiàn)正則匹配 “1234567” 這個(gè)字符串出現(xiàn)四次或四次以上
文章介紹了如何在Java中使用正則表達(dá)式匹配一個(gè)字符串四次或四次以上的出現(xiàn),首先創(chuàng)建正則表達(dá)式,然后使用Pattern和Matcher類進(jìn)行匹配和計(jì)數(shù),通過(guò)示例代碼展示了如何實(shí)現(xiàn)這一功能,并解釋了匹配的整體次數(shù)和精確出現(xiàn)次數(shù)的邏輯,感興趣的朋友一起看看吧2025-02-02Java使用x-www-form-urlencoded發(fā)請(qǐng)求方式
在開發(fā)中經(jīng)常使用JSON格式,但遇到x-www-form-urlencoded格式時(shí),可以通過(guò)重新封裝處理,POSTMan和APIpost工具中對(duì)此編碼的稱呼不同,分別是x-www-form-urlencoded和urlencoded,分享這些經(jīng)驗(yàn)希望對(duì)他人有所幫助2024-09-09關(guān)于線程池異步線程中再次獲取線程池資源的問(wèn)題
這篇文章主要介紹了關(guān)于線程池異步線程中再次獲取線程池資源的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08