spring2.0之自动装配
Bar.java
Foo.java
SimpleTest.java
public class SimpleTest {
private Foo foo;
private Foo foo2;
private Bar bar;
public SimpleTest(){
}
public SimpleTest(Foo foo, Bar bar) {
System.out.println("constructor bar,foo call");
this.foo = foo;
this.bar = bar;
}
public SimpleTest(Foo foo) {
System.out.println("constructor foo call");
this.foo = foo;
}
public Bar getBar() {
return bar;
}
public void setBar(Bar bar) {
System.out.println("property bar set");
this.bar = bar;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
System.out.println("property foo set");
this.foo = foo;
}
public Foo getFoo2() {
return foo2;
}
public void setFoo2(Foo foo2) {
System.out.println("property foo2 set");
this.foo2 = foo2;
}
}
xml配置文件:
<beans>
<!-- 定义foo和bar -->
<bean id="foo" class="Foo"/>
<bean id="bar" class="Bar"/>
<!-- 使用ByName方式装配,这种方式的原理是如果目标bean有一个名为foo的属性而bean工厂有一个同名的bean,则把这个bean装配给目标bean的属性 Spring容器查找与SimpleTest属性名称(即foo和bar)相同的Bean,然后自动注入到该属性中,否则不会注入 -->
<bean id="simpleTest1" class="SimpleTest" autowire="byName"/>
<!-- 使用ByType方式装配,这种方式的原理是如果目标bean的属性类型和bean工厂中的一个bean的类型相府,则把这个bean装配给目标bean,如果对于一个相符,则出现异常 Spring容器查找与SimpleTest属性的设值方法参数类型(即Foo和Bar)兼容的Bean -->
<bean id="simpleTest2" class="SimpleTest" autowire="byType"/>
<!-- 使用constructor方式,这种方式的原理是最大数量的匹配目标bean的构造方法,如果目标bean有一个String的构造方法,一个String和int的构造方法 bean工厂中也有string和int两个bean,则spring会装配String和int的构造方法
Spring容器将试图找出所有与构造方法的参数类型兼容的Bean
-->
<bean id="simpleTest3" class="SimpleTest" autowire="constructor"/>
<!-- 自动装配方式,这种方式会自动在byType和Constructor中自动匹配,当目标bean有一个无参(本例)构造方法是,spring会选择使用byType方式 -->
<bean id="simpleTest4" class="SimpleTest" autowire="autodetect"/>
</beans>
测试:
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestSpring {
public static void main(String args[]) throws Exception{
//获取bean factory
BeanFactory factory=(BeanFactory)getBeanFactory();
SimpleTest simpleTest=null;
System.out.println("use byName:");
simpleTest=(SimpleTest)factory.getBean("simpleTest1");
System.out.println("use byType:");
simpleTest=(SimpleTest)factory.getBean("simpleTest2");
System.out.println("use byContructor:");
simpleTest=(SimpleTest)factory.getBean("simpleTest3");
System.out.println("use byAuto:");
simpleTest=(SimpleTest)factory.getBean("simpleTest4");
}
public static BeanDefinitionRegistry getBeanFactory() {
//获取bean factory
String realpath="";
//加载配置项
realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+File.separator+"beans.xml";
XmlBeanFactory factory=new XmlBeanFactory(new FileSystemResource(realpath));
return factory;
}
}
结果:
2008-2-29 10:26:04 org.springframework.core.CollectionFactory <clinit>
信息: JDK 1.4+ collections available
2008-2-29 10:26:04 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [D:\MyWork\AutoWire\src\beans.xml]
use byName:
property bar set
property foo set
property foo2 set
use byType:
property bar set
property foo set
property foo2 set
use byContructor:
constructor bar,foo call
use byAuto:
property bar set
property foo set
property foo2 set
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1675871