Spring 核心技术——IoC 之 DI (2)

来源:互联网 发布:管理类联考网络冲刺班 编辑:程序博客网 时间:2024/06/03 22:42

  • bean 之间的关系
    • 1 引用
    • 2 依赖
    • 3 继承
  • 自动装配
    • 1 相关配置及作用域
    • 2 自动装配的方式
      • 21 byName
      • 22 byType
      • 23 constructor
    • 3 自动装配的控制
      • 31 autowire-candidate
      • 32 default-autowire-candidates
  • scope 和 scoped-proxy
    • 1 scope
    • 2 scoped-proxy
  • lazy-init
    • 1 相关配置及作用域
    • 2 属性值
  • 生命周期
    • 1 配置方法
    • 2 使用场景

1 bean 之间的关系

bean 之间的常见关系有如下几种:

  • 引用:idref/ref
  • 依赖:depends-on
  • 继承:abstract,parent

1.1 引用

引用是 bean 之间最常见的关系,一个 bean 内部使用了另外一个 bean,即构成了引用关系,通常使用 idref/ref 来指定引用关系,内部 bean 也是一种引用。

<bean id="pet" class="com.jyhuang.spring.DI.DI_type.Pet">    <property name="name" value="Jerry"/>    <property name="age" value="3"/></bean><bean id="people2" class="com.jyhuang.spring.DI.DI_type.People">    <property name="name" value="John"/>    <property name="age" value="20"/>    <property name="pet" ref="pet"/></bean>

1.2 依赖

区别于引用,存在依赖关系的 bean 之间并不一定存在直接的关联,通常是 beanB 的初始化需要一定的条件,而这些条件需要由 beanA 完成,此时就需要使用依赖。对于存在依赖关系的 bean,被依赖的 bean 一定会提前初始化。

<bean id="A" class="com.jyhuang.spring.DI.depends_on.ExampleBeanA" depends-on="B"/><bean id="B" class="com.jyhuang.spring.DI.depends_on.ExampleBeanB"/>

1.3 继承

区别于上述两种,继承表示 bean 之间存在父子关系,父 bean 可以是抽象,子 bean 可以继承和覆盖父 bean 中的内容。如果父 bean 是抽象的(使用 abstract 属性指定,默认为 false),则无须指定 class 属性的值,因为它本身就不能实例化;子 bean 需要使用 parent 属性指定父 bean 的 id,此时就可以继承父 bean 中的 property,同时还可以在子 bean 中显式父 bean 中同名的 property 来覆盖父 bean 中该 property 的值。

<bean id="parent" abstract="true">    <property name="name" value="parent"/>    <property name="age" value="30"/>    <property name="asset" value="all"/></bean><bean id="child" class="com.jyhuang.spring.DI.inheritance.ChildClass"      parent="parent">    <property name="name" value="child"/>    <property name="age" value="1"/></bean>

2 自动装配

在 Spring 核心技术——IoC 之 DI (1) 中已经说明了手动注入的相关配置说明,此处主要介绍自动装配。

与手动注入异同:

  • 自动装配可以简化配置文件,Spring 会根据自动装配的方式自动进行 bean 的注入,而无须显式指定;也正是如此,使用自动装配时,往往会出现多个候选 bean 导致 Spring 报错或者无法确定 Spring 到底注入得是哪个 bean。
  • 无论是手动注入还是自动装配,对于需要注入的属性值,都必须提供对应的 setter 方法,否则无法注入。

2.1 相关配置及作用域

  • 装配方式,取值见 2.2 自动装配的方式
    • <beans> 标签的 default-autowire 属性,对该标签下的所有 bean 生效
    • <bean> 标签的 autowire 属性,只对当前 bean 有效
  • 装配候选 bean,具体见 2.3 自动装配的控制
    • <beans> 标签的 default-autowire-candidates 属性,指定自动装配的 bean 名字的表达式,如:*Service*Dao 等,指定多个时,用 , 隔开。
    • <bean> 标签的 autowire-candidate 属性,指定当前 bean 是否作为自动装配的候选 bean

2.2 自动装配的方式

主要有五种(default-autowire/autowire 的取值),实际只有四种:

  • default/no: 默认值,不进行自动装配
  • byName: 通过属性名进行自动装配
  • byType: 通过属性类型进行自动装配
  • constructor: 通过构造器进行自动装配

使用以下两个类,对全局的自动装配进行简单说明。(局部的配置与全局的基本一致,如果两处同时配置,以局部为准)

public class Pet {    private String name;    private int age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Override    public String toString() {        return "Pet{name='" + name + ", age=" + age + '}';    }}public class People {    private String name;    private Pet pet;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Pet getPet() {        return pet;    }    public void setPet(Pet pet) {        this.pet = pet;    }    @Override    public String toString() {        return "People{name='" + name + ", pet=" + pet + '}';    }}

