为了账号安全,请及时绑定邮箱和手机立即绑定

Spring bean 的创建问题?

Spring bean 的创建问题?

浮云间 2019-05-13 13:29:10
先上我的代码@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
@SpringBootApplication
publicclassExample{
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes=Example.class)
publicclassTest1{
@Autowired
CSPDJournalListTaskcspdJournalListTask;
@Test
publicvoidtest(){
cspdJournalListTask.task();
}
}
@Component
publicclassCSPDJournalListTask{
@Async
publicvoidtask(){
//TODOAuto-generatedmethodstub
System.out.println("async");
}
}
首先要明确的一点是这个错误肯定不是因为使用了async,BeanCreationNotAllowedException大概率是因为项目配置问题,比如jar之间相互冲突不兼容之类的,建议从最简单的项目配置开始,能跑起来之后再增加配置。
                            
查看完整回答
反对 回复 2019-05-13
  • 2 回答
  • 0 关注
  • 552 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信