欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring啟動錯誤Singleton bean creation not allowed while the singletons of this factory are indestruction

 更新時間:2023年07月12日 11:30:50   作者:西木風(fēng)落  
本文主要介紹了spring啟動錯誤Singleton bean creation not allowed while the singletons of this factory are indestruction,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、問題描述

最近在使用線程池做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)文章

  • HttpClient 在Java項目中的使用詳解

    HttpClient 在Java項目中的使用詳解

    HttpClient作為訪問Http服務(wù)的客戶端訪問程序已經(jīng)被廣泛使用,提高了開發(fā)效率,也提高了代碼的健壯性。因此熟練掌握HttpClient是必需的,關(guān)于httpclient感興趣的朋友可以參考本篇文章
    2015-10-10
  • Maven包沖突導(dǎo)致NoSuchMethodError錯誤的解決辦法

    Maven包沖突導(dǎo)致NoSuchMethodError錯誤的解決辦法

    web 項目 能正常編譯,運行時也正常啟動,但執(zhí)行到需要調(diào)用 org.codehaus.jackson 包中的某個方法時,產(chǎn)生運行異常,這篇文章主要介紹了Maven包沖突導(dǎo)致NoSuchMethodError錯誤的解決辦法,需要的朋友可以參考下
    2024-05-05
  • Java實現(xiàn)FTP上傳與下載功能

    Java實現(xiàn)FTP上傳與下載功能

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)FTP上傳與下載功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • SpringBoot?Security權(quán)限控制自定義failureHandler實例

    SpringBoot?Security權(quán)限控制自定義failureHandler實例

    這篇文章主要為大家介紹了SpringBoot?Security權(quán)限控制自定義failureHandler實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • 基于Spring中各個jar包的作用及依賴(詳解)

    基于Spring中各個jar包的作用及依賴(詳解)

    下面小編就為大家?guī)硪黄赟pring中各個jar包的作用及依賴(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式

    Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式

    這篇文章主要介紹了Spring MVC使用jstl 標(biāo)簽c:forEach 遍歷輸出雙層嵌套List的數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java實現(xiàn)正則匹配 “1234567” 這個字符串出現(xiàn)四次或四次以上

    Java實現(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-02
  • Spring Boot 配置大全(小結(jié))

    Spring Boot 配置大全(小結(jié))

    本篇文章主要介紹了Spring Boot 配置大全(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Java使用x-www-form-urlencoded發(fā)請求方式

    Java使用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
  • 關(guān)于線程池異步線程中再次獲取線程池資源的問題

    關(guān)于線程池異步線程中再次獲取線程池資源的問題

    這篇文章主要介紹了關(guān)于線程池異步線程中再次獲取線程池資源的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08

最新評論