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