Spring自动装配(绑定)与依赖检查

55 篇文章 0 订阅
33 篇文章 0 订阅

 一、自动装配(autowire):

       官方给出的定义是这样:spring IoC容器可以自动装配相互协作bean之间的关联关系。因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。由于autowire可以针对单个bean进行设置,因此可以让有些bean使用autowire,有些bean不采用。autowire的方便之处在于减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥,自动装配就是让我们少几个  <ref ="...">。

  如果不用自动装配,代码实现会是这样:

bean:

public class HelloBean {
    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

applicationContext.xml:

<?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">
 
    <bean id="date" class="java.util.Date" />
    <bean id="helloBean" class="com.yourcompany.spring.spring1.HelloBean">
    	<property name="date" ref="date"/>
    </bean>

</beans>

程序主入口:

public class Hello {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext1.xml");
        HelloBean hello = (HelloBean) context.getBean("helloBean");
        System.out.println("Date:" + hello.getDate());
    }
}

        但如果使用自动装配则不需要写ref="……",自动装配的方式有四种:byName、byType、constructor和autodetect。

        1、byName:

        即配置文件会自动根据名字去匹配相同的属性,如果没找到则为null。byName的name是指类中setXxxx()方法的Xxxx和配置文件中<bean>的id属性匹配,和成员变量名毫无关系。此时唯一改变的文件是:

applicationContext.xml:

<?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">
 
    <bean id="date" class="java.util.Date" />
    <bean id="helloBean" class="com.yourcompany.spring.spring1.HelloBean" autowire="byName">
    </bean>

</beans>

        当然bean文件可以像下面这样,注意set方法方法名中set后的名词必须在配置文件中<bean>的id属性中找得到:

public class HelloBean {
    private Date date1;

    public Date getDate() {
        return date1;
    }

    public void setDate(Date date1) {
        this.date1 = date1;
    }
}

        2、byType:

        即配置文件会自动根据类型去匹配相同的属性,如果没找到则为null。byType的Type是指类中setXxxx()方法中入参类型和配置文件中<bean>的class属性匹配。此时唯一改变的文件是:

applicationContext.xml:

<?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">
 
    <bean id="date" class="java.util.Date" />
    <bean id="helloBean" class="com.yourcompany.spring.spring1.HelloBean" autowire="byType">
    </bean>

</beans>

        3、constructor:

        即配置文件会自动根据构造方法中的形参变量名去匹配配置文件中<bean>的id属性,如果没找到则为null。

bean:

public class HelloBean {
    private Date date;
    private String str;
    private ArrayList list;

    public HelloBean(Date date, String str, ArrayList list) {
        this.date = date;
        this.str = str;
        this.list = list;
    }

    public Date getDate() {
        return date;
    }

    public String getStr() {
        return str;
    }

    public ArrayList getList() {
        return list;
    }
}

applicationContext.xml:

<?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">
 
    <bean id="date" class="java.util.Date" />
    <bean id="str" class="java.lang.String"/>
    <bean id="list" class="java.util.ArrayList"/>
    <bean id="helloBean" class="com.yourcompany.spring.spring1.HelloBean" autowire="constructor">
    </bean>

</beans>

程序主入口:

public class Hello {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext1.xml");
        HelloBean hello = (HelloBean) context.getBean("helloBean");
        System.out.println("Date:" + hello.getDate());
        System.out.println("str:" + hello.getStr());
        System.out.println("list:" + hello.getList());
    }
}

        4、autodetect:

        首先尝试使用constructor来自动装配,然后再尝试使用byType方式。

二、依赖检查(dependency-check):

       用来检查spring容器管理的bean中是否有属性未定义。如果有属性未定义,抛出org.springframework.beans.factory.UnsatisfiedDependencyException异常。可以在spring配置文件的根元素beans中设置default-dependency-check属性,或子元素bean中设置dependency-check属性。属性值可选:

        1、simple:检查对应基本类型的bean属性和Java.lang.String类型的bean属性

        2、objects:检查对应其他对象类型的bean属性

        3、all:simple+object

        4、none


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值