Spring第八篇:容器中bean对象的创建顺序

  一、无依赖bean的创建和销毁顺序

  Spring的org.springframework.beans.factory.DisposableBean接口中存在destroy()方法,当我们的调用容器的close()方法会调用所有bean的destroy()方法来做相应的处理。

  创建无依赖bean的class文件。

public class NoDependBean {

    public static class Bean1 implements DisposableBean{

        public Bean1() {
            System.out.println(this.getClass() + "constructor!");
        }

        @Override
        public void destroy() throws Exception {
            System.out.println(this.getClass() + "destroy()");
        }
    }

    public static class  Bean2 implements DisposableBean{

        public Bean2() {
            System.out.println(this.getClass() + "constructor!");
        }

        @Override
        public void destroy() throws Exception {
            System.out.println(this.getClass() + "destroy()");
        }
    }

    public static class Bean3 implements DisposableBean{

        public Bean3(){
            System.out.println(this.getClass() + "constructor!");
        }

        @Override
        public void destroy() throws Exception {
            System.out.println(this.getClass() + "destroy()");
        }
    }

}

  test方法运行结果:从运行结果可以看出,无依赖bean创建顺序以xml文件中的配置顺序一致,销毁顺序则是xml配置文件的倒叙。

class com.java.spring01.demo6.NoDependBean$Bean3constructor!
class com.java.spring01.demo6.NoDependBean$Bean2constructor!
class com.java.spring01.demo6.NoDependBean$Bean1constructor!
class com.java.spring01.demo6.NoDependBean$Bean1destroy()
class com.java.spring01.demo6.NoDependBean$Bean2destroy()
class com.java.spring01.demo6.NoDependBean$Bean3destroy()

  二、通过构造器强依赖的bean创建和销毁

  class文件

public class StrongDependBean {
    public static class Bean1 implements DisposableBean{

        public Bean1() {
            System.out.println(this.getClass() + "constructor!");
        }

        @Override
        public void destroy() throws Exception {
            System.out.println(this.getClass() + "destroy()");
        }
    }

    public static class  Bean2 implements DisposableBean{
        private Bean1 bean1;
        public Bean2(Bean1 bean1) {
            this.bean1 = bean1;
            System.out.println(this.getClass() + "constructor!");
        }

        public Bean2() {
        }

        @Override
        public void destroy() throws Exception {
            System.out.println(this.getClass() + "destroy()");
        }
    }

    public static class Bean3 implements DisposableBean {

        private Bean2 bean2;

        public Bean3(Bean2 bean2){
            this.bean2 = bean2;
            System.out.println(this.getClass() + "constructor!");
        }

        public Bean3() {
        }

        @Override
        public void destroy() throws Exception {
            System.out.println(this.getClass() + "destroy()");
        }
    }

}

  bean.xml文件增加配置

<!--强依赖对象创建-->
    <bean id = "bean3" class="com.java.spring01.demo6.StrongDependBean.Bean3" autowire="constructor"/>
    <bean id = "bean2" class="com.java.spring01.demo6.StrongDependBean.Bean2" autowire="constructor"/>
    <bean id = "bean1" class="com.java.spring01.demo6.StrongDependBean.Bean1"/>

  test方法运行结果

class com.java.spring01.demo6.StrongDependBean$Bean1constructor!
class com.java.spring01.demo6.StrongDependBean$Bean2constructor!
class com.java.spring01.demo6.StrongDependBean$Bean3constructor!
class com.java.spring01.demo6.StrongDependBean$Bean3destroy()
class com.java.spring01.demo6.StrongDependBean$Bean2destroy()
class com.java.spring01.demo6.StrongDependBean$Bean1destroy()

  强依赖的创建顺序同依赖一致,销毁同依赖关系相反。

  三、使用depend-on干扰bean的创建和销毁

<bean id="bean1" class="" depend-on="bean2,bean3; bean4" />

  depend-on用来定义创建当前的bean之前,需要先创建好哪些bean对象。销毁的时候会先此处id的bean对象,然后再去销毁依赖的bean对象。

  在无依赖的bean.xml中添加depend-on属性指定创建属性,来查看bean的创建和销毁。修改bean.xml文件增加depend-on属性。

<bean id = "bean3" class="com.java.spring01.demo6.NoDependBean.Bean3" depends-on="bean1 bean2"/>
    <bean id = "bean2" class="com.java.spring01.demo6.NoDependBean.Bean2"/>
    <bean id = "bean1" class="com.java.spring01.demo6.NoDependBean.Bean1"/>

  运行test方法,查看运行结果。

class com.java.spring01.demo6.NoDependBean$Bean1constructor!
class com.java.spring01.demo6.NoDependBean$Bean2constructor!
class com.java.spring01.demo6.NoDependBean$Bean3constructor!
class com.java.spring01.demo6.NoDependBean$Bean3destroy()
class com.java.spring01.demo6.NoDependBean$Bean2destroy()
class com.java.spring01.demo6.NoDependBean$Bean1destroy()

总结

  1. 无依赖的bean创建顺序和定义的顺序一致,销毁顺序刚好相反

  2. 通过构造器强依赖的bean,会先创建构造器参数中对应的bean,然后才会创建当前bean,销毁顺序刚好相反

  3. depend-on可以指定档期bean依赖的bean,通过这个可以确保depend-on指定的bean在当前bean创建之前先创建好,销毁顺序刚好相反

  4. bean的销毁顺序和bean创建的顺序相反

  

posted @ 2020-05-20 20:10  风缱云流  阅读(1231)  评论(0编辑  收藏  举报