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
- No bean named BookServiceImpl availableBeanAI
- No bean named 'cacheManager' availablej 異常解決BeanAI
- 自己動手實現springboot執行時執行java原始碼(執行時編譯、載入、註冊bean、呼叫)Spring BootJava原始碼編譯Bean
- SpringBoot 學習之 No bean named ‘xxx’ availableSpring BootBeanAI
- No bean named ''XXX“ availableBeanAI
- 使用@Service註解出現No bean named 'xxxx' available]錯誤BeanAI
- Spring中出現:No bean named 'XXX' available問題解決SpringBeanAI
- spring getBean(String) No bean named '' is definedSpringBean
- 死磕Spring之IoC篇 - @Bean 等註解的實現原理SpringBean
- Spring配置使用註解注入beanSpringBean
- spring使用註解注入bean理解SpringBean
- SpringBoot基礎篇Bean之條件注入之註解使用Spring BootBean
- Caused by:.NoSuchBeanDefinitionException: No bean named 'userInfoServiceImpl' availableBeanExceptionAI
- No bean named 'xxx' is defined錯誤,原因及解決方案Bean
- SpringBoot基礎篇Bean之動態註冊Spring BootBean
- [請教] 會話Bean呼叫實體Bean時產生的Conection沒有關閉的異常會話Bean
- Spring中用註解建立bean例項SpringBean
- spring註解@lazy,bean懶載入SpringBean
- spring註解開發(一)Bean注入SpringBean
- 3_基於註解管理Bean物件Bean物件
- 報錯No bean named ' is defined的原因和解決方案Bean
- springboot bean的迴圈依賴實現 原始碼分析Spring BootBean原始碼
- 【Spring 註解】@Configuration和@BeanSpringBean
- 怎麼樣在JBUILDERX中實現會話BEAN訪問實體BEAN??UI會話Bean
- SpringMVC莫名其妙出現No bean named 'cacheManager' is defined錯誤SpringMVCBean
- No Bean named 'Xxx' available 異常報錯BeanAI
- 模型Bean:Model Bean模型Bean
- 一個Session Bean如何去呼叫另外一個Session Bean裡的方法?SessionBean
- Spring框架系列(二)之Bean的註解管理Spring框架Bean
- Spring的Bean生命週期中@PostConstruct註解SpringBeanStruct
- 使用Idea構建springmvc框架,出現no bean named 'cacheManager' is defined 錯誤IdeaSpringMVC框架Bean
- 使用Idea構建springmvc框架,出現no bean named ‘cacheManager’ is defined 錯誤。IdeaSpringMVC框架Bean
- spring中如何向一個單例bean中注入非單例beanSpring單例Bean
- spring中No bean named xxx available錯誤SpringBeanAI
- spring與mybatis中注入了卻總提示no bean namedSpringMyBatisBean
- SpringBoot自動裝配原理之Configuration以及@Bean註解的使用Spring BootBean
- SpringBoot中根據屬性動態註冊Spring BeanSpring BootBean