论坛首页 Java企业应用论坛

Spring自动绑定技术总结

浏览 2724 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-03-09  
1.概念介绍:这些概念可以参考开发参考手册,可以访问:http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch03s03.html#beans-factory-autowire
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。由于autowire可以针对单个bean进行设置,因此可以让有些bean使用 autowire,有些bean不采用。autowire的方便之处在减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥! 在xml配置文件中,autowire一共有五种类型,可以在<bean/>元素中使用autowire属性指定:
a.no
不使用自动装配。必须通过ref元素指定依赖,这是默认设置。由于显式指定协作者可以使配置更灵活、更清晰,因此对于较大的部署配置,推荐采用该设置。而且在某种程度上,它也是系统架构的一种文档形式。
b.byName
根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。例如,在bean定义中将autowire设置为by name,而该bean包含master属性(同时提供setMaster(..)方法),Spring就会查找名为master的bean定义,并用它来装配给master属性。
c.byType
如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置dependency-check="objects"让Spring抛出异常。
d.constructor
与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。
e.autodetect
通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式。
2.举例:
这里一个Shape 类依赖一个Circle 类,模拟bean之间的依赖关系,并通过设定autowire属性来自动绑定协助者。
a. Shape.java
package com.cn.autowiring;

public class Shape {
	private Circle circle;

	public Shape() {

	}

	public Shape(Circle circle) {
		this.circle = circle;
	}

	public void setCircle(Circle circle) {
		this.circle = circle;
	}

	public Circle getCircle() {
		return circle;
	}

}

b.Circle.java
package com.cn.autowiring;

public class Circle {
	
	private String center;
	
	private int radio;

	public String getCenter() {
		return center;
	}

	public void setCenter(String center) {
		this.center = center;
	}

	public int getRadio() {
		return radio;
	}

	public void setRadio(int radio) {
		this.radio = radio;
	}
	
}

c.shape-beans.xml
<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

	<bean id="noAutoWiring" class="com.cn.autowiring.Shape"
		autowire="no">
		<property name="circle" ref="circle"></property>
	</bean>

	<bean id="getCircleByName" class="com.cn.autowiring.Shape"
		autowire="byName">
	</bean>

	<bean id="getCircleByType" class="com.cn.autowiring.Shape"
		autowire="byType">
	</bean>

	<bean id="getCircleCst" class="com.cn.autowiring.Shape"
		autowire="constructor">
	</bean>

	<bean id="getCircleAdt" class="com.cn.autowiring.Shape"
		autowire="autodetect">
	</bean>

	<bean id="circle" class="com.cn.autowiring.Circle">
		<property name="center" value="center"></property>
		<property name="radio" value="10"></property>
	</bean>


</beans>

d.测试
package com.cn.autowiring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAutoWiring {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"shape-beans.xml");

		Shape shape = (Shape) ctx.getBean("noAutoWiring");
		System.out.println("noAutoWiring : " + shape.getCircle().getCenter()
				+ " " + shape.getCircle().getRadio());

		Shape shape1 = (Shape) ctx.getBean("getCircleByName");
		System.out.println("getCircleByName: " + shape1.getCircle().getCenter()
				+ " " + shape1.getCircle().getRadio());

		Shape shape2 = (Shape) ctx.getBean("getCircleByType");
		System.out.println("getCircleByType: " + shape2.getCircle().getCenter()
				+ " " + shape2.getCircle().getRadio());

		Shape shape3 = (Shape) ctx.getBean("getCircleCst");
		System.out.println("getCircleCst: " + shape3.getCircle().getCenter()
				+ " " + shape3.getCircle().getRadio());

		Shape shape4 = (Shape) ctx.getBean("getCircleAdt");
		System.out.println("getCircleAdt: " + shape4.getCircle().getCenter()
				+ " " + shape4.getCircle().getRadio());

	}
}

e.运行结果:
引用
noAutoWiring : center 10
getCircleByName: center 10
getCircleByType: center 10
getCircleCst: center 10
getCircleAdt: center 10
   发表时间:2009-03-10  
这个功能是弥补Spring的默认转化不足或者是更佳精确邦定
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics