集成@EnableAsync後出現問題的記錄

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)

解決方式: 參考鏈接

  1. 註釋掉全部@Async後測試是否能正常運行
  2. 能夠正常啓動後再進行排查是由哪塊代碼引起,排除後即可正常運行
  3. 可以定義異步處理Service類通過回調函數執行操作

同一個類調用另一個@Async的方法無法生效

解決方式:

將方法放到另一個Service再調用或者使用異步處理Service類通過回調函數執行操作

方法中聲明@Transactional和@Async註解,異步操作生效(打印當前線程信息可看),但仍需等待線程執行完畢後返回結果(其中在測試過程中加了代碼段使其睡眠30s驗證是否存在此問題)

問題描述:

在controller調用的service中調用了其他service中的異步方法後,異步操作生效,但仍需等待異步操作線程執行完畢後才能返回結果(此時有將查詢的jpa對象傳入其他service中的異步方法)

解決方式:

1.可以將查詢和修改放在同一個異步方法操作
2.當前jpa環境下,仍使用原來的方式(實參爲查詢後的持久化對象),需要將當前查詢的持久化對象移除管理.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章