org.springframework.beans.factory.NoSuchBeanDefinitionException

本文详细解析了在Spring框架中,当使用@Autowired注解时,出现NoSuchBeanDefinitionException异常的原因及解决方案。通过理解ContextLoaderListener的工作原理,揭示了监听器和过滤器不被Spring容器管理的真相,并提供了解决方案。

错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.test.service.PersonService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

注:

ContextLoaderListener源代码,发现它继承自ContextLoader,并且实现ServletContextListener接口,所以这样写是找不到注解的类等等的,全都要自己在spring.xml里bean里配置<bean id="xxx" class="xxx"/>

ServletContextListener获取spring注入的bean,侦听器、过滤器等不归Spring容器托管,无法用注解注入!!!

经过测试:ContextLoaderListener这段保留,再实现ServletContextListener接口,同时用并不冲突。解决问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值