Spring bean 实现初始化、销毁方法的方式及顺序
2016-09-14 10:17
591 查看
Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作,常用方法有三种:
使用注解,在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用;
使用xml配置,通过<bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
实现InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法。
这三种实现方式,在执行顺序上还是有一定差异,简单测试代码:
java类:
xml 配置applicationContext.xml:
java 测试类:
执行结果:
由此可得出:
Bean在实例化的过程中:Constructor --> @PostConstruct -->InitializingBean --> init-method
Bean在销毁的过程中:@PreDestroy --> DisposableBean --> destroy-method
使用注解,在指定方法上加上@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
相关文章推荐
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- Spring bean 的初始化和销毁的三种方式和执行顺序
- Spring bean通过实现InitializingBean ,DisposableBean接口实现初始化方法和销毁前操作
- 在Spring容器中Bean的几种初始化、销毁方法的实现和先后顺序
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- spring bean的几种初始化方法和销毁方法的先后顺序
- Spring_Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- Spring 实现初始化和销毁bean之前进行的操作的方法
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作