在用spring写一个简单的查询功能的时候,测试查询能够通过,但是在tomcat服务器中始终报错
javax.servlet.ServletException: Servlet[springmvc]的Servlet.init()引发异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.lmj.service.ItemService com.lmj.controller.ItemController.itemService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.lmj.service.ItemService] 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)}
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.lmj.service.ItemService com.lmj.controller.ItemController.itemService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.lmj.service.ItemService] 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)}
一直在报错,我百度了很多的解决办法,很多都说是bean没加载这个ItemService,自动注入有问题,我改了很多次,还是在出这个错,实在自己找不到这个错误在哪里,重新创建一个maven项目,将代码直接copy进去,发现还是这个错误,然而测试类能够通过,于是开始查找xml文件的问题,从appliction.xml开始查找,发现代码没有问题,启动tomcat能够加载index页面,我直接进入web.xml查找问题,发现了监听器的问题
这个是报错的web.xml:
修改之后的web.xml:
原来自己监听器写错了,修改之后成功运行.
总结:写代码的时候一定要细心,一个小错误就能够引发很多的错误,学习要长久且细心的学下去