2.2.1 byName

需要注入的属性名与配置中 bean 的 id/name 相同即可注入。由于容器中 bean 的 id/name 是唯一的,所以不存在无法精确定位 bean 的问题。

<?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.xsd"       default-autowire="byName">    <bean id="pet" class="com.jyhuang.spring.DI.autowiring.Pet">        <property name="name" value="Jerry"/>        <property name="age" value="10"/>    </bean>    <bean id="people1" class="com.jyhuang.spring.DI.autowiring.People">        <property name="name" value="Tom"/>    </bean></beans>    

2.2.2 byType

需要注入的属性类型与配置中 bean 的类型一致,即可注入。 与 byName 不同,因为容器中可以定义多个类型相同的 bean,所以必须保证能够精确定位。

<?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.xsd"       default-autowire="byType">    <bean id="mouse" class="com.jyhuang.spring.DI.autowiring.Pet">        <property name="name" value="Jerry"/>        <property name="age" value="10"/>    </bean>    <bean id="people2" class="com.jyhuang.spring.DI.autowiring.People">        <property name="name" value="Tom"/>    </bean></beans>

2.2.3 constructor

使用构造方法进行自动装配与 byType 类似,也是通过类型进行自动注入的,即也需要保证自动装配 bean 的唯一性。不同的是,使用构造方法进行自动装配必须提供对应的带参构造方法。

    <bean id="mouse" class="com.jyhuang.spring.DI.autowiring.Pet">        <property name="name" value="Jerry"/>        <property name="age" value="10"/>    </bean>    <bean id="people3" class="com.jyhuang.spring.DI.autowiring.People">        <property name="name" value="Jerry"/>    </bean></beans>

2.3 自动装配的控制

对于一个 Spring 项目,通常 bean 的数据会相当多,此时使用自动装配就容易出现多个候选 bean,导致 Spring 报错。针对这个问题,可以使用 autowire-candidate 相关属性对自动装配的 bean 进行进一步的控制。

2.3.1 autowire-candidate

<bean> 标签的属性,用于指定当前 bean 是否作为自动装配的 bean,有如下值:true/false/default,默认为 true

2.3.2 default-autowire-candidates

<beans> 标签的属性,用于指定该标签下哪些可以作为自动装配的 bean,它的值会对 bean 的 name/id 进行模式匹配,从而对自动装配进行限制。常见的如:*Service*Dao 等,如果需要指定多个装配模式,可以使用 , 进行分隔。

注意:

  • 如果同时显式指定了上面的配置,前者会覆盖后者,即模式匹配将不起作用。
  • 上面的两种配置对 byName 方式的自动装配无效。

3 scope 和 scoped-proxy

3.1 scope

scope 是指 bean 的作用范围,默认为 singleton(单例),随着 Spring 的不断完善,目前 Spring4 主要支持如下值:

  • singleton: 默认值,单例,即无论获取多少次该 bean 的实例,都是同一个对象。
  • prototype: 与 singleton 相反,它可以针对一个 bean 实例化多个对象
  • request: bean 的生命周期仅限于一个 HTTP 请求,只能用于与 Web 相关的 ApplicationContext 中
  • session: bean 的生命周期仅限于一个 HTTP session,只能用于与 Web 相关的 ApplicationContext 中
  • globalSession: bean 的生命周期仅限于一个全局的 HTTP session,只能用于与 Web 相关的 ApplicationContext 中
  • application: bean 的生命周期仅限于一个 ServletContext,只能用于与 Web 相关的 ApplicationContext 中

通常,使用最多的是 singleton 和 prototype,下面几个均需要与相关的 Web 容器结合使用,另外,Spring 还支持自定义 scope,需要实现 Scope 接口,此处不再说明。

对于常见的三层开发结构(Controller/Service/Dao),控制层基本都是 prototype 或者与 Web 相关 scope 中的一种,而业务层和数据访问层基本都是 singleton。

3.2 scoped-proxy

正因为 bean 有多个 scope 值,所以,当拥有不同 scope 的 bean 之间出现引用关系时就会存在问题。常见的为一个长生命周期的 bean 引用一个短生命周期的 bean,以 singleton 的 beanA 引用 prototype 的 beanB 为例加以说明:

默认情况下, beanA 会在容器初始化时被实例化,且仅此一次,此时 beanB 会被注入到 beanA 中,那么,在之后的使用中,beanA 中的 beanB 就会永远是第一次被注入时的那个 bean 实例,即使 beanB 的 scope 是 prototype,显然这不是我们希望的。因此,Spring 提供了 scoped-proxy 的配置,用以解决这个问题。

e.g.

