springboot註解實現非同步呼叫時no bean of type TaskExecutor and no bean named 'taskExecutor' either
在使用springboot 註解@Async 實現非同步呼叫時。
啟動類:
@EnableAsync//啟動非同步
public class JtaAtomikosApp {
public static void main(String[] args){
SpringApplication.run(JtaAtomikosApp.class,args);
}
}
controller類:
@Autowired
private TestYiBu testYiBu;
@ResponseBody
@RequestMapping("/sendMsg")
public String sendMsg(){
System.out.println("test 非同步 1");
testYiBu.sendMsg();//非同步呼叫方法
System.out.println("test 非同步 2");
return "test";
}
呼叫的非同步類
package com.boot.other;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component//注入到SpringBoot容器
public class TestYiBu {
@Async//非同步載入標誌
public String sendMsg(){
System.out.println("test 非同步 3");
for (int i=1; i<=3 ;i++){
System.out.println("i=" + i);
}
System.out.println("test 非同步 4");
return "success";
}
}
結果輸出如下:
如圖所示,雖然實現了非同步的呼叫,但是出現了異常:
No task executor bean found for async processing: no bean of type TaskExecutor and no bean named ‘taskExecutor’ either
解決方法:
修改啟動類如下:
@EnableAsync//啟動非同步
public class JtaAtomikosApp {
public static void main(String[] args){
SpringApplication.run(JtaAtomikosApp.class,args);
}
@Bean(name = "threadPoolTaskExecutor")
public Executor threadPoolTaskExecutor() {
return new ThreadPoolTaskExecutor();
}
}
結果如圖:
其他方法:
解決方法參照:https://www.jb51.net/article/137259.htm
相關文章
- SpringBoot(14)—註解裝配BeanSpring BootBean
- SpringBoot 學習之 No bean named ‘xxx’ availableSpring BootBeanAI
- 使用@Service註解出現No bean named 'xxxx' available]錯誤BeanAI
- @Bean 註解Bean
- No bean named ''XXX“ availableBeanAI
- No bean named BookServiceImpl availableBeanAI
- Spring中出現:No bean named 'XXX' available問題解決SpringBeanAI
- No bean named 'cacheManager' availablej 異常解決BeanAI
- 自己動手實現springboot執行時執行java原始碼(執行時編譯、載入、註冊bean、呼叫)Spring BootJava原始碼編譯Bean
- Caused by:.NoSuchBeanDefinitionException: No bean named 'userInfoServiceImpl' availableBeanExceptionAI
- spring getBean(String) No bean named '' is definedSpringBean
- 【Spring 註解】@Configuration和@BeanSpringBean
- @Configuration, @Bean 註解的作用Bean
- Spring:錯誤No bean named 'xxxxxxx' availableSpringBeanAI
- SpringBoot基礎篇Bean之條件注入之註解使用Spring BootBean
- No bean named 'xxx' is defined錯誤,原因及解決方案Bean
- 死磕Spring之IoC篇 - @Bean 等註解的實現原理SpringBean
- SpringBoot基礎篇Bean之動態註冊Spring BootBean
- No Bean named 'Xxx' available 異常報錯BeanAI
- spring中No bean named xxx available錯誤SpringBeanAI
- SpringBoot自動裝配原理之Configuration以及@Bean註解的使用Spring BootBean
- spring註解@lazy,bean懶載入SpringBean
- spring註解開發(一)Bean注入SpringBean
- Spring中用註解建立bean例項SpringBean
- Maven專案報錯:No bean named 'xxxxxx' availableMavenBeanAI
- springboot bean的迴圈依賴實現 原始碼分析Spring BootBean原始碼
- 3_基於註解管理Bean物件Bean物件
- Spring的Bean生命週期中@PostConstruct註解SpringBeanStruct
- 3. 基於註解方式管理BeanBean
- SpringBoot中根據屬性動態註冊Spring BeanSpring BootBean
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'aaaa' availableSpringFrameworkBeanExceptionAI
- idea maven搭建spring報錯:No bean named 'xxx' availableIdeaMavenSpringBeanAI
- springboot使用jpa啟動報錯consider defining a bean of type '*.*.**Repository' in your configuration....Spring BootIDEBean
- 使用Idea構建springmvc框架,出現no bean named ‘cacheManager’ is defined 錯誤。IdeaSpringMVC框架Bean
- 【SpringBoot】@Configration與@Bean的使用Spring BootBean
- Springboot中的Bean作用域Spring BootBean
- spring-mybatis整合:NoSuchBeanDefinitionException: No bean named 'xxx.class' availableSpringMyBatisBeanExceptionAI
- spring中如何向一個單例bean中注入非單例beanSpring單例Bean