码迷,mamicode.com
首页 > 编程语言 > 详细

spring Boot异步操作报错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.self.spring.springboot.Jeep' available

时间:2017-08-03 21:49:25      阅读:604      评论:0      收藏:0      [点我收藏+]

标签:sync   实现   factor   操作   com   get   default   import   port   

我也是最近开始学习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(),才会正常运行不报错。。

其实我也不得解答,如果有哪位高手看到了,请给我留言,已解我心中之谜。。。。

spring Boot异步操作报错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.self.spring.springboot.Jeep' available

标签:sync   实现   factor   操作   com   get   default   import   port   

原文地址:http://www.cnblogs.com/javJoker/p/7281688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!