springboot 集成@EnableAsync后报错
问题描述:
在集成@EnableAsync后项目无法启动,出现下面的异常信息,在百度一番后说是依赖循环导致的,但未使用时没有出现此问题 (项目中之前已使用@Async注解,但未使用@EnableAsync,所以之前的异步操作并未生效)
o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '***Aspect': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name '***Service': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '***Service':
Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '***Service': Injection of resource dependencies failed; nested
exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '***Service': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name '***Service': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '***Service':
Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '***Service': Injection of resource dependencies failed; nested exception
is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '***Service': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name '***Service': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '***Service': Injection of resource
dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name '***Service': Bean with name '***Service' has been injected into other beans
[***Service,***Service] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager
type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:325)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:847)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)
解决方式: 参考链接
- 注释掉全部@Async后测试是否能正常运行
- 能够正常启动后再进行排查是由哪块代码引起,排除后即可正常运行
- 可以定义异步处理Service类通过回调函数执行操作
同一个类调用另一个@Async的方法无法生效
解决方式:
将方法放到另一个Service再调用或者使用异步处理Service类通过回调函数执行操作
方法中声明@Transactional和@Async注解,异步操作生效(打印当前线程信息可看),但仍需等待线程执行完毕后返回结果(其中在测试过程中加了代码段使其睡眠30s验证是否存在此问题)
问题描述:
在controller调用的service中调用了其他service中的异步方法后,异步操作生效,但仍需等待异步操作线程执行完毕后才能返回结果(此时有将查询的jpa对象传入其他service中的异步方法)
解决方式:
1.可以将查询和修改放在同一个异步方法操作
2.当前jpa环境下,仍使用原来的方式(实参为查询后的持久化对象),需要将当前查询的持久化对象移除管理.