§1 什么是自动装配?

Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。

简而言之,就是对于bean当中引用的其他bean不需要我们自己去配置它改使用哪个类,Spring 的自动装配可以帮助我们完成这些工作。

§2 自动装配的意义?
引用
理解自动装配的优缺点是很重要的。其中优点包括:

自动装配能显著减少配置的数量。不过,采用bean模板(见这里)也可以达到同样的目的。

自动装配可以使配置与java代码同步更新。例如,如果你需要给一个java类增加一个依赖,那么该依赖将被自动实现而不需要修改配置。因此强烈推荐在开发过程中采用自动装配,而在系统趋于稳定的时候改为显式装配的方式。
§3 自动装配有几种类型?

 
5种模式 说明 

通过配置default-autowire 属性,Spring IOC 容器可以自动为程序注入bean;默认是no,不启用自动装配;
default-autowire 的类型有byName,byType,constructor;
byName:通过名称进行自动匹配;
byType:根据类型进行自动匹配;
constructor:和byType 类似,只不过它是根据构造方法注入而言的,根据类型,自动注入;

byName 根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自
动装配。例如,在bean定义中将 autowire设置为by name,而该bean包含master属性(同时提供
setMaster(..)方法),Spring就会查找名为master的bean定义,并用它来装配给master属性。

byType 如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的
bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,
则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置
dependency-check="objects"让Spring抛出异常。

constructor 与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类
型一致的bean,那么将会抛出异常。

autodetect 通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。
如果发现默认的构造器,那么将使用byType方式。

