Spring bean 实现初始化、销毁方法的方式及顺序

时间:2023-03-08 17:11:16
Spring bean 实现初始化、销毁方法的方式及顺序

Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作,常用方法有三种:

  • 使用注解,在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用;
  • 使用xml配置,通过<bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
  • 实现InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法。

这三种实现方式,在执行顺序上还是有一定差异,简单测试代码:

java类:

 package com.test.spring.initorder;

 import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; public class InitOrderBean implements InitializingBean,DisposableBean { public InitOrderBean() {
super();
System.out.println("InitOrderBean执行构造方法......");
} @Override
public void destroy() throws Exception {
System.out.println("InitOrderBean执行 DisposableBean destory 方法........"); } @Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitOrderBean执行InitializingBean 初始化方法 ....."); } @PostConstruct
public void postConstruct() {
System.out.println("InitOrderBean执行postConstruct Anno: postConstruct");
} @PreDestroy
public void preDestroy() {
System.out.println("InitOrderBean执行preDestroy Anno: preDestroy");
} public void init (){
System.out.println("InitOrderBean执行xml init......");
} public void destory(){
System.out.println("InitOrderBean执行xml destory.........");
} }

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 测试类:

 package com.test.spring.initorder;

 import org.springframework.context.support.ClassPathXmlApplicationContext;

 public class Test {

     public static void main(String[] args) {
ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();
}
}

执行结果:

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