javaBean:
package
ch4_autowire;
public class Foo ... {
}
package ch4_autowire;
public class Bar ... {
}
public class Foo ... {
}
package ch4_autowire;
public class Bar ... {
}
配置文件:
<?
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-2.0.xsd" >
<!-- 定义foo和bar -->
< bean id ="foo" class ="ch4_autowire.Foo" />
< bean id ="bar" class ="ch4_autowire.Bar" />
<!-- 使用ByName方式装配,这种方式的原理是如果目标bean有一个名为foo的属性而bean工厂有一个同名的bean,则把这个bean装配给目标bean的属性 -->
< bean id ="simpleTest1" class ="ch4_autowire.SimpleTest" autowire ="byName" />
<!-- 使用ByType方式装配,这种方式的原理是如果目标bean的属性类型和bean工厂中的一个bean的类型相府,则把这个bean装配给目标bean,如果对于一个相府,则出现异常 -->
< bean id ="simpleTest2" class ="ch4_autowire.SimpleTest" autowire ="byType" />
<!-- 使用constructor方式,这种方式的原理是最大数量的匹配目标bean的构造方法,如果目标bean有一个String的构造方法,一个String和int的构造方法
bean工厂中也有string和int两个bean,则spring会装配String和int的构造方法
-->
< bean id ="simpleTest3" class ="ch4_autowire.SimpleTest" autowire ="constructor" />
<!-- 自动装配方式,这种方式会自动在byType和Constructor中自动匹配,当目标bean有一个无参(本例)构造方法是,spring会选择使用byType方式 -->
< bean id ="simpleTest4" class ="ch4_autowire.SimpleTest" autowire ="autodetect" />
</ beans >
< 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-2.0.xsd" >
<!-- 定义foo和bar -->
< bean id ="foo" class ="ch4_autowire.Foo" />
< bean id ="bar" class ="ch4_autowire.Bar" />
<!-- 使用ByName方式装配,这种方式的原理是如果目标bean有一个名为foo的属性而bean工厂有一个同名的bean,则把这个bean装配给目标bean的属性 -->
< bean id ="simpleTest1" class ="ch4_autowire.SimpleTest" autowire ="byName" />
<!-- 使用ByType方式装配,这种方式的原理是如果目标bean的属性类型和bean工厂中的一个bean的类型相府,则把这个bean装配给目标bean,如果对于一个相府,则出现异常 -->
< bean id ="simpleTest2" class ="ch4_autowire.SimpleTest" autowire ="byType" />
<!-- 使用constructor方式,这种方式的原理是最大数量的匹配目标bean的构造方法,如果目标bean有一个String的构造方法,一个String和int的构造方法
bean工厂中也有string和int两个bean,则spring会装配String和int的构造方法
-->
< bean id ="simpleTest3" class ="ch4_autowire.SimpleTest" autowire ="constructor" />
<!-- 自动装配方式,这种方式会自动在byType和Constructor中自动匹配,当目标bean有一个无参(本例)构造方法是,spring会选择使用byType方式 -->
< bean id ="simpleTest4" class ="ch4_autowire.SimpleTest" autowire ="autodetect" />
</ beans >
测试代码:
package
ch4_autowire;
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;
}
}
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;
}
}
package
ch4_autowire;
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+"ch4_autowire"+File.separator+"applicationContext.xml";
XmlBeanFactory factory=new XmlBeanFactory(new FileSystemResource(realpath));
return factory;
}
}
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+"ch4_autowire"+File.separator+"applicationContext.xml";
XmlBeanFactory factory=new XmlBeanFactory(new FileSystemResource(realpath));
return factory;
}
}
Spring自动装配是默认关闭的,项目中也不推荐使用,主要是因为太不可控制了,如果class多且关系繁琐的情况下