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 autowiring. Bean references must be defined via a ref element. Changing the default setting is not recommended for larger deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents the structure of a system.


byName


Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name, and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master, and uses it to set the property.


byType


Allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set.


constructor


Analogous to byType, but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.

案例分析:

1、创建CumputerBean类

package www.csdn.spring.autowire.bean;

public class CumputerBean {

// 电脑名称

private String name;

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return "CumputerBean [name=" + name + "]";

}

}

2、创建DeptBean 类

package www.csdn.spring.autowire.bean;

public class DeptBean {

//部门名称

private String name;

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return "DeptBean [name=" + name + "]";

}

}

3、创建EmployeeBean

package www.csdn.spring.autowire.bean;

public class EmployeeBean {

private DeptBean deptBean;

private CumputerBean cumputerBean;

public void setDeptBean(DeptBean deptBean) {

this.deptBean = deptBean;

}

public void setCumputerBean(CumputerBean cumputerBean) {

this.cumputerBean = cumputerBean;

}

@Override

public String toString() {

return "EmployeeBean [deptBean=" + deptBean + ", cumputerBean="

+ cumputerBean + "]";

}

}

首先分析no、byName、byType的配置都是采用setter方法依赖注入实现的案例

1、no配置(通过ref=””引用需要的bean)

<?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 -->

<bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">

<property name="name" value="HP6325笔记本" />

</bean>

<!-- 部门bean -->

<bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">

<property name="name" value="CSDN教育事业部" />

</bean>

<!-- 员工bean  根据EmployeeBean中的 属性名称  去匹配-->

<bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean">

<property name="cumputerBean" ref="cumputerBean" />

<property name="deptBean" ref="deptBean" />

</bean>

</beans>

2、byName配置(分析:会根据EmployeeBean中属性的名称 自动装配)

<?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 -->

<bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">

<property name="name" value="HP6325笔记本" />

</bean>

<!-- 部门bean -->

<bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">

<property name="name" value="CSDN教育事业部" />

</bean>

<!-- 员工bean-->

<bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean" autowire="byName"/>

</beans>

3、byType配置(分析:会根据EmployeeBean中属性的类型 自动装配)

<?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 -->

<bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">

<property name="name" value="HP6325笔记本" />

</bean>

<!-- 部门bean -->

<bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">

<property name="name" value="CSDN教育事业部" />

</bean>

<!-- 员工bean  根据EmployeeBean中的 属性名称  去匹配-->

<bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean" autowire="byType"/>

</beans>

注意:当根据byType类型装配时,当在容器内找到多个匹配的类型时会出现如下bug

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘employeeBean‘ defined in class path resource [spring-byType.xml]: Unsatisfied dependency expressed through bean property ‘deptBean‘: : No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] is defined: expected single matching bean but found 2: deptBean,deptBean1; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] is defined: expected single matching bean but found 2: deptBean,deptBean1

4、Constructor(构造器参数根据byType类型匹配,自动装配)

首先修改EmployeeBean类 修改后代码如下:

package www.csdn.spring.autowire.bean;

public class EmployeeBean {

private DeptBean deptBean;

private CumputerBean cumputerBean;

public EmployeeBean(DeptBean deptBean, CumputerBean cumputerBean) {

super();

this.deptBean = deptBean;

this.cumputerBean = cumputerBean;

}

@Override

public String toString() {

return "EmployeeBean [deptBean=" + deptBean + ", cumputerBean="

+ cumputerBean + "]";

}

}

配置文件操作:

 <?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 -->

<bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">

<property name="name" value="HP6325笔记本" />

</bean>

<!-- 部门bean -->

<bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">

<property name="name" value="CSDN教育事业部" />

</bean>

<!-- 员工bean 根据EmployeeBean中的 属性名称 bytype 去匹配 -->

<bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean"

autowire="constructor">

</bean>

</beans>

说明:

1、当构造器的参数类型在容器中找不全时。

比如:

配置文件中只配置了CumpterBean时

  <?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 -->

<bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">

<property name="name" value="HP6325笔记本" />

</bean>

<!-- 员工bean 根据EmployeeBean中的 属性名称 bytype 去匹配 -->

<bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean"

autowire="constructor">

</bean>

</beans>

会出现如下bug:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘employeeBean‘ defined in class path resource [spring-constructors.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [www.csdn.spring.autowire.bean.DeptBean]: : No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

Caused by:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

2、当配置文件找到构造器参数的类型有多个的时候比如配置文件如下:

<?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 -->

<bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">

<property name="name" value="HP6325笔记本" />

</bean>

<!-- 部门bean -->

<bean id="deptBean" class="www.csdn.spring.autowire.bean.DeptBean">

<property name="name" value="CSDN教育事业部" />

</bean>

<!-- 部门bean -->

<bean id="deptBean1" class="www.csdn.spring.autowire.bean.DeptBean">

<property name="name" value="CSDN教育事业部" />

</bean>

<!-- 员工bean 根据EmployeeBean中的 属性名称 bytype 去匹配 -->

<bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean"

autowire="constructor">

</bean>

</beans>

说明:上面配置有两个同样类型的DeptBean但是不会出现bug,原因是在EmployeeBean中构造器接受的参数名称与deptBean一致。

3、当配置文件找到构造器参数的类型有多个的时候比如配置文件如下:

<?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 -->

<bean id="cumputerBean" class="www.csdn.spring.autowire.bean.CumputerBean">

<property name="name" value="HP6325笔记本" />

</bean>

<!-- 部门bean -->

<bean id="deptBean1" class="www.csdn.spring.autowire.bean.DeptBean">

<property name="name" value="CSDN教育事业部" />

</bean>

<!-- 部门bean -->

<bean id="deptBean2" class="www.csdn.spring.autowire.bean.DeptBean">

<property name="name" value="CSDN教育事业部" />

</bean>

<!-- 员工bean 根据EmployeeBean中的 属性名称 bytype 去匹配 -->

<bean id="employeeBean" class="www.csdn.spring.autowire.bean.EmployeeBean"

autowire="constructor">

</bean>

</beans>

会出现如下bug(与byType的bug一致):

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘employeeBean‘ defined in class path resource [spring-constructors.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [www.csdn.spring.autowire.bean.DeptBean]: : No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] is defined: expected single matching bean but found 2: deptBean1,deptBean2; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [www.csdn.spring.autowire.bean.DeptBean] is defined: expected single matching bean but found 2: deptBean1,deptBean2

时间: 2025-01-04 06:15:53

Spring中的自动装配案例分析的相关文章

Spring中类型自动装配--byType

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

Spring框架之自动装配

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

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 IOC容器-自动装配

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

Spring(六)之自动装配

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

Spring中Bean命名源码分析

Spring中Bean命名源码分析 一.案例代码 首先是demo的整体结构 其次是各个部分的代码,代码本身比较简单,不是我们关注的重点 配置类 /** * @Author Helius * @Create 2019-10-25-20:16 */ @Configuration @ComponentScan(basePackages = {"service"}) public class SpringConfiguration { } 接口的实现类 public interface Use

spring第一课,beans配置(中)——自动装配

bean的配置除了有手动配置,还有自动装配这一说法,具体怎么操作,如下代码事例. 自动装配可根据bean的名称与配置bean属性名称的对应进行配置,也可根据bean的类型与配置bean属性的类型进行配置. 1.自动配置 byName person类 1 package com.org.spring.beans; 2 3 public class Person { 4 private String name; 5 private int age; 6 private Car car; 7 8 @O

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