zoukankan      html  css  js  c++  java
  • 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(),才会正常运行不报错。。

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

  • 相关阅读:
    接口测试基础一--HTTP请求
    python笔记8-python的异常处理
    web自动化测试中的八大定位方法,推荐使用xpath
    charles抓取https包
    Python 中WebElement 类中的部分操作
    selenium 启动浏览器后基本操作:后退、前进、刷新、关闭窗口、关闭会话
    fiddler抓包可以抓到电脑数据抓不到手机上的数据------防火墙问题
    Charles的安装与破解
    python+ selenium + webdriver的环境准备
    python——print函数
  • 原文地址:https://www.cnblogs.com/javJoker/p/7281688.html
Copyright © 2011-2022 走看看