spring 笔记之九自动绑定和依赖检查

 

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,
 

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="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>

      
      
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没有被自动绑定到,所以需要特别设置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值