Spring学习系列:错误记录(一):Spring注解错误记录

错误提示

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.yeonon.web.LoginController.setUserService(com.yeonon.service.UserService); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.yeonon.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

这是运行后浏览器中的错误提示,在IDEA中也是一样的。

从字面上来看,似乎是无法注解注入,原因是找不到对应的bean,但是我多次查看定义bean的XML文件中并没有发现错误,而且单元测试那里用到了UserService的注解,并完美通过,这就让我很诧异。

问题排查

Google了很长时间,终于在一篇文章中找到了原因,文章说到不仅仅要检查定义bean,还要看看监听器部分,这让我看到了希望。

问题解决

web.xml文件

问题就在这里了,监听器本应是ContextLoaderListener,而我这里写成了ContextCleanupListener,两者大不相同。改过来之后,Web能正常的运行了。
应该是写代码的时候图快(用IDEA自动补全),不仔细看导致的。
(IDEA自动补全是好,但是也要看清哈)

ContextCleanupListener是什么

为了不在同一个地方跌倒,我特意去查了一下官方文档,ContextCleanupListener是个什么东西。

    Web application listener that cleans up remaining disposable attributes in the ServletContext, i.e. attributes which implement DisposableBean and haven't been removed before. This is typically used for destroying objects in "application" scope, for which the lifecycle implies destruction at the very end of the web application's shutdown phase.

这是文档中写的,从字面上看,似乎是显示的销毁一个对象。(英语水平有限,如有错误,感谢指正!),这就很可怕了,如果在还没创建就销毁,就很难发现问题,DEBUG相当麻烦了。

初学Spring,以上描述如有错误或不足,感谢指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值