原文地址:http://blog.csdn.net/conglinyu/article/details/63684957

Spring 自动装配

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

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

【byName】

  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. }

测试:

  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. }

【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. Spring4自动装配(default-autowire)

    §1 什么是自动装配? Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系.因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我 ...

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

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

  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. Ubuntu下VirtualBox虚拟机与主机机互相ping通的简单配置心得

    virtualbox 是个非常优秀的软件,通常与主机之间有3种方式,Bridged方式,NAT方式,host-only方式,安装的默认方式是NAT,可是这样的方式在virtualbox虚拟机下有个缺点 ...

  2. Disruptor 创建过程

    1 Disruptor disruptor = new Disruptor<ValueEvent>(ValueEvent.EVENT_FACTORY, ringBufferSize, ex ...

  3. ASP.NET动态网站制作(14)-- CSS3

    前言:这节课主要讲解CSS之前没有讲到过的知识点以及CSS3的一些内容. 内容: 1.内容参考博文:http://www.cnblogs.com/ruanmou/p/4832214.html. 后记: ...

  4. Lumen开发:lumen源码解读之初始化(5)——注册(register)与启动(boot)

    版权声明:本文为博主原创文章,未经博主允许不得转载. register()是在服务容器注册服务, bootstrap/app.php /** * 注册外部服务 */ $app->register ...

  5. 九度OJ 1250:矩阵变换 (矩阵运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:95 解决:47 题目描述: 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右)某一个元素也加一, 现给出一正数矩 ...

  6. js跨浏览器复制: ZeroClipboard

    实例结构: demo.html <script type="text/javascript" src='http://code.jquery.com/jquery.js'&g ...

  7. [note]最近公共祖先

    最近公共祖先(LCA)https://www.luogu.org/problemnew/show/P3379 #define RG register #include<cstdio> #i ...

  8. 三款查看class文件结构的工具

    classpy JavaClassViewer jclasslib

  9. Java语言实现简单FTP软件------>连接管理模块的实现:主机与服务器之间的连接与关闭操作(八)

    (1)FTP连接 运行FTP客户端后,首先是连接FTP服务器,需要输入FTP服务器的IP地址及用户名.密码以及端口号后点击连接按钮开始连接FTP服务器,连接流程图如下图所示. 点击"连接&q ...

  10. activiti踩坑

    最近在学习activiti,偶然间遇到一个错误:加载引擎的时候报错,显示空指针错误,跟代码发现初始化配置文件返回为null.几经排查,可能是因为我发布流程后又清空了数据库数据导致的.然后我把表全部删除 ...