这篇文章来谈谈《Spring Framework 开发参考手册》的3.3.5小节中的自动装配协作对象和3.3.6 小节中的依赖检查。
仔细看看文档,相关配置见前面文章。
先建立一个包:javamxj.spring.basic.autowiring ,然后把以下4个文件放在这个包下。
· HelloBean,一个简单的Bean,含有一个属性hello。
HelloBean.java |
package javamxj.spring.basic.autowiring;
public class HelloBean { private String hello;
public String getHello() { return hello; }
public void setHello(String hello) { this.hello = hello; }
} |
· HelloDate类先定义了三个构造函数,然后设置了三个属性:hello、date、date2。
HelloDate.java |
package javamxj.spring.basic.autowiring;
import java.util.Date; import java.util.GregorianCalendar;
public class HelloDate {
public HelloDate() { System.out.println("defalt Constructor called"); }
public HelloDate(HelloBean hello) { System.out.println("HelloDate(HelloBean) called"); }
public HelloDate(HelloBean hello, Date date) { System.out.println("HelloDate(HelloBean,Date) called"); }
public void setHello(HelloBean hello) { System.out.println("Property hello set"); }
public void setDate(Date date) { System.out.println("Property date set"); }
public void setDate2(GregorianCalendar date) { System.out.println("Property date2 set"); }
} |
· beans.xml中定义了七个bean,
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <bean id="date" name="myDate" class="java.util.Date"/> <bean id="helloBean" class="javamxj.spring.basic.autowiring.HelloBean" dependency-check="simple"> <property name="hello" value="javamxj"/> </bean> <bean id="HelloByName" class="javamxj.spring.basic.autowiring.HelloDate" autowire="byName"/> <bean id="HelloByType" class="javamxj.spring.basic.autowiring.HelloDate" autowire="byType"/> <bean id="HelloConstructor" class="javamxj.spring.basic.autowiring.HelloDate" autowire="constructor"/> <bean id="HelloAutodetect" class="javamxj.spring.basic.autowiring.HelloDate" autowire="autodetect"/> <bean id="helloCheck" class="javamxj.spring.basic.autowiring.HelloDate" autowire="byType" dependency-check="objects"> <property name="date2" > <bean class="java.util.GregorianCalendar"/> </property> <!-- <property name="date" ref="date"/>--> <!-- <property name="hello" ref="helloBean"/>--> </bean> </beans>
Main.java |
package javamxj.spring.basic.autowiring;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource;
public class Main { public static void main(String[] args) { BeanFactory bf = new XmlBeanFactory(new ClassPathResource( "javamxj/spring/basic/autowiring/beans.xml"));
System.out.println("使用 byName:"); HelloDate hb = (HelloDate) bf.getBean("HelloByName");
System.out.println("\n使用 byType:"); hb = (HelloDate) bf.getBean("HelloByType");
System.out.println("\n使用 constructor:"); hb = (HelloDate) bf.getBean("HelloConstructor");
System.out.println("\n使用 autodetect:"); hb = (HelloDate) bf.getBean("HelloAutodetect");
System.out.println("\n使用 dependency-check:"); hb = (HelloDate) bf.getBean("helloCheck"); } } |
运行结果:
使用 byName: defalt Constructor called Property date set
使用 byType: defalt Constructor called Property date set Property hello set
使用 constructor: HelloDate(HelloBean,Date) called
使用 autodetect: defalt Constructor called Property date set Property hello set
使用 dependency-check: defalt Constructor called Property date2 set Property date set Property hello set
参考运行结果,详细说明一下beans.xml的配置:
· HelloByName指定了autowire="byName",则Spring会根据bean的名称与属性名称是否符合来进行自动绑定。如这里HelloDate含有属性:hello、date、date2,在beans.xml中包含了date、helloBean两个bean,所以只有date符合要求。(bean的名称包括Id名称和name名称)
· HelloByType指定了autowire="byType",这里HelloDate含有属性:hello、date、date2,在beans.xml中包含了Date类和HelloBean类,分别匹配date和hello属性。
· HelloConstructor指定了autowire="constructor",它会优先调用含有较多参数的构造函数。
· HelloAutodetect指定了autowire="autodetect",如果找到了一个缺省的构造函数,则会应用byType。
· helloBean中指定了dependency-check="simple",则会对基本类型和集合进行依赖检查。这里如果不对属性hello进行赋值,就会抛出一个异常。
· helloCheck中指定了dependency-check="objects",同时也指定了autowire="byType",虽然会自动绑定属性date和hello,但由于属性date2没有被自动绑定到,所以需要特别设置。
|