spring Boot異步操作報錯誤: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.self.spring.springboot.Jeep' available


我也是最近開始學習Spring Boot,在執行異步操作的時候總是匯報如下的錯誤:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'com.self.spring.springboot.Jeep' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090) at com.self.spring.springboot.App.main(App.java:24)

 我運行的代碼如下:

Jeep類:
import java.util.concurrent.TimeUnit;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class Jeep implements Runnable {
    
  //異步注解 @Async
public void run() { try { for(int i=1;i<=10;i++){ System.out.println("==============" + i); TimeUnit.SECONDS.sleep(1); } } catch (Exception e) { e.printStackTrace(); } } }

main類:

@EnableAutoConfiguration
//使@Async注解生效 @EnableAsync @ComponentScan
public class App { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(App.class, args); System.out.println(context.getBean(TomcatProperties.class)); context.getBean(Jeep.class).run(); System.out.println("===========end==========="); context.close(); } }

錯誤分析:錯誤提示沒有這個 com.self.spring.springboot.Jeep' available,但是已經存在這個類,后來查詢了一會,非異步操作的時候,我們通過context.getBean(類名.class)來調用這個類里面的方法,但是在異步操作的時候,調用Jeep類實現了接口Runnable的方法,我們調用context.getBean(Runnable.class).run(),才會正常運行不報錯。。

其實我也不得解答,如果有哪位高手看到了,請給我留言,已解我心中之謎。。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 springboot 報錯 org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type 'com.example.service.HrService' available: 有沒有大佬出個主意,我找了一天,剛入門springboot springboot啟動報錯:Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zxkj.lockserver.dao.CompanyDao' available: expected at least 1 bean which qua Spring 報錯 :org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'statementController' available org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type 'xxx 'available spring 框架錯誤 org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.thinkplatform.dao.UserLogDao' available: expected at least 1 bean which qualifies as autowi Caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type "" available: expected at least 1 bean which qualifies as autowire candidate Spring Boot 調用 MongoRepository時報org.springframework.beans.factory.NoSuchBeanDefinitionException錯誤的解決辦法 解決 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type的問題 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.yang.service.StudentInterface' available: expected at least 1 bean which qualifies as autowire candidate
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM