关于BeanCreationException:Injection of autowired dependencies failed异常的解决思路

今天做项目时,遇到了一个异常。感到非常头大。运用了百度编程,上百度查了一上午。还是没有解决。中午吃完饭后,回到电脑前,又上网查了下。突然搜到了一位博主的博客,突然眼前一亮。解决了自己的问题。现在让我来给大家分享分享吧。我现在进行下转载!
以下为大佬的博客:
下面记录的是今天的解决问题的思路与方法。 先来看一下我的异常问题的截图
关于BeanCreationException:Injection of autowired dependencies failed异常的解决思路
具体异常代码如下

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的实现类

@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private ItemMapper itemMapper;
}

Controller层

@Controller
public class ItemController {
	@Autowired
	private ItemService itemService;
}

通过上面的@Service(来自SpringMvc的注解)来实现Service层的实现类注入到Spring容器中。而此时这里的@Autowired的DAO层接口的代理对象并没有报错,当时在网上查找了一番,以为是因为DAO层没有用@Compent或者@Repository这样的注解造成的错误,后来思考了一下,发现因为是用Mapper映射做的DAO层接口的实现类,所以完全没有必要去做这个注解,那么问题也就与DAO层无关了。 而@Controller这里的注解是没有报错的。。也就是说我在spring-mvc.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时恍然大悟了。。因为我的配置文件在配置的时候名字是这样的。!

看图。!

关于BeanCreationException:Injection of autowired dependencies failed异常的解决思路


问题就是如此的蛋疼。。。因为自己的马虎,之前配的时候没有仔细看,所以导致了没有加载上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的包扫描。 结论: 遇到异常问题的时候,先看报错信息,一步一步分析,看看到底是代码写的有问题像这种情况一样,想一想问题的根本之源在哪里。。。相信有逻辑性的去判断解决一个问题,那也仅仅是时间问题吧!… ------------------------------------------------------本文为了记录一下现在的解决思路。。。给以后的自己看!~
作者:sy_y
来源:****
原文:https://blog.****.net/s740556472/article/details/54974074
版权声明:本文为博主原创文章,转载请附上博文链接!