BeanCreationException:Injection of autowired dependencies failed异常的解决思路
来源:互联网 发布:淘宝国外代购怎么做 编辑:程序博客网 时间:2024/05/21 02:20
关于BeanCreationException:Injection of autowired dependencies failed异常的解决思路
总结一下今天下午的研究成果,整合SSM项目时候,最后测试从库中查询数据报了一个这样的异常,很是让我头大,足足百度+stackoverflow了一下午,愣是没有研究出成果,最后快要吃饭的时候静下心来想一想,到底是哪里出了问题,从最开始的流程去想。。。检查的时候,恍然大悟,所以想记录的是今天的解决问题的思路与方法。
先来看一下我的异常问题的截图
具体异常代码如下
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemController':Could not autowire field: private com.taotao.service.ItemService com.taotao.controller.ItemController.itemService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.taotao.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)} at .........................省略
解决思路:
先看异常信息的时候:大概意思是说Bean容器创建异常,@autowired的注解注入Service层失败了,同时在后面又报出了Controller层也是没有注解注入!所有都是围绕着这个@autowired不能自动注入,而在我的代码中是这样写到的:
Service层的实现类
@Servicepublic class ItemServiceImpl implements ItemService { @Autowired private ItemMapper itemMapper;}
Controller层:
@Controllerpublic class ItemController { @Autowired private ItemService itemService;}
通过上面的@Service(来自SpringMVC的注解)来实现Service层的实现类注入到Spring容器中。而此时这里的@Autowired的DAO层接口的代理对象并没有报错,当时在网上查找了一番,以为是因为DAO层没有用@Compent或者@Repository这样的注解造成的错误,后来思考了一下,发现因为是用Mapper映射做的DAO层接口的实现类,所以完全没有必要去做这个注解,那么问题也就与DAO层无关了。
而@Controller这里的注解是没有报错的。。也就是说我在spring-mvc.xml配置文件中的包扫描注解是没有问题的。代码如下:
<!-- 扫描包Controller --> <context:component-scan base-package="com.xxxxx.controller"></context:component-scan> <!-- 配置mvc注解驱动 --> <!-- 例如@resquestmapping这类的 --> <mvc:annotation-driven />
但是Controller层报错的信息是注入到它里的Service层,也就是@Autowired这个注解。可是为什么@Autowired会失效呢?那么仔细想一下肯定是Service层并没有注入到Spring容器中去!所以追溯到Service的实现类去。也就是说Service的实现类并没有成功注入到Spring容器中。
既然是@Service的注解没有生效,那么问题应该是出现在applicationContext-service.xml里的包扫描了,然而检查了一下代码,也并没有问题,代码如下:
<!-- 扫描包Service实现类 --> <context:component-scan base-package="com.xxxxx.service"></context:component-scan>
此时我的心情是崩溃的- -。。。马上该吃饭的时候,突然想到。。。这些配置既然没有错的话,为什么不行?根本问题到底在哪里!突然想到了万一是Spring-*,xml的配置文件没有加载进来呢,也会导致包扫描注解失效,注入Spring容器中失效!所以果不其然,看了一眼web.xml里的配置。
<!-- 加载spring容器 --> <!-- 初始化加载application.xml的各种配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/application-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置springmvc前端控制器 --> <servlet> <servlet-name>taotao-manager</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
看到web.xml中的classpath:spring/application-*.xml时恍然大悟了。。因为我的配置文件在配置的时候名字是这样的。!看图。!
问题就是如此的蛋疼。。。因为自己的马虎,之前配的时候没有仔细看,所以导致了没有加载上applicationContext-service.xml。。。
改正后
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param>
无报错信息。。。。。。。。
之前看到网上许多人报这个错误的原因多数都是没有进行对应逻辑层与配置文件的包扫描,比如Service层里对应的applicationContext-service.xml里应该配置component-scan的包扫描。
结论:
遇到异常问题的时候,先看报错信息,一步一步分析,看看到底是代码写的有问题像这种情况一样,想一想问题的根本之源在哪里。。。相信有逻辑性的去判断解决一个问题,那也仅仅是时间问题吧!……..
——————————————————本文为了记录一下现在的解决思路。。。给以后的自己看!~
- BeanCreationException:Injection of autowired dependencies failed异常的解决思路
- BeanCreationException:Injection of autowired dependencies failed异常的解决思路
- Spring Boot异常:BeanCreationException:Injection of autowired dependencies failed;
- Spring Boot异常:BeanCreationException:Injection of autowired dependencies failed;
- Injection of autowired dependencies failed
- Injection of autowired dependencies failed;
- Injection of autowired dependencies failed
- Injection of autowired dependencies failed
- Spring+MyBatis整合开发时 Injection of autowired dependencies failed错误的解决
- error:Injection of autowired dependencies failed
- 异常:Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentExceptio
- Java-4-解决报错:Error creating bean with name 'globalAppObj': Injection of autowired dependencies failed
- Error creating bean with name '****': Injection of autowired dependencies failed
- ava 注解 出错 Injection of autowired dependencies failed
- tomcat启动报错:Injection of autowired dependencies failed
- debug报错injection of autowired dependencies failed
- Injection of autowired dependencies failed ( mybatis进行数据处理的四种方式 )
- Injection of resource dependencies failed
- 前端性能优化,前端开发注意的小细节
- poj3253——Fence Repair(哈夫曼)
- 作为it从业人员,如何写好一份可堪入目的简历?
- linux常用命令
- CSU-1111
- BeanCreationException:Injection of autowired dependencies failed异常的解决思路
- Windows10下使用Virtual Box一启动虚拟机就蓝屏(错误代码SYSTEM_SERVICE_EXCEPTION)解决方案
- 螺旋方阵(递归)
- JS判断上传文件类型
- ArcEngine HRESULT:0x80040215
- mysql常用操作
- 【Python】urllib2禁止302_301跳转+源码分析
- linux——虚拟机扩容
- CSU-1112