建议:自动装配机制慎用,它屏蔽了装配细节,容易产生潜在的错误;

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd"
  6. default-autowire="byName">
  7. <bean id="car2" class="com.zhiqi.model.Car">
  8. <property name="id" value="2007"></property>
  9. <property name="carName" value="奥迪"></property>
  10. </bean>
  11. <!-- 自动装配 byName 来找car -->
  12. <bean id="car" class="com.zhiqi.model.Car">
  13. <property name="id" value="2009"></property>
  14. <property name="carName" value="奥拓"></property>
  15. </bean>
  16. <bean id="employee" class="com.zhiqi.model.Employee">
  17. <property name="id" value="10080"></property>
  18. <property name="name" value="贾经理"></property>
  19. <property name="sex" value="男"></property>
  20. </bean>
  21. </beans>
  1. public class Car {
  2. private int id;
  3. private String carName;
  4. public int getId() {
  5. return id;
  6. }
  7. public void setId(int id) {
  8. this.id = id;
  9. }
  10. public String getCarName() {
  11. return carName;
  12. }
  13. public void setCarName(String carName) {
  14. this.carName = carName;
  15. }
  16. }
  1. public class Employee {
  2. private int id;
  3. private String name;
  4. private String sex;
  5. private Car car;
  6. public Employee() {
  7. super();
  8. // TODO Auto-generated constructor stub
  9. }
  10. public Employee(int id, String name, String sex) {
  11. super();
  12. this.id = id;
  13. this.name = name;
  14. this.sex = sex;
  15. }
  16. public int getId() {
  17. return id;
  18. }
  19. public void setId(int id) {
  20. this.id = id;
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. public String getSex() {
  29. return sex;
  30. }
  31. public void setSex(String sex) {
  32. this.sex = sex;
  33. }
  34. public Car getCar() {
  35. return car;
  36. }
  37. public void setCar(Car car) {
  38. this.car = car;
  39. }
  40. }

public class MyTest {

  1. public static void main(String[] args) {
  2. ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
  3. Employee employee=(Employee)ac.getBean("employee");
  4. //employee.setName("李经理");//在xml中属性注入
  5. System.out.println(employee.getCar().getCarName());
  6. }
  7. }

【byType】

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd"
  6. default-autowire="byType">
  7. <!-- 自动装配 byType 有两个相同类型会报错 -->
  8. <bean id="car2" class="com.zhiqi.model.Car">
  9. <property name="id" value="2007"></property>
  10. <property name="carName" value="奥迪"></property>
  11. </bean>
  12. <!-- 自动装配 byType 来找car -->
  13. <bean id="car" class="com.zhiqi.model.Car">
  14. <property name="id" value="2009"></property>
  15. <property name="carName" value="奥拓"></property>
  16. </bean>
  17. <bean id="employee" class="com.zhiqi.model.Employee">
  18. <property name="id" value="10080"></property>
  19. <property name="name" value="贾经理"></property>
  20. <property name="sex" value="男"></property>
  21. </bean>
  22. </beans>

测试:

  1. public class MyTest {
  2. public static void main(String[] args) {
  3. ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
  4. Employee employee=(Employee)ac.getBean("employee");
  5. //employee.setName("李经理");//在xml中属性注入
  6. System.out.println(employee.getCar().getCarName());
  7. }
  8. }

运行:

【default-autowire="constructor"】

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd"
  6. default-autowire="constructor">
  7. <!-- 自动装配 constructor 需要写单参构造方法 -->
  8. <bean id="car3" class="com.zhiqi.model.Car">
  9. <property name="id" value="2007"></property>
  10. <property name="carName" value="奥迪"></property>
  11. </bean>
  12. <bean id="employee" class="com.zhiqi.model.Employee">
  13. <property name="id" value="10080"></property>
  14. <property name="name" value="贾经理"></property>
  15. <property name="sex" value="男"></property>
  16. </bean>
  17. </beans>

    1. 自动装配 constructor 需要写单参构造方法

转载自:

Spring4自动装配(default-autowire)的更多相关文章

  1. Spring笔记04(DI(给属性赋值),自动装配(autowire))

    给不同数据类型注入值: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  2. Spring4自动装配(default-autowire) (转)

    原文地址:http://blog.csdn.net/conglinyu/article/details/63684957 Spring 自动装配 通过配置default-autowire 属性,Spr ...

  3. [转载]Spring Autowire自动装配介绍

    转自: http://www.cnblogs.com/zhishan/p/3190757.html 在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型 ...

  4. Spring Autowire自动装配介绍

    在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...

  5. Spring Autowire自动装配

    在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...

  6. Spring学习记录(三)---bean自动装配autowire

    Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref autowire: no ---默认情况,不自动装配,通过ref手动引用 byName---根据 ...

  7. Spring 自动装配及其注解

    一.属性自动装配 首先,准备三个类,分别是User,Cat,Dog.其中User属性拥有Cat和Dog对象. package com.hdu.autowire; public class User { ...

  8. spring的自动装配(default-autowire="byName")

    自动装配,官方给出的定义是这样:Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系.因此,如果可能的话,可以自 动让Spring通过检查BeanFactory中的内 ...

  9. Spring(三)之自动装配、表达式

    自动装配 自动装配(autowire)协作者 Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系.因此,如果可能的话,可以自动让Spring通过检查BeanFact ...

随机推荐

  1. Vue2入门路线及资源

    前言:最近在学习Vue,感觉对vue+vuex+vue-router算是小小地入门了.想起前期最苦恼也是最费时的事,就是在每个阶段找到合适当前水平的资源或者demo,所以本文我根据我自己的体验,整理了 ...

  2. ubuntu下使用python3的有些库时,解决"raise ImportError(str(msg) + ', please install the python3-tk package') ImportError: No module named '_tkinter', please install the python3-tk package"的错误

    问题: 在Ubuntu下使用matplotlib这个库时,运行时出现如下错误: raise ImportError(str(msg) + ', please install the python3-t ...

  3. Atitit.播放系统的选片服务器,包厢记时系统 的说明,教程,维护,故障排查手册p825

    Atitit.播放系统的选片服务器,包厢记时系统 的说明,教程,维护,故障排查手册p825 1. 播放系统服务器方面的维护2 1.1. 默认情况下,已经在系统的启动目录下增加了俩个启动项目2 1.2. ...

  4. web 学习资源

    学习 https://skills.bugbank.cn/ https://github.com/JnuSimba/MiscSecNotes 靶场 http://skysec.top/2018/01/ ...

  5. MYSQL 外键 on语句 多表查询

    外键约束 创建外键 --- 每一个班主任会对应多个学生 , 而每个学生只能对应一个班主任 ----主表 CREATE TABLE ClassCharger( id TINYINT PRIMARY KE ...

  6. winform listbox 使用DrawMode使用OwnerDrawVarialbe或OwnerDrawFixed无水平滚动条

    因为需要使用DrawMode自行DrawItem,所以需要将DrawMode设置为OwnerDrawVarialbe或OwnerDrawFixed模式,代码如下: private void listB ...

  7. leetCode题解之求二叉树每层的平均值

    1.题目描述 Given a non-empty binary tree, return the average value of the nodes on each level in the for ...

  8. ASP.NET中母版页引用外部js或css文件无效,提示对象未定义解决方法

    最近做网站用了一个js+css实现的带有二级菜单的导航条,在母版页创建好后,子页面调用出现了许多奇怪的问题,多方查证后的最终解决方案和大家分享下.... 1.路径问题 如果是一个单独的aspx页面调用 ...

  9. Oracle存储过程_语法

    create or replace procedure procedure_name --存储过程名字 ( --进行输入/输出的量 量_name in out 量_类型 --e.g. username ...

  10. Linux查看磁盘读写

    ---------- 查看磁盘读写---------iostat -k 1 SQL> ho iostatLinux 2.6.32-279.el6.x86_64 (server-92)  08/1 ...