Spring4自动装配(default-autowire)
§1 什么是自动装配?
简而言之,就是对于bean当中引用的其他bean不需要我们自己去配置它改使用哪个类,Spring 的自动装配可以帮助我们完成这些工作。
§2 自动装配的意义?
引用
理解自动装配的优缺点是很重要的。其中优点包括:
自动装配能显著减少配置的数量。不过,采用bean模板(见这里)也可以达到同样的目的。
自动装配可以使配置与java代码同步更新。例如,如果你需要给一个java类增加一个依赖,那么该依赖将被自动实现而不需要修改配置。因此强烈推荐在开发过程中采用自动装配,而在系统趋于稳定的时候改为显式装配的方式。
§3 自动装配有几种类型?
通过配置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方式。
建议:自动装配机制慎用,它屏蔽了装配细节,容易产生潜在的错误;
- <?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"
- default-autowire="byName">
- <bean id="car2" class="com.zhiqi.model.Car">
- <property name="id" value="2007"></property>
- <property name="carName" value="奥迪"></property>
- </bean>
- <!-- 自动装配 byName 来找car -->
- <bean id="car" class="com.zhiqi.model.Car">
- <property name="id" value="2009"></property>
- <property name="carName" value="奥拓"></property>
- </bean>
- <bean id="employee" class="com.zhiqi.model.Employee">
- <property name="id" value="10080"></property>
- <property name="name" value="贾经理"></property>
- <property name="sex" value="男"></property>
- </bean>
- </beans>
- public class Car {
- private int id;
- private String carName;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getCarName() {
- return carName;
- }
- public void setCarName(String carName) {
- this.carName = carName;
- }
- }
- public class Employee {
- private int id;
- private String name;
- private String sex;
- private Car car;
- public Employee() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Employee(int id, String name, String sex) {
- super();
- this.id = id;
- this.name = name;
- this.sex = sex;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public Car getCar() {
- return car;
- }
- public void setCar(Car car) {
- this.car = car;
- }
- }
public class MyTest {
- public static void main(String[] args) {
- ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
- Employee employee=(Employee)ac.getBean("employee");
- //employee.setName("李经理");//在xml中属性注入
- System.out.println(employee.getCar().getCarName());
- }
- }
【byType】
- <?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"
- default-autowire="byType">
- <!-- 自动装配 byType 有两个相同类型会报错 -->
- <bean id="car2" class="com.zhiqi.model.Car">
- <property name="id" value="2007"></property>
- <property name="carName" value="奥迪"></property>
- </bean>
- <!-- 自动装配 byType 来找car -->
- <bean id="car" class="com.zhiqi.model.Car">
- <property name="id" value="2009"></property>
- <property name="carName" value="奥拓"></property>
- </bean>
- <bean id="employee" class="com.zhiqi.model.Employee">
- <property name="id" value="10080"></property>
- <property name="name" value="贾经理"></property>
- <property name="sex" value="男"></property>
- </bean>
- </beans>
测试:
- public class MyTest {
- public static void main(String[] args) {
- ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
- Employee employee=(Employee)ac.getBean("employee");
- //employee.setName("李经理");//在xml中属性注入
- System.out.println(employee.getCar().getCarName());
- }
- }
运行:
【default-autowire="constructor"】
- <?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"
- default-autowire="constructor">
- <!-- 自动装配 constructor 需要写单参构造方法 -->
- <bean id="car3" class="com.zhiqi.model.Car">
- <property name="id" value="2007"></property>
- <property name="carName" value="奥迪"></property>
- </bean>
- <bean id="employee" class="com.zhiqi.model.Employee">
- <property name="id" value="10080"></property>
- <property name="name" value="贾经理"></property>
- <property name="sex" value="男"></property>
- </bean>
- </beans>
- 自动装配 constructor 需要写单参构造方法
转载自:
Spring4自动装配(default-autowire)的更多相关文章
- Spring笔记04(DI(给属性赋值),自动装配(autowire))
给不同数据类型注入值: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...
- Spring4自动装配(default-autowire) (转)
原文地址:http://blog.csdn.net/conglinyu/article/details/63684957 Spring 自动装配 通过配置default-autowire 属性,Spr ...
- [转载]Spring Autowire自动装配介绍
转自: http://www.cnblogs.com/zhishan/p/3190757.html 在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型 ...
- Spring Autowire自动装配介绍
在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...
- Spring Autowire自动装配
在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...
- Spring学习记录(三)---bean自动装配autowire
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref autowire: no ---默认情况,不自动装配,通过ref手动引用 byName---根据 ...
- Spring 自动装配及其注解
一.属性自动装配 首先,准备三个类,分别是User,Cat,Dog.其中User属性拥有Cat和Dog对象. package com.hdu.autowire; public class User { ...
- spring的自动装配(default-autowire="byName")
自动装配,官方给出的定义是这样:Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系.因此,如果可能的话,可以自 动让Spring通过检查BeanFactory中的内 ...
- Spring(三)之自动装配、表达式
自动装配 自动装配(autowire)协作者 Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系.因此,如果可能的话,可以自动让Spring通过检查BeanFact ...
随机推荐
- Python Django Ajax 传递列表数据
function getTableContent(node) { event.preventDefault(); var tr = node.parentNode.parentNode; var id ...
- [js样式效果]具有停顿效果上下滚动方式
一般用于公告的滚动效果 <!DOCTYPE HTML> <html> <head> <meta charset="gb2312" /> ...
- java.lang.IllegalStateException: Mapped class was not specified
错误如下:java.lang.IllegalStateException: Mapped class was not specifiedat org.springframework.util.Asse ...
- 玩转Android拍摄功能
简单拍照与摄像 在富媒体开始流行之前,整个世界是一个灰暗且平淡无奇的地方.还记得Gopher吗?我或许不记得了.自从APP成为用户生活的一部分之后,这便给他们提供了一种方式可以来存放他们生活的细节.使 ...
- android之画板功能之橡皮擦 画笔大小和画笔颜色
第一展示设置画笔颜色的功能,第二展示设置画笔大小的颜色,而第三则展示橡皮擦的功能,这节将图标颜色设置为了蓝色,并且,增加了最左边的按钮(其实,就是在gridview中多增加了一个item). 下面分别 ...
- Surface电池阈值
Surface电池阈值 笔记本电脑一般都会提供一个电池保养的软件,其主要最用是让电池在插电情况下保持在50%-80%之间,以延长电池寿命,减少电池损耗.而微软自家的Surface却一直没有这个设置. ...
- Mac下驱动BCM20702A0 USB蓝牙
偶然高了一个USB蓝牙,查到Mac下能识别,无法驱动,就去找了一下.方法很简单,就是把蓝牙的PID和VID加入到驱动里边去,具体方法和步骤如下: 1. 进入到/System/Libary/Extens ...
- 从AggregateException看异常类的设计
异常是程序在有bug时最直观的表现形式,不担心有bug存在,而担心bug埋没在大堆的代码中而发现不了. 这篇随笔简单谈谈从AggregateException类源码(http://www.projky ...
- etcd 分布式数据库概念初探
Lease(租约): 其实就是一个定时器.首先申请一个TTL=N的lease(定时器),然后创建key的时候传入该lease,那么就实现了一个定时的key. 在程序中可以定时为该lease续约,也就是 ...
- 使用Instruments中的CoreAnimation分析动画
使用Instruments中的CoreAnimation分析动画 1. 打开Instruments中的CoreAnimation 2. 运行前的准备工作 要注意勾选以下选项,便于调试 3. 运行与调试 ...