易学笔记--第2章:spring中的Bean/2.5 Bean的周期回调

 

第2章:spring中的Bean/2.5 Bean的周期回调/2.5.1  概念

  • 概念

 

    • 这里的声明周期指的是Bean在创建完成后和销毁时这两个时间点,对于不同的作用域这两个时间点有所不同
    • 作用域对应的时间点
      1. 单例:singleton( 第2章:spring中的Bean/2.3 Bean的作用域/2.3.1 单例:singleton)
        1. 初始化:
          1. spring容器启动时,也就是生成spring的applicationContext对象时,具体参考:
            1. 第1章:spring注入/1.1 元数据配置/1.2 XML配置方式/1.2.3 XML配置方式调用
            2. 第1章:spring注入/1.1 元数据配置/1.3 JAVA配置方式/1.3.3 配置类方式调用
            3. 第1章:spring注入/1.1 元数据配置/1.4 注解方式/1.4.3 注解调用方式/1.4.3.2 调用方式
          2. bean对象被引用时
          3. applicationContext.getBean操作时
        2. 销毁 通过applicationContext 对象调用registerShutdownHook方法
      2. 原型作用域:prototype( 第2章:spring中的Bean/2.3 Bean的作用域/2.3.2 原型作用域:prototype)
        1. 初始化:
          1. bean对象被引用时
          2. applicationContext.getBean操作时
        2. 销毁:因为prototype作用域对象无法跟踪,不会调用销毁方法
      3. 请求作用域:request( 第2章:spring中的Bean/2.3 Bean的作用域/2.3.3 请求作用域:request)
        1. 初始化:有请求进来时,参考另外一本笔记:
          1. 第5章:作为Web应用:属性和监听者/5.4 Servlet监听器/5.4.7 请求跟踪监听器(ServletRequestListener)/5.4.7.1 触发时机
          2. 第5章:作为Web应用:属性和监听者/5.4 Servlet监听器/5.4.7 请求跟踪监听器(ServletRequestListener)/5.4.7.2 实例讲解
        2. 销毁:当前WEB请求结束后
      4. 会话作用域:session( 第2章:spring中的Bean/2.3 Bean的作用域/2.3.4 会话作用域:session)
        1. 初始化:创建会话时时,参考另外一本笔记:
          1. 第5章:作为Web应用:属性和监听者/5.4 Servlet监听器/5.4.4 会话跟踪监听器(HttpSessionListener)/5.4.4.1 触发时机
          2. 第5章:作为Web应用:属性和监听者/5.4 Servlet监听器/5.4.4 会话跟踪监听器(HttpSessionListener)/5.4.4.2 实例讲解
        2. 销毁:当前会话结束时

第2章:spring中的Bean/2.5 Bean的周期回调/2.5.2 声明周期方法

  • 声明周期方法
  1. 方法范围:在Bean类中的任何方法
  2. 方法参数:无参数
  3. 方法返回值:无返回值
  4. 是否可以抛出异常:可以抛出异常

第2章:spring中的Bean/2.5 Bean的周期回调/2.5.3 XML方式定义回调方法

  • XML方式定义声明周期回调方法

 

  1. Bean对象创建完成后初始化方法:init-method="方法名",这里的方法名符合: 第2章:spring中的Bean/2.5 Bean的声明周期/2.5.2 声明周期方法
  2. Bean对象销毁之前清理方法:destroy-method="方法名",这里的方法名符合: 第2章:spring中的Bean/2.5 Bean的声明周期/2.5.2 声明周期方法
  3. 举例:
    1. 类定义:

      public class Foo {

           //初始化方法

           public void init() throws Exception {

                 System.out.println("Foo.init method invoked");

           }

           

           //销毁方法

           public void destroy() throws RuntimeException {

                 System.out.println("Foo.destroy method invoked");

           }

      }

    2. XML格式定义:

        <!-- init和 destroy分别对应com.wiley.beginningspring.ch2.Foo类中的方法-->

           <bean id="foo" class="com.wiley.beginningspring.ch2.Foo" init-method="init" destroy-method="destroy"/>

    3. 调用方法:

      public class Main {

           public static void main(String[] args) {

                 ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");

                 applicationContext.registerShutdownHook();

           }

      }

    4. 调用结果:Foo.init method invoked

      Foo.destroy method invoked


第2章:spring中的Bean/2.5 Bean的周期回调/2.5.4 注解方式方式定义回调方法

  • 注解方式方式定义回调方法
  1. Bean对象创建完成后初始化方法:使用注解:@PostConstruct,
  2. Bean对象销毁之前清理方法:使用注解:@PreDestroy
  3. 举例:
    1. 类定义:

      import javax.annotation.PostConstruct;

      import javax.annotation.PreDestroy;

      public class Bar {

           @PostConstruct

           public void init() throws Exception {

                 System.out.println("Bar.init method invoked");

           }

           

           @PreDestroy

           public void destroy() throws RuntimeException {

                 System.out.println("Bar.destroy method invoked");

           }    

      }

    2. XML中定义:

      <!-- 声明使用注解 -->

           <bean class="com.wiley.beginningspring.ch2.Bar"/>

           <context:annotation-config/>

    3. 调用方法:

      public class Main {

           public static void main(String[] args) {

                 ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");

                 applicationContext.registerShutdownHook();

           }

      }

    4. 调用结果: Bar.init method invoked

      Bar.destroy method invoked


第2章:spring中的Bean/2.5 Bean的周期回调/2.5.5 实现接口方式

  • 实现接口方式
  1. Bean对象创建完成后初始化方法:实现接口InitializingBean中的public void afterPropertiesSet() throws Exception方法
  2. Bean对象销毁之前清理方法:实现接口DisposableBean中的public void destroy() throws Exception方法
  3. 举例:
    1. 举例:
      1. 类定义:

        import org.springframework.beans.factory.DisposableBean;

        import org.springframework.beans.factory.InitializingBean;

        public class Baz implements InitializingBean, DisposableBean {

            //初始化方法

             public void afterPropertiesSet() throws Exception {

                   System.out.println("Baz.init method invoked");

             }

             //销毁方法

             public void destroy() throws Exception {

                   System.out.println("Baz.destroy method invoked");

             }

             

        }

      2. XML中定义:

        <bean class="com.wiley.beginningspring.ch2.Baz"/>

      3. 调用方法:

        public class Main {

             public static void main(String[] args) {

                   ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");

                   applicationContext.registerShutdownHook();

             }

        }

      4. 调用结果: Baz.init method invoked

        Baz.destroy method invoked

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

易学笔记(qq:1776565180)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值