Spring容器启动常见错误整理

以下是工作过程中常见的一些Spring容器启动时错误整理,方便后期再次遇到的时候,能够快速查询。本文章将不定时长期更新

1、NoSuchBeanDefinitionException

org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'xxx.xxx.xxx': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'xxx.xxx.xxx' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}

产生的原因可能有以下两点:

  1. 如果该Bean是通过xml配置文件配置实例化的,请检查xml配置文件中是否存在该类型的Bean;
  2. 如果该Bean是通过注解实例化的,请检查该类型Bean的Class类是否使用了注解标签,形如@Service、@Repository、@Component,以保证可以被Spring容器加载到

2、BeanNotOfRequiredTypeException

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'BeanName' is expected to be of type '包.类名' but was actually of type '包.类名'

产生的原因可能有以下两点:

  1. 如果该Bean是通过xml配置文件实例化的,请检查xml配置文件中是否存在该类型Bean;
  2. 如果该Bean是通过注解实例化的,请检查注解Bean的Class类是否与需要加载到Spring容器里的其他Bean的Class类存在着“同名不同包”的情况。但这种情况的出现需要一个特殊的场景:使用@Resource注解JavaBean。由于@Resource是默认按照“BeanName”初始化的JavaBean,当出现“同名不同包”时,Spring容器无法确定当前注解的Bean的类型。而当我们使用@Autowired形式实例化JavaBean时,由于@Autowired默认是按照Bean的Class类类型初始化,Spring容器会自行为加载到Spring容器里的JavaBean创建id,并解决BeanID冲突的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值