Spring抛出异常_自动装配

Spring自动装配(autowire)出错

报错如下:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘person‘ defined in class path resource [appContext.xml]: Unsatisfied dependency expressed through bean property ‘axe‘: : No qualifying bean of type [com.itheima.app.Axe] is defined: expected single matching bean but found 2: steelAxe,stoneAxe; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.itheima.app.Axe] is defined: expected single matching bean but found 2: steelAxe,stoneAxe
...
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.itheima.app.Axe] is defined: expected single matching bean but found 2: steelAxe,stoneAxe
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1126)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1292)
... 36 more

分析:

Caused by 这一行,提示我们未限定Axe类型(注:Axe是一个接口)的bean,发现两个实现bean都匹配Axe类型.

Spring的XML配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >

    <!-- <bean id="person" class="com.itheima.app.impl.Chinese" >
        <property name="axe" ref="steelAxe"></property>
        <property name="dog" ref="gunDog"></property>
    </bean> -->

    <bean id="person" class="com.itheima.app.impl.Chinese" autowire="byType"> <!-- 此处使用byType策略进行自动装配 -->
    </bean>
      <!-- 配置stellAxe Bean, 其实现类实现了Axe接口 -->
    <bean id="steelAxe" class="com.itheima.app.impl.SteelAxe" ></bean>
    <!-- 配置stoneAxe Bean, 其实现类也实现了Axe接口 -->
    <bean id="stoneAxe" class="com.itheima.app.impl.StoneAxe" ></bean>

    <bean id="gunDog" class="com.itheima.app.impl.GunDog" >
        <property name="name" value="wangwang"></property>
    </bean>
</beans>

原因:

autowire="byType" 说明了是根据setter方法参数类型和bean的类型进行匹配.
  <!-- 配置stellAxe Bean, 其实现类实现了Axe接口 -->
    <bean id="steelAxe" class="com.itheima.app.impl.SteelAxe" ></bean>
    <!-- 配置stoneAxe Bean, 其实现类也实现了Axe接口 -->
    <bean id="stoneAxe" class="com.itheima.app.impl.StoneAxe" ></bean>

看注释解释,因为容器中有两个类型为Axe的Bean,Spring无法确定应为person Bean注入那个Bean,所以程序抛出异常.

解决:

使用ref显式指定依赖,显式指定的依赖覆盖自动装配依赖

<bean id="person" class="com.itheima.app.impl.Chinese" autowire="byType">
  <property name="axe" ref="steelAxe"></property>
</bean>

时间: 2024-08-08 05:39:10

Spring抛出异常_自动装配的相关文章

Spring(六)之自动装配

一.自动装配模型 下面是自动连接模式,可以用来指示Spring容器使用自动连接进行依赖注入.您可以使用元素的autowire属性为bean定义指定autowire模式. 可以使用 byType 或者 constructor 自动装配模式来连接数组和其他类型的集合. 自动装配的局限性 当自动装配始终在同一个项目中使用时,它的效果最好.如果通常不使用自动装配,它可能会使开发人员混淆的使用它来连接只有一个或两个 bean 定义.不过,自动装配可以显著减少需要指定的属性或构造器参数,但你应该在使用它们之

Spring IOC容器-自动装配

1 autowire="byName" 根据名称自动装配,自动去IOC容器中找与属性名同名的引用的对象,并自动注入. <!-- ###############自动装配############### --> <bean id="userDao" class="d_auto.UserDao"></bean> <bean id="userService" class="d_auto

Spring 源码(九)@Autowired注解实现原理(Spring Bean的自动装配

@Autowired注解的实现过程,其实就是Spring Bean的自动装配过程.通过看@Autowired源码注释部分我们可以看到@Autowired的实现是通过AutowiredAnnotationBeanPostProcessor后置处理器中实现的. AutowiredAnnotationBeanPostProcessor 类图 PriorityOrdered:确认 AutowiredAnnotationBeanPostProcessor 后置处理器的执行优先级 BeanFactoryAw

Spring 源码(九)@Autowired注解实现原理(Spring Bean的自动装配)

@Autowired注解的实现过程,其实就是Spring Bean的自动装配过程.通过看@Autowired源码注释部分我们可以看到@Autowired的实现是通过AutowiredAnnotationBeanPostProcessor后置处理器中实现的. AutowiredAnnotationBeanPostProcessor 类图 PriorityOrdered:确认 AutowiredAnnotationBeanPostProcessor 后置处理器的执行优先级BeanFactoryAwa

Spring框架之自动装配

Spring的IoC容器通过Java反射机制了解了容器中所存在Bean的配置信息,这包括构造方法的结构,属性的信息,而正是由于这个原因,Spring容器才能通过某种规则来对Bean进行自动装配,而无须通过显式的方法进行配置. 一.自动装配类型:Spring IoC容器可以自动装配相互协作Bean之间的关联关系.因此,可以自动使Spring通过检查BeanFactory中的内容,来指定Bean协作(其它被依赖的Bean),,下面来介绍这4种类型: 1.byName类型:根据属性名自动装配.此类型将

Spring中类型自动装配--byType

在Spring中,"类型自动装配"的意思是如果一个bean的数据类型与其它bean属性的数据类型相同,将自动兼容装配它. 例如,一个"persion" bean 公开以"ability"类数据类型作为属性,Spring会找到ability类相同的数据类型,并自动装配它的Bean.如果没有匹配找到,它什么也不做. package auto_w; /** * Created by luozhitao on 2017/8/8. */ public cl

Spring bean的自动装配属性

bean的自动装配属性能简化xml文件配置. bean 的自动装配属性分为四种: 1.byName 2.byTyoe 3.constructor 4. autodetect byName: 它查找配置文件中的的bean的id 或者name 和本bean中的成员属性名相同的bean 自动装配 所以不用再给本bean添加peoperty标签 例:有两个类 public Class  Person{ } public Class Customer{ private Person p; public s

Spring中的自动装配案例分析

Spring_Autowiring collaborators 在Spring3.2.2中自动装配类型,分别为:no(default)(不采用自动装配).byName,byType,constructor下面来分别介绍一下这些是如何自动装配的 <bean id="foo" class="...Foo" autowire="autowire type"> Mode Explanation no (Default) No autowiri

Spring框架学习(五):Spring @Autowired实现自动装配

学习自动装配之前,讲一个概念:Component,即组件.组件你也可以理解为bean对象,只不过通常Component的组成会稍微复杂一些,比如,一个组件里面会引用一个或多个别的bean对象,组件的定义方式也不一样.自动装配貌似就是为组件而生的. 自动装配(Autowired)这个概念,如果你已经使用过SpringMVC或者SpringBoot做开发,你会发现@Autowired注解是特别经常使用到的.比如你的Controller.Service相关的类里面就会经常用到.如果你看过@Contro