首页 > 其他分享 >Spring Task Scheduler - No qualifying bean of type [org.springframework.sch

Spring Task Scheduler - No qualifying bean of type [org.springframework.sch

时间:2023-01-02 18:33:31浏览次数:66  
标签:... Task sch No Spring public bean org class

1. Overview

In this article, we are discussing the Springorg.springframework.beans.factory.NoSuchBeanDefinitionException – this is a common exception thrown by the BeanFactory when trying to resolve a bean that simply isn’t defined in the Spring Context.

We will discuss here the possible causes for this problem and the available solutions.

2. Cause: No qualifying bean of type [...] found for dependency

The most common cause of this exception is simply trying to inject a bean that isn’t defined. For example – BeanB is wiring in a collaborator – BeanA: 

​​@Component​​
​​public​​​ ​​ class​​​ ​​ BeanA {​​

​​@Autowired​​
​​private​​​ ​​ BeanB dependency;​​
​​...​​
​​}​​

Now, if the dependency – BeanB – is not defined in the Spring Context, the bootstrap process will fail with the no such bean definition exception:

​​org.springframework.beans.factory.NoSuchBeanDefinitionException: ​​
​​No qualifying bean of ​​​​type​​​ ​​ [org.baeldung.packageB.BeanB] found ​​​​for​​​ ​​ dependency: ​​
​​expected at least 1 bean ​​​​which​​​ ​​ qualifies as autowire candidate ​​​​for​​​ ​​ this dependency. ​​
​​Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=​​​​true​​​​)}​​

The reason is clearly indicated by Spring: “expected at least 1 bean which qualifies as autowire candidate for this dependency“

One reason BeanB may not exist in the context – if beans are picked up automatically byclasspath scanning, and if BeanB is correctly annotated as a bean (@Component,@Repository, @Service, @Controller, etc) – is that it may be defined in a package that is not scanned by Spring:

​​package​​​ ​​ org.baeldung.packageB;​​
​​@Component​​
​​public​​​ ​​ class​​​ ​​ BeanB { ...}​​

While the classpath scanning may be configured as follows:

​​@Configuration​​
​​@ComponentScan​​​​(​​​​"org.baeldung.packageA"​​​​)​​
​​public​​​ ​​ class​​​ ​​ ContextWithJavaConfig {​​
​​...​​
​​}​​

If beans are not automatically scanned by instead defined manually, then BeanB is simply not defined in the current Spring Context.

3. Cause: No qualifying bean of type [...] is defined

Another cause for the exception is the existence of two bean definitions in the context, instead of one. For example, if an interface – IBeanB is implemented by two beans –BeanB1 and BeanB2:

​​@Component​​
​​public​​​ ​​ class​​​ ​​ BeanB1 ​​​​implements​​​ ​​ IBeanB {​​
​​//​​
​​}​​
​​@Component​​
​​public​​​ ​​ class​​​ ​​ BeanB2 ​​​​implements​​​ ​​ IBeanB {​​
​​//​​
​​}​​

Now, if BeanA autowires this interface, Spring will not know which one of the two implementations to inject:

​​@Component​​
​​public​​​ ​​ class​​​ ​​ BeanA {​​

​​@Autowired​​
​​private​​​ ​​ IBeanB dependency;​​
​​...​​
​​}​​

And again, this will result in a NoSuchBeanDefinitionException being thrown by theBeanFactory:

​​Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: ​​
​​No qualifying bean of ​​​​type​​​ ​​ [org.baeldung.packageB.IBeanB] is defined: ​​
​​expected single matching bean but found 2: beanB1,beanB2​​

Similarly, Spring clearly indicates the reason for the wiring failure: “expected single matching bean but found 2″.

Notice however, that in this case, the exact exception being thrown is notNoSuchBeanDefinitionException but a subclass – theNoUniqueBeanDefinitionException. This new exception has been ​​introduced in Spring 3.2.1​​, for exactly this reason – to differentiate between the cause where no bean definition was found and this one – where several definitions are found in the context.

Before this change, the exception above was:

​​Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: ​​
​​No qualifying bean of ​​​​type​​​ ​​ [org.baeldung.packageB.IBeanB] is defined: ​​
​​expected single matching bean but found 2: beanB1,beanB2​​

One solution to this problem is to use the @Qualifier annotation to specify exactly the name of the bean we want to wire:

​​@Component​​
​​public​​​ ​​ class​​​ ​​ BeanA {​​

​​@Autowired​​
​​@Qualifier​​​​(​​​​"beanB2"​​​​)​​
​​private​​​ ​​ IBeanB dependency;​​
​​...​​
​​}​​

Now Spring has enough  information to make the decision of which bean to inject –BeanB1 or BeanB2 (the default name of BeanB2 is beanB2).

4. Cause: No Bean Named [...] is defined

A NoSuchBeanDefinitionException may also be thrown when a bean that isn’t defined isrequested by name from the Spring context:

​​@Component​​
​​public​​​ ​​ class​​​ ​​ BeanA ​​​​implements​​​ ​​ InitializingBean {​​

​​@Autowired​​
​​private​​​ ​​ ApplicationContext context;​​

​​@Override​​
​​public​​​ ​​ void​​​ ​​ afterPropertiesSet() {​​
​​context.getBean(​​​​"someBeanName"​​​​);​​
​​}​​
​​}​​

