1. 程式人生 > >易學筆記--第2章:spring中的Bean/2.5 Bean的週期回撥

易學筆記--第2章:spring中的Bean/2.5 Bean的週期回撥

 

第2章:spring中的Bean/2.5 Bean的週期回撥/2.5.1  概念

 


第2章:spring中的Bean/2.5 Bean的週期回撥/2.5.2 宣告週期方法

  1. 方法範圍:在Bean類中的任何方法
  2. 方法引數:無引數
  3. 方法返回值:無返回值
  4. 是否可以丟擲異常:可以丟擲異常

第2章:spring中的Bean/2.5 Bean的週期回撥/2.5.3 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