Spring之XML 配置里Bean的自动装配

Spring IOC 容器可以自动装配 Bean, 需要做的仅仅是在 <bean> 的 autowire 属性里指定自动装配的模式。

自动装配模式

1. byType(根据类型自动装配):若 IOC 容器中有多个与目标 Bean 类型一致的 Bean。 在这种情况下,Spring 将无法判定哪个 Bean 最合适该属性,所以不能执行自动装配。

2. byName(根据名称自动装配):必须将目标 Bean 的名称和属性名设置的完全相同。

3. constructor(通过构造器自动装配): 当 Bean 中存在多个构造器时。此种自动装配方式将会很复杂,不推荐使用。

自动装配的缺点

1. 在 Bean 配置文件里设置 autowire 属性进行自动装配将会装配 Bean 的所有属性。然而,若只希望装配个别属性时,autowire 属性就不够灵活了。

2. autowire 属性要么根据类型自动装配,要么根据名称自动装配,不能两者兼而有之。

一般情况下,在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些。

示例

1. 添加模型类

package xyz.huning.spring4.di.xml.beancfg.autowire;

public class Battery {

	private int id;
	
	private String type;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Override
	public String toString() {
		return "Battery [id=" + id + ", type=" + type + "]";
	}
	
}
package xyz.huning.spring4.di.xml.beancfg.autowire;

public class Screen {

    private int id;
	
	private int size;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	@Override
	public String toString() {
		return "Screen [id=" + id + ", size=" + size + "]";
	}

}
package xyz.huning.spring4.di.xml.beancfg.autowire;

public class Cellphone {

	private int id;
	
	private Battery battery;
	
	private Screen screen;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Battery getBattery() {
		return battery;
	}

	public void setBattery(Battery battery) {
		this.battery = battery;
	}

	public Screen getScreen() {
		return screen;
	}

	public void setScreen(Screen screen) {
		this.screen = screen;
	}

	@Override
	public String toString() {
		return "Cellphone [id=" + id + ", battery=" + battery + ", screen="
				+ screen + "]";
	}
	
}

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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="battery" class="xyz.huning.spring4.di.xml.beancfg.autowire.Battery"
		  p:id="1" p:type="high"></bean>
	
	<bean id="screen1" class="xyz.huning.spring4.di.xml.beancfg.autowire.Screen"
		  p:id="1" p:size="9"></bean>
	
	<!--手动配置bean属性-->
	<bean id="cellphone1" class="xyz.huning.spring4.di.xml.beancfg.autowire.Cellphone"
		  p:id="1" p:battery-ref="battery" p:screen-ref="screen1"></bean>
	
	<!--使用byName方式自动装配bean属性-->
	<bean id="cellphone2" class="xyz.huning.spring4.di.xml.beancfg.autowire.Cellphone"
		  p:id="2" autowire="byName"></bean>
	
	<!--使用byType方式自动装配bean属性-->
	<bean id="cellphone3" class="xyz.huning.spring4.di.xml.beancfg.autowire.Cellphone"
		  p:id="3" autowire="byType"></bean>
	
</beans>

3. 添加测试类

package xyz.huning.spring4.di.xml.beancfg.autowire;

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

public class Main {

	public static void main(String[] args) {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("autowire.xml");
		
		Cellphone cellphone1 = ctx.getBean("cellphone1", Cellphone.class);
		System.out.println(cellphone1);
		
		Cellphone cellphone2 = ctx.getBean("cellphone2", Cellphone.class);
		System.out.println(cellphone2);
		
		Cellphone cellphone3 = ctx.getBean("cellphone3", Cellphone.class);
		System.out.println(cellphone3);
		
		
		((ClassPathXmlApplicationContext)ctx).close();
	}
}

4. 执行结果

 

 

猜你喜欢

转载自ihuning.iteye.com/blog/2224175