Spring 允許 Bean 在初始化完成后以及銷毀前執行特定的操作,常用方法有三種:
- 使用注解,在指定方法上加上@PostConstruct或@PreDestroy注解來制定該方法是在初始化之后還是銷毀之前調用;
- 使用xml配置,通過<bean> 元素的 init-method/destroy-method屬性指定初始化之后 /銷毀之前調用的操作方法;
- 實現InitializingBean/DisposableBean 接口來定制初始化之后/銷毀之前的操作方法。
這三種實現方式,在執行順序上還是有一定差異,簡單測試代碼:
java類:
1 package com.test.spring.initorder; 2 3 import javax.annotation.PostConstruct; 4 import javax.annotation.PreDestroy; 5 6 import org.springframework.beans.factory.DisposableBean; 7 import org.springframework.beans.factory.InitializingBean; 8 9 public class InitOrderBean implements InitializingBean,DisposableBean { 10 11 12 public InitOrderBean() { 13 super(); 14 System.out.println("InitOrderBean執行構造方法......"); 15 } 16 17 @Override 18 public void destroy() throws Exception { 19 System.out.println("InitOrderBean執行 DisposableBean destory 方法........"); 20 21 } 22 23 @Override 24 public void afterPropertiesSet() throws Exception { 25 System.out.println("InitOrderBean執行InitializingBean 初始化方法 ....."); 26 27 } 28 29 @PostConstruct 30 public void postConstruct() { 31 System.out.println("InitOrderBean執行postConstruct Anno: postConstruct"); 32 } 33 34 35 @PreDestroy 36 public void preDestroy() { 37 System.out.println("InitOrderBean執行preDestroy Anno: preDestroy"); 38 } 39 40 41 public void init (){ 42 System.out.println("InitOrderBean執行xml init......"); 43 } 44 45 46 public void destory(){ 47 System.out.println("InitOrderBean執行xml destory........."); 48 } 49 50 }
xml 配置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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <context:component-scan base-package="com.test.spring.initorder" /> <context:annotation-config/> <bean class="com.test.spring.initorder.InitOrderBean" init-method="init" destroy-method="destory"> </bean> </beans>
java 測試類:
1 package com.test.spring.initorder; 2 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5 public class Test { 6 7 public static void main(String[] args) { 8 ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml"); 9 context.close(); 10 } 11 }
執行結果:
InitOrderBean執行構造方法......
InitOrderBean執行postConstruct Anno: postConstruct
InitOrderBean執行InitializingBean 初始化方法 .....
InitOrderBean執行xml init......
InitOrderBean執行preDestroy Anno: preDestroy
InitOrderBean執行 DisposableBean destory 方法........
InitOrderBean執行xml destory.........
由此可得出:
Bean在實例化的過程中:Constructor --> @PostConstruct -->InitializingBean --> init-method
Bean在銷毀的過程中:@PreDestroy --> DisposableBean --> destroy-method