zoukankan      html  css  js  c++  java
  • (II)第八节:bean 的生命周期

    一、概述

      Spring IOC 容器可以管理 bean 的生命周期,Spring 允许在 bean 生命周期内特定的时间点执行指定的任务

    二、bean 的生命周期

      Spring IOC 容器对 bean 的生命周期进行管理的过程:
        ① 通过构造器或工厂方法创建 bean 实例;
        ② 为 bean 的属性设置值或对其他 bean 的引用(依赖注入);
        ③ 调用 bean 的初始化方法;
        ④ 使用 bean;
        ⑤ 当容器关闭时,调用 bean 的销毁方法;
        当在 bean 中声明了 init() 和 destroy() 方法,Spring IOC 容器并不知道,所以在配置 bean 时,需要使用 init-method 和 destroy-method 属性为 bean 指定初始化和销毁方法。
        注意
              使用 ApplicationContext 创建的引用,它里面并没有关闭的方法,所以,想要关闭 IOC 容器,需要调用 ConfigurableApplicationContext(接口) 或 ClassPathXmlApplicationContext(实现类) 的 close() 方法。
     
      创建 Java 类:
    public class Person {
         
         private Integer id;
         
         private String sex;
         
         private String name;
         public Person() {
             super();
             System.out.println("1.创建对象");
         }
         public Person(Integer id, String sex, String name)  {
             super();
             this.id = id;
             this.sex = sex;
             this.name = name;
         }
         public Integer getId() {
             return id;
         }
         public void setId(Integer id) {
             System.out.println("2.依赖注入");
             this.id = id;
         }
         public String getSex() {
             return sex;
         }
         public void setSex(String sex) {
             this.sex = sex;
         }
         public String getName() {
             return name;
         }
         public void setName(String name) {
             this.name = name;
         }
         @Override
         public String toString() {
             return "4. Person [id=" + id + ", sex=" + sex  + ", name=" + name + "]";
         }
         
         public void init() {
             System.out.println("3. 初始化");
         }
         
         public void destroy () {
             System.out.println("5. 销毁");
         }
    }
     
      配置文件:
        <!--
            生命周期:bean 的创建到销毁:
                IOC 容器中注册的 bean;
                    (1)单例 bean:容器启动的时候就会创建好,容器关闭也会销毁创建的 bean;
                    (2)多实例 bean:获取的时候才创建
                我们可以为 bean 自定义一些生命周期方法,Spring 在创建或者销毁的时候就会调用指定的方法
                自定义初始化方法和销毁方法:The method must have no arguments, but may throw any exception.
                    init-method:
                    destroy-method
        -->
        <bean id="person" class="com.spring.ioc.life.Person"  init-method="init" destroy-method="destroy">
            <property name="id" value="1001"></property>
            <property name="sex" value="男"></property>
        </bean>
     
      测试代码:
        /**
         *  单例:Bean 的生命周期
         *      (容器启动)构造器 ---> 初始化方法 ---> 使用 ---> (容器关闭)销毁方法
         *
         *  多实例 Bean 的生命周期
         *      获取 bean (构造器 ---> 初始化方法) ---> 容器关闭不会调用 bean 的销毁方法
         */
        @Test
        public void test8() {
            ConfigurableApplicationContext ioc = new ClassPathXmlApplicationContext("ioc3.xml");
    
            Person person = ioc.getBean("person",  Person.class);
            System.out.println(person);
    
            ioc.close();
        }
     
     
     
  • 相关阅读:
    软件设计——继承、多态、绑定、重置、创建型设计模式、结构型设计模式和行为设计模式
    软件设计——2017年下半年选择题重要知识点
    day---06 文件的操作
    day---05 基本数据类型(下)
    day---04 基本数据类型(上)
    day---04 流程控制
    day---03 基础的数据类型
    day---02 编程语言的发展史
    day---01 计算机基础
    Day——07
  • 原文地址:https://www.cnblogs.com/niujifei/p/15416840.html
Copyright © 2011-2022 走看看