In this case, there is no bean definition for “someBeanName” – leading to the following exception:

​​Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: ​​
​​No bean named ​​​​'someBeanName'​​​ ​​ is defined​​

Again, Spring clearly and concisely indicates the reason for the failure: “No bean named X is defined“.

5. Cause: Proxied Beans

When a bean in the context is proxied using the JDK Dynamic Proxy mechanism, then the proxy will not extend the target bean (it will however implement the same interfaces).

Because of this, if the bean is injected by an interface, it will be correctly wired in. If however the bean is injected by the actual class, then Spring will not find a bean definition that matches the class – since the proxy does not actually extend the class.

A very common reason the bean may be proxied is the Spring transactional support – namely beans that are annotated with @Transactional.

For example, if ServiceA injects ServiceB, and both services are transactional, injecting by the class definition will not work:


​​@Service​​
​​@Transactional​​
​​public​​​ ​​ class​​​ ​​ ServiceA ​​​​implements​​​ ​​ IServiceA{​​

​​@Autowired​​
​​private​​​ ​​ ServiceB serviceB;​​
​​...​​
​​}​​

​​@Service​​
​​@Transactional​​
​​public​​​ ​​ class​​​ ​​ ServiceB ​​​​implements​​​ ​​ IServiceB{​​
​​...​​
​​}​​

The same two services, this time correctly injecting by the interface, will be OK:

​​@Service​​
​​@Transactional​​
​​public​​​ ​​ class​​​ ​​ ServiceA ​​​​implements​​​ ​​ IServiceA{​​

​​@Autowired​​
​​private​​​ ​​ IServiceB serviceB;​​
​​...​​
​​}​​

​​@Service​​
​​@Transactional​​
​​public​​​ ​​ class​​​ ​​ ServiceB ​​​​implements​​​ ​​ IServiceB{​​
​​...​​
​​}​​

6. Conclusion

This tutorial discussed examples of the possible causes for the commonNoSuchBeanDefinitionException – with a focus on how to address these exceptions in practice.

The implementation of all these exceptions examples can be found in ​​the github project​​ – this is an Eclipse based project, so it should be easy to import and run as it is.

 

原文地址:​http://www.baeldung.com/spring-nosuchbeandefinitionexception​

​http://stackoverflow.com/questions/31199888/spring-task-scheduler-no-qualifying-bean-of-type-org-springframework-scheduli​

 



标签:...,Task,sch,No,Spring,public,bean,org,class
From: https://blog.51cto.com/u_15147537/5983939

相关文章

  • 2023.1.2 No.5 新年
    距离上一次写日记过去了一个多月了,我12月初回的家,带着一大堆的实验报告。但是谁也没想到刚回家一周不到就放开了,12月中旬我母亲感染,几天后我也跟着感染。回家后摆烂导......
  • NC16679 [NOIP2003]神经网络
    题目链接题目题目描述人工神经网络(ArtificialNeuralNetwork)是一种新兴的具有自我学习能力的计算系统,在模式识别、函数逼近及贷款风险评估等诸多领域有广泛的应用。对......
  • (转)ERROR 3021 (HY000): This operation cannot be performed with a running slave io
    在mysql主从复制的时候,使用changeto改变主从信息的时候,却发现change不了提示ERROR3021(HY000):Thisoperationcannotbeperformedwitharunningslaveiothread......
  • some thing about conda and jupyter notebook
    -condaappendchannels,$condaconfig--appendchannelsconda-forgethenasimplecondaconfig~/.condarcshouldbe,auto_activate_base:falseshow_channe......
  • 合宙ESP32C3 + VSCode + OpenOCD调试经历
    合宙ESP32C3+VSCode+OpenOCD调试经历环境Windows10VSCode+ESP-IDF合宙ESP32C3(无串口芯片版本)理论想要直接使用内置JTAG,USB要求连接GPIO18和GPIO19合宙ESP32......
  • /home/software/python/Modules/_ctypes/_ctypes.c:118:17: fatal error: ffi.h: No s
     001、python3.11编译报错/home/software/python/Modules/_ctypes/_ctypes.c:118:17:fatalerror:ffi.h:Nosuchfileordirectory  002、解决方法[root@PC......
  • 使用@Scheduled实现定时任务
    使用@Scheduled实现定时任务用一个实现签到功能的Demo工程为例1.SignApplication启动类importorg.springframework.boot.SpringApplication;importorg.springframew......
  • [WARNING] Resolved [org.springframework.web.HttpMediaTypeNotSupportedException:
    这是json转对象失败的报错 这是web层的代码   这是报错      解决方法: 第一步:请求body选raw,并选择application/json类型   第......
  • [NOI2018] 归程 题解
    题目描述[NOI2018]归程思路题目说,所有海拔\(\leqp\)的边都会有积水,因此所有的边分为了两个集合\(S_车,S_步\),其中\(S_车\)所有的边的海拔都\(>p\),\(S_步\)反......
  • 第12代处理器 Widnows 10 调度
    原文地址:https://www.bilibili.com/video/BV1LK411B7ZS针对第1类处理器电源效率的处理器性能核心放置最小核心数量powercfg-attributesSUB_PROCESSOR0cc5b647-c1df-......