public class SingletonBean {    private PrototypeBean prototypeBean;    public void printTime() {        prototypeBean.printTime();    }    public void setPrototypeBean(PrototypeBean prototypeBean) {        this.prototypeBean = prototypeBean;    }}public class PrototypeBean {    public void printTime() {        System.out.println(System.currentTimeMillis());    }}// 测试类public class DI_scope_Test {    @Test    public void test_scope_proxy() {        SingletonBean singletonBean = context.getBean(SingletonBean.class);        singletonBean.printTime();        singletonBean = context.getBean(SingletonBean.class);        singletonBean.printTime();    }}
<bean id="singletonBean" class="com.jyhuang.spring.DI.scope.scope_proxy.SingletonBean">    <property name="prototypeBean" ref="prototypeBean"/></bean><bean id="prototypeBean" class="com.jyhuang.spring.DI.scope.scope_proxy.PrototypeBean"      scope="prototype">    <aop:scoped-proxy/></bean>

说明:

  • 使用如上配置后,每次获取 singletonBean,其中的 prototypeBean 都会重新获取一个实例;否则输出的时间值永远都是一样的,因为是同一个 prototypeBean。
  • 默认不使用代理,开启需要显示指定 <aop:scoped-proxy/>
  • 默认的代理是基于 class 的,即使用 CGLIB,如果需要改变,指定该标签的属性 proxy-target-class 为 false,此时的代理就是基于 interface,不过使用此方式,必须保证需要代理的 bean 至少实现一个接口。

4 lazy-init

默认情况下,ApplicationContext 的实现类会在初始化的时候会即时创建和配置 singleton 的 bean。通常,是推荐即时加载,因为可以在初始化阶段就发现 bean 配置中的问题,但即时加载并不是必须,此时,就可以通过设置懒加载属性来阻止初始化时的 singleton bean 的即时加载。

注意:

  • 对于配置了懒加载的 bean,IoC 容器只会在该 bean 被第一次请求时实例化,而不是在容器启动时。
  • 懒加载属性只对 singleton 的 bean 有效,对于其他类型的 bean,如:prototype,只有在第一次被请求时才会被容器实例化。

4.1 相关配置及作用域

  • <beans> 标签属性 default-lazy-init,对 <beans> 标签下的所有 bean 生效
  • <bean> 标签属性 lazy-init,只对当前 bean 生效

当上述两者同时被指定时,以局部配置为准。

4.2 属性值

default-lazy-init/lazy-init 有如下三个值:

  • default: 默认值,即 false,不使用懒加载
  • true: 使用懒加载
  • false: 同默认值

5 生命周期

对于任何一个由 Spring 管理的 bean,都有如下生命周期:构造、初始化、使用、销毁。构造由 Spring 容器执行,使用是指相关业务方法的调用,初始化和销毁可手动指定(其实,通常并不需要)。

5.1 配置方法

<bean> 标签提供了 init/destory-method 属性用于配置初始化和销毁的方法,属性值对应 bean 中的初始化和销毁方法,简单举例:

Java Bean:

public class ExampleBean {    public ExampleBean() {        System.out.println("constructor...");    }    public void init() {        System.out.println("init...");    }    public void sayHello() {        System.out.println("Hello World!");    }    public void destory() {        System.out.println("destory...");    }}

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.xsd">    <bean id="exampleBean" class="com.jyhuang.spring.DI.lifecycle.ExampleBean"          init-method="init" destroy-method="destory"/></beans>

Test Class:

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("/com/jyhuang/spring/DI/lifecycle/applicationContext.xml")public class DI_lifecycle_Test {    @Autowired    ExampleBean exampleBean;    @Test    public void test() {        // ApplicationContext context = new ClassPathXmlApplicationContext(        //      "com/jyhuang/spring/DI/lifecycle/applicationContext.xml");        // ExampleBean exampleBean = (ExampleBean) context.getBean("exampleBean");        exampleBean.sayHello();    }}

输出结果:

constructor...init...Hello World!INFO: Closing ...GenericApplicationContext... // 关闭 Spring 容器destory...

说明:

  • bean 初始化的时机是在 Spring 容器初始化的之后,类似,bean 销毁的时机是在 Spring 容器销毁的之前。上面的测试类中,使用的是 Spring 的测试框架,它会在业务执行结束后销毁 Spring 容器,而类中注释代码的方式不会,所以无法看到 destory 方法的调用。
  • bean 的 destory 方法只对 singleton 类型的 bean 有效,对于其他类型的 bean,即使 Spring 容器销毁,也不会调用 destory 方法。

5.2 使用场景

通常情况下,对于自定义的 bean,基本不需要使用 init/destory-method 属性指定生命周期,最常见的应用为数据源的配置。

0 0
原创粉丝点击