Spring Task Scheduler - No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
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:
|
1
2
3
4
5
6
7
|
@Componentpublic 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:
|
1
2
3
4
|
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:
|
1
2
3
|
package org.baeldung.packageB;@Componentpublic class BeanB { ...} |
While the classpath scanning may be configured as follows:
|
1
2
3
4
5
|
@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:
|
1
2
3
4
5
6
7
8
|
@Componentpublic class BeanB1 implements IBeanB { //}@Componentpublic class BeanB2 implements IBeanB { //} |
Now, if BeanA autowires this interface, Spring will not know which one of the two implementations to inject:
|
1
2
3
4
5
6
7
|
@Componentpublic class BeanA { @Autowired private IBeanB dependency; ...} |
And again, this will result in a NoSuchBeanDefinitionException being thrown by theBeanFactory:
|
1
2
3
|
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:
|
1
2
3
|
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:
|
1
2
3
4
5
6
7
8
|
@Componentpublic 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:
|
1
2
3
4
5
6
7
8
9
10
11
|
@Componentpublic 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:
|
1
2
|
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:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Service@Transactionalpublic class ServiceA implements IServiceA{ @Autowired private ServiceB serviceB; ...}@Service@Transactionalpublic class ServiceB implements IServiceB{ ...} |
The same two services, this time correctly injecting by the interface, will be OK:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Service@Transactionalpublic class ServiceA implements IServiceA{ @Autowired private IServiceB serviceB; ...}@Service@Transactionalpublic 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://blog.csdn.net/free4294/article/details/38706569
http://stackoverflow.com/questions/31199888/spring-task-scheduler-no-qualifying-bean-of-type-org-springframework-scheduli
Spring Task Scheduler - No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined的更多相关文章
- Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined(转)
最近项目里面,用了spring的定时任务,一直以来,项目运行的不错.定时器也能正常使用.可是,今天启动项目测试的时候,盯着启动Log看了一阵子,突然间发现,启动的Log中居然有一个异常,虽然一闪而过, ...
- Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined stac ...
- No qualifying bean of type 'org.springframework.scheduling.TaskScheduler' available
2019-07-22 17:59:30,966 [DEBUG] [localhost-startStop-1] [ScheduledAnnotationBeanPostProcessor.java : ...
- spring异步执行报异常No qualifying bean of type 'org.springframework.core.task.TaskExecutor' available
最近观察项目运行日志的时候突然发现了一个异常, [2018-04-03 10:49:07] 100.0.1.246 http-nio-8080-exec-9 DEBUG org.springframe ...
- spring cloud gateway网关启动报错:No qualifying bean of type 'org.springframework.web.reactive.DispatcherHandler'
网关配置好后启动报错如下: org.springframework.context.ApplicationContextException: Unable to start web server; n ...
- NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.web.servlet.view.InternalResourceViewResolver' available
问题描述: 项目中需要配置多个视图解析器,所以使用ContentNegotiatingViewResolver来处理,在其内部设置了FreeMarkerViewResolver .InternalRe ...
- SpringBoot- springboot集成Redis出现报错:No qualifying bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory'
Springboot将accessToke写入Redisk 缓存,springboot集成Redis出现报错 No qualifying bean of type 'org.springframewo ...
- 解决spring-test中Feign问题: No qualifying bean of type 'org.springframework.cloud.openfeign.FeignContext' available
问题现象: 启动测试类(含通过Feign远程调用的组件),报错: No qualifying bean of type 'org.springframework.cloud.openfeign.Fei ...
- 解决:No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency
错误: Description: Field jdbcTemplate in com.gwd.dao.impl.IUserDaoImpl required a bean of type 'org.sp ...
随机推荐
- PostgreSQL与MySQL比较(转)
Mysql 使用太广泛了,以至于我不得不将一些应用从mysql 迁移到postgresql, 很多开源软件都是以Mysql 作为数据库标准,并且以Mysql 作为抽象基础的,但是具体使用过程中,发现M ...
- 【转】Java基本数据类型
原文网址:http://blog.csdn.net/bingduanlbd/article/details/27790287 Java语言是静态类型的(statical typed),也就是说所有变量 ...
- Majority Element II 解答
Question Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Th ...
- bzoj2243-染色(动态树lct)
解析:增加三个变量lc(最左边的颜色),rc(最右边的颜色),sum(连续相同颜色区间段数).然后就是区间合并的搞法.我就不详细解释了,估计你已经想到 如何做了. 代码 #include<cst ...
- 【转】Linux系统性能分析命令
作为一名linux系统管理员,最主要的工作是优化系统配置,使应用在系统上以最优的状态运行,但是由于硬件问题.软件问题.网络环境等的复杂性和多变性,导致对系统的优化变得异常复杂,如何定位性能问题出在哪个 ...
- C#实现下载功能,可用于软件自动更新
以前在百度写的文档,转移到此处 软件截图: 代码下载: http://twzy.ys168.com/ 在代码下载文件夹中 //代码: using System; using System.Comp ...
- SEO 外链 内链 的定义
外链 外链就是指从别的网站导入到自己网站的链接.导入链接对于网站优化来说是非常重要的一个过程.导入链接的质量(即导入链接所在页面的权重)直接决定了我们的网站在搜索引擎中的权重. 外链是互联网的血液,是 ...
- JAVA反射机制示例,读取excel数据映射到JAVA对象中
import java.beans.PropertyDescriptor; import java.io.File; import java.io.FileInputStream; import ja ...
- js控制html5 audio的暂停、播放、停止
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name ...
- android 注释常用标签
javadoc: {@link ActivityGroup} 链接到包.类: {@link #setContentView} 用#链接到类成员: @return View The current ...