先上我的代码@Component@Slf4jpublicclassCSPDJournalListTask{@AutowiredArticleEntityRepositoryarticleEntityRepository;@Asyncpublicvoidtask2(){Listlist=articleEntityRepository.findAll();System.out.println(list.toString());}}上面是一个异步任务,使用SpringBoot创建,并且已经添加了@EnableAsync注解接下来,我就创建了一个简单地单元测试,如下:@RunWith(SpringRunner.class)@SpringBootTestpublicclassCSPDJournalListProcessorTest{@AutowiredCSPDJournalListTaskcspdJournalListTask;@Testpublicvoidtest(){cspdJournalListTask.task();}}然后,诡异的事情发生了,抛出了如下异常org.springframework.beans.factory.BeanCreationNotAllowedException:Errorcreatingbeanwithname'inMemoryDatabaseShutdownExecutor':Singletonbeancreationnotallowedwhilesingletonsofthisfactoryareindestruction(DonotrequestabeanfromaBeanFactoryinadestroymethodimplementation!)atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:208)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:304)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:515)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:290)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:168)~[spring-tx-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:150)~[spring-tx-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)~[spring-data-jpa-2.0.8.RELEASE.jar:2.0.8.RELEASE]atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)~[spring-data-commons-2.0.8.RELEASE.jar:2.0.8.RELEASE]atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]atcom.sun.proxy.$Proxy89.findAll(UnknownSource)~[na:na]atcom.example.webmagic_demo.article.CSPDJournalListTask.task2(CSPDJournalListTask.java:43)~[classes/:na]atcom.example.webmagic_demo.article.CSPDJournalListTask$$FastClassBySpringCGLIB$$c9b82008.invoke()~[classes/:na]atorg.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]atorg.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)~[spring-aop-5.0.7.RELEASE.jar:5.0.7.RELEASE]atjava.util.concurrent.FutureTask.run(FutureTask.java:266)~[na:1.8.0_171]atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)~[na:1.8.0_171]atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)~[na:1.8.0_171]atjava.lang.Thread.run(Thread.java:748)~[na:1.8.0_171]重点看这句:Singletonbeancreationnotallowedwhilesingletonsofthisfactoryareindestruction(DonotrequestabeanfromaBeanFactoryinadestroymethodimplementation!)好像是说我使用的单实例bean已经关闭了,难道时因为这个异步的原因吗?如果需要在异步任务中这样做,该怎么实现呢?希望大神们看到解答一下,非常感谢!
2 回答
ABOUTYOU
TA贡献1812条经验 获得超5个赞
这里贴出我的代码,一点问题也没有@EnableAsync@SpringBootApplicationpublicclassExample{}@RunWith(SpringRunner.class)@SpringBootTest(classes=Example.class)publicclassTest1{@AutowiredCSPDJournalListTaskcspdJournalListTask;@Testpublicvoidtest(){cspdJournalListTask.task();}}@ComponentpublicclassCSPDJournalListTask{@Asyncpublicvoidtask(){//TODOAuto-generatedmethodstubSystem.out.println("async");}}首先要明确的一点是这个错误肯定不是因为使用了async,BeanCreationNotAllowedException大概率是因为项目配置问题,比如jar之间相互冲突不兼容之类的,建议从最简单的项目配置开始,能跑起来之后再增加配置。
添加回答
举报
0/150
提交
取消