Spring NoSuchBeanDefinitionException六大原因总结(非常实用)【添加第三方jar的类 作Service层的依赖失败 因为此错。xml定义“同名bean”解决】

=====给了我思路。

【添加第三方jar的类 作Service层的依赖失败 因为此错。xml定义“同名bean”解决】

======


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
@Component
publicclass  BeanA {
 
    @Autowired
    privateBeanB 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 fordependency:
    expected at least 1 bean whichqualifies as autowire candidate forthis 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
packageorg.baeldung.packageB;
@Component
publicclass  BeanB { ...}

While the classpath scanning may be configured as follows:

?
1
2
3
4
5
@Configuration
@ComponentScan("org.baeldung.packageA")
publicclass  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
@Component
publicclass  BeanB1 implementsIBeanB {
    //
}
@Component
publicclass  BeanB2 implementsIBeanB {
    //
}

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
@Component
publicclass  BeanA {
 
    @Autowired
    privateIBeanB 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
@Component
publicclass  BeanA {
 
    @Autowired
    @Qualifier("beanB2")
    privateIBeanB 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

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
@Component
publicclass  BeanA implementsInitializingBean {
 
    @Autowired
    privateApplicationContext context;
 
    @Override
    publicvoid  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
@Transactional
publicclass  ServiceA implementsIServiceA{
 
    @Autowired
    privateServiceB serviceB;
    ...
}
 
@Service
@Transactional
publicclass  ServiceB implementsIServiceB{
    ...
}

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
@Transactional
publicclass  ServiceA implementsIServiceA{
 
    @Autowired
    privateIServiceB serviceB;
    ...
}
 
@Service
@Transactional
publicclass  ServiceB implementsIServiceB{
    ...
}

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个异常`org.springframework.beans.factory.NoSuchBeanDefinitionException`表示没有找到符合条件的bean。在给定的引用中,它们都是关于缺少特定型的bean。根据提供的信息,我们可以推断出可能有两个问题导致这个异常。第一个可能是在代码中没有正确配置bean,第二个可能是缺少相应的依赖项。 对于第一个问题,我们需要检查代码中的bean配置是否正确。可能需要检查使用注解或xml配置的bean。确保正确设置bean的扫描路径和包名,以便Spring容器能够找到并管理它们。 对于第二个问题,我们需要检查是否添加了正确的依赖项。根据提供的Maven坐标,我们可以看到应该添加`org.springframework:spring-beans`的jar包。确保pom.xml文件中包含正确的依赖项,并且版本号与提供的坐标一致。 总而言之,要解决这个异常,我们需要检查代码中的bean配置和依赖项配置是否正确,并确保它们与提供的引用一致。这样就可以解决`org.springframework.beans.factory.NoSuchBeanDefinitionException`异常。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type](https://blog.csdn.net/weixin_51962261/article/details/127827133)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.qf](https://blog.csdn.net/qq_45399396/article/details/120854665)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [spring-beans-5.2.0.RELEASE-API文档-中英对照版.zip](https://download.csdn.net/download/qq_36462452/86086690)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值