NoSuchBeanDefinconstitionnException一般是什么原因导致

测试开发之路
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
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
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&, 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
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 ApplicationC
&&&&@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:
@Transactional
public class &ServiceA&implements IServiceA{
&&&&@Autowired
&&&&private ServiceB serviceB;
@Transactional
public class &ServiceB&implements IServiceB{
The same two services, this time correctly&injecting by the interface, will be OK:
@Transactional
public class &ServiceA&implements IServiceA{
&&&&@Autowired
&&&&private IServiceB serviceB;
@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&&– this is an Eclipse based project, so it should be easy to import and run as it is.
阅读(...) 评论()【转】 Spring NoSuchBeanDefinitionException原因分析
时间: 00:23:38
&&&& 阅读:178
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&摘要:本文译自Eugen Paraschiv文章&NoSuchBeanDefinitionException 原文链接:&&感谢Eugen Paraschiv对此所做的研究。
在本文中,我将通过实例向你展示Spring 中org.springframework.beans.factory.NoSuchBeanDefinitionException 出现的原因。如果BeanFactory在Spring Context中没有找到bean的实例,就会抛出这个常见的异常。
Cause: No qualifying bean of type […] found for dependency
这个异常的出现一般是因为需要注入的bean未定义&有一个类BeanA.
package com.csdn.training.
@Component
public class BeanA {
@Autowired
private BeanB beanB;
有一个类BeanB.java
package com.csdn.training.
@Component
public class BeanB {
配置文件applicationContext.xml
用一个类去启动拉起Spring容器:
package com.csdn.
public class AppTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
BeanA beanA = (BeanA) context.getBean("beanA");
自动扫描包路径缺少了BeanB,它和BeanA 不在同一路径下&如果依赖 IBusinessService 在Spring 上下文中没有定义,引导进程报错:No Such Bean Definition Exception.
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.csdn.training.service.BeanB] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Spring会提示:”Expected at least 1 bean which qualifies as autowire candidate for this dependency“(依赖至少有一个备选的bean能被自动注入)&出现异常的原因是IBusinessService 在上下文中不存在:如果bean是通过classpath自动扫描来装配,并且IBusinessService已经正确的加上了注解(@Component,@Repository,@Service,@Controller等),也许是你没有把正确的包路径告诉Spring。
配置文件可以如下配置:
如果bean不能自动被扫描到,而手动定义却可以识别,那Bean就没有在Spring上下文中定义。
Cause: No qualifying bean of type […] is defined
造成这一异常的原因可能是Spring上下文中存在两个或以上该bean的定义。如果接口IService 有两个实现类 ServiceImplA 和ServiceImplB&接口:IService.java
package com.csdn.training.
public interface IService {
两个实现类:ServiceImplA.java
package com.csdn.training.
import org.springframework.stereotype.S
public class ServiceImplA implements IService {
ServiceImplB.java
package com.csdn.training.
import org.springframework.stereotype.S
public class ServiceImplB implements IService {
如果BeanA 自动注入这一接口,Spring就无法分辨到底注入哪一个实现类:
package com.csdn.training.
import com.csdn.training.service.IS
@Component
public class BeanA {
@Autowired
private IService serviceI
然后,BeanFactory就抛出异常NoSuchBeanDefinitionException&Spring会提示:”expected single matching bean but found 2“(只应该匹配一个bean但是找到了多个)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.csdn.training.service.IService] is defined:
expected single matching bean but found 2: serviceImplA,serviceImplB
上例中,有时你看到的异常信息是NoUniqueBeanDefinitionException,它是NoSuchBeanDefinitionException 它的子类,在Spring 3.2.1中,修正了这一异常,为的是和bean未定义这一异常区分开。
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.csdn.training.service.IService] is defined: expected single matching bean but found 2: serviceImplA,serviceImplB
解决这一异常可以用注解@Qualifier 来指定想要注入的bean的名字。
package com.csdn.training.
import com.csdn.training.service.IS
@Component
public class BeanA {
@Autowired
@Qualifier("serviceImplA")
private IService serviceI
修改以后,Spring就可以区分出应该注入那个bean的实例,需要注意的是ServiceImplA的默认实例名称是serviceImplA
Cause: No Bean Named […] is defined
当你通过具体的bean的名字去得到一个bean的实例的时候,如果Spring 没有在上下文中找到这个bean,就会抛出这个异常。
package com.csdn.
public class AppTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
context.getBean("beanX");
这个例子中,没有一个bean被定义成 “beanX”,就会抛出如下异常:&Spring会提示:”No bean named XXX is defined” (没有找到一个名叫XXX的bean)
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘beanX‘ is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition
Cause: Proxied Beans
如果bean由JDK的动态代理机制所管理,那么代理将不会继承该bean,它只会实现与其相同的接口。因此,如果bean是通过接口注入的,就可以成功注入。如果通过其实现类注入,Spring就无法将bean实例与类关联,因为代理并不真正的继承于类。&出现这一原因,很有可能是因为你使用了Spring的事物,在bean上使用了注解@Transactional&如下,ServiceA注入了ServiceB,这两个service都使用了事物,通过实现类注入bean就不起作用了。&借口IService.java无变化,其实现类加上事物的注解&ServiceImplA.java
package com.csdn.training.
@Transactional
public class ServiceImplA implements IService {
@Autowired
private ServiceImplB serviceImplB;
ServiceImplB.java
package com.csdn.training.
@Transactional
public class ServiceImplB implements IService {
如果改成通过接口注入,就可以:
ServiceImpl.java
package com.csdn.training.
@Transactional
public class ServiceImplA implements IService {
@Autowired
@Qualifier("serviceImplB")
private IService serviceImplB;
本文通过几个小例子,分析了NoSuchBeanDefinitionException 用了这一异常可能出现的情形,对于我们在实际开发过程中定位错误提供了一定的参考。&原文为英文版,我在原文的基础上稍作改动,但又尽量保持原文的精髓,如果对译文有异议,欢迎指正。标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文地址:http://www.cnblogs.com/dafanshu1996/p/6498784.html
&&国之画&&&& &&&&chrome插件
版权所有 京ICP备号-2
迷上了代码!没有更多推荐了,
不良信息举报
举报内容:
exception集合
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!nosuchbeandefinitionexception - 我的异常网当前位置:& &&&nosuchbeandefinitionexceptionnosuchbeandefinitionexceptionwww.MyException.Cn&&网友分享于:&&&搜索量:8次
场景:NoSuchBeanDefinitionException: No bean named 'userService' is defined求助:NoSuchBeanDefinitionException: No bean named 'userService' is defined
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' is defined
配置SpringJunit4测试时如何配置@ContextConfiguration(locations = { "classpath*:/conf/common/applicationContext.xml" })?????
具体问题描述如下:
1、问题报错:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' is defined
2、配置文件
文档结构:
测试代码 MybatisTest1:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/conf/common/applicationContext.xml" })
public class MybatisTest1 {
private UserService userS
@Resource(name = "userService")
public void setUserService(UserService userService) {
this.userService = userS
public void setUp() throws Exception {
public void tearDown() throws Exception {
public void test() {
userService.findAll();
配置文件applicationContext.xml:
&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&
/WEB-INF/conf/*/*.xml
&/param-value&
&/context-param&
&!-- log4j --&
&context-param&
&param-name&log4jConfigLocation&/param-name&
&param-value&
/WEB-INF/conf/common/log4j.properties
&/param-value&
&/context-param&
&listener&
&listener-class&org.springframework.web.util.Log4jConfigListener&/listener-class&
&/listener&
&!-- Loads the Spring web application context --&
&listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class&
&/listener&
&!-- The front controller of this Spring Web application, responsible for handling all application requests --&
&servlet-name&Spring MVC Dispatcher Servlet&/servlet-name&
&servlet-class&org.springframework.web.servlet.DispatcherServlet&/servlet-class&
&init-param&
&param-name&contextConfigLocation&/param-name&
&param-value&/WEB-INF/conf/common/applicationContext.xml&/param-value&
&/init-param&
&load-on-startup&1&/load-on-startup&
&/servlet&
3、分析原因:
配置文件applicationConetxt.xml 找不到,导致注解无法找到userService bean的配置,但不知道 (@ContextConfiguration(locations = { "classpath*:/conf/common/applicationContext.xml" }))这个配置该如何修改。
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关文章 123456 Copyright & &&版权所有NoSuchBeanDefinitionException
时间: 15:41:57
&&&& 阅读:272
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&异常信息如下:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘zybCoreBS‘ is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:971) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880) at com.tansun.scf.zyb.intf.test.LoadPropertis.getBean(LoadPropertis.java:25) at com.tansun.scf.zyb.intf.test.Test01.setUp(Test01.java:24) at junit.framework.TestCase.runBare(TestCase.java:132) at junit.framework.TestResult$1.protect(TestResult.java:110) at junit.framework.TestResult.runProtected(TestResult.java:128) at junit.framework.TestResult.run(TestResult.java:113) at junit.framework.TestCase.run(TestCase.java:124) at junit.framework.TestSuite.runTest(TestSuite.java:232) at junit.framework.TestSuite.run(TestSuite.java:227) at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
我的application-channel-test.xml文件内容如下:
&?xml version="1.0" encoding="UTF-8"?&&!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&&beans&
&bean id="zybCoreBS" class="com.tansun.scf.zyb.intf.core.service.impl.ZybCoreBSImpl"&
&property name="bussCommonBS"&
&ref bean="bussCommonBS" /&
&/property& &/bean&
&bean id="zybCmsBS" class="com.tansun.scf.zyb.intf.cms.service.impl.ZybCmsBSImpl"&
&property name="bussCommonBS"&
&ref bean="bussCommonBS" /&
&/property& &/bean&
&bean id="bussCommonBS" class="com.tansun.scf.common.service.impl.BussCommonBSImpl"&
&property name="sysparamBS"&
&ref bean="sysparamBS" /&
&/property& &/bean& &/beans&
单元测试调zybCmsBS中的方法可以跑,但是当调到bussCommonBS的方法时报空指针异常,已经注入了还报这个说不过去,于是测试zybCoreBS,没想到直接报错如上,再测试直接调bussCommonBS,报空指针异常,分析了半天也不知道是什么原因,有大神可以指点下么?
我的Test类如下:
public class Test01 extends LoadPropertis { // public TestAction testA private IZybCmsBS zybCmsBS;// private IBussCommonBS bussCommonBS; private IZybCoreBS zybCoreBS;
protected void setUp() throws Exception{
super.setUp();
zybCmsBS = (IZybCmsBS)this.getBean("zybCmsBS");//
bussCommonBS = (IBussCommonBS)this.getBean("bussCommonBS");
zybCoreBS = (IZybCoreBS)this.getBean("zybCoreBS"); }
protected void tearDown()throws Exception{
super.tearDown();
public void test01() {//BlacklistInVO blacklistInVO = new BlacklistInVO();CurrAccoInVo currAccoInVo = new CurrAccoInVo();
zybCmsBS.blacklistInfoSync(blacklistInVO);
zybCoreBS.currAccoQuery(currAccoInVo);
} catch (Exception e) {
System.out.println("失败");
Date date = bussCommonBS.getBussDate();//
System.out.println(date);
}}标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文:http://www.cnblogs.com/lufm/p/5537523.html
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!

我要回帖

更多关于 postition 的文章

 

随机推荐