一、前言

spring的一大优点就是扩展性很强,比如,在spring bean 的生命周期中,给我们预留了很多参与bean 的生命周期的方法。
大致梳理一下,有以下几种:
  • 通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;
  • 通过 <bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
  • 在指定方法上加上@PostConstruct 或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用;
  • 自定义 org.springframework.beans.factory.config.BeanPostProcessor ,来让 spring 回调我们的方法来参与 bean的生命周期。

但有个问题是,如果同时使用上面四种方式,会是什么结果? 谁先,谁后呢?

二、验证

1、新建工程

我这边建立了测试工程,源码在文末:

xml中定义如下:

<bean class="com.ckl.springbeanlifecycle.DemoController" init-method="init" destroy-method="cleanUp"></bean>

重点是我们的DemoController,该类作为一个 bean,在xml中已经定义了,我们为该类,实现了各种接口,以及各种生命周期的相关注解:

 package com.ckl.springbeanlifecycle;

 import com.ckl.springbeanlifecycle.service.IDemoService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; /**
* desc:
*
* @author : caokunliang
* creat_date: 2019/7/20 0020
* creat_time: 18:45
**/
public class DemoController implements InitializingBean,DisposableBean { @Autowired
private IDemoService iDemoService; public DemoController() {
System.out.println();
System.out.println("constructor ");
System.out.println( "属性:" + iDemoService);
System.out.println();
} @Override
public void destroy() throws Exception {
System.out.println();
System.out.println("implements DisposableBean interface");
System.out.println( "属性iDemoService已注入:" + (iDemoService != null));
System.out.println( "属性iDemoService已注入:" + iDemoService);
System.out.println();
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println();
System.out.println("afterPropertiesSet interface");
System.out.println( "属性iDemoService已注入:" + (iDemoService != null));
System.out.println( "属性iDemoService已注入:" + iDemoService);
System.out.println();
} @PostConstruct
public void postConstruct(){
System.out.println();
System.out.println("@PostConstrut....");
System.out.println( "属性iDemoService已注入:" + (iDemoService != null));
System.out.println( "属性iDemoService已注入:" + iDemoService);
System.out.println();
} @PreDestroy
public void preDestroy(){
System.out.println();
System.out.println("@PreDestroy.....");
System.out.println( "属性iDemoService已注入:" + iDemoService);
System.out.println();
} public void init(){
System.out.println();
System.out.println("init-method by xml 配置文件");
System.out.println( "属性iDemoService已注入:" + (iDemoService != null));
System.out.println();
}
public void cleanUp(){
System.out.println();
System.out.println("destroy-method by xml 配置文件");
System.out.println( "属性iDemoService已注入:" + iDemoService);
System.out.println();
}
}

因为我们还需要验证 org.springframework.beans.factory.config.BeanPostProcessor,所以我们自定义了一个 org.springframework.beans.factory.config.BeanPostProcessor:

 package com.ckl.springbeanlifecycle;

 import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component; import java.lang.reflect.Field; /**
* desc:
*
* @author : caokunliang
* creat_date: 2019/7/20 0020
* creat_time: 18:52
**/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DemoController){
System.out.println();
System.out.println("BeanPostProcessor:" + "postProcessBeforeInitialization");
Field field = null;
try {
field = bean.getClass().getDeclaredField("iDemoService");
field.setAccessible(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
try {
Object o = field.get(bean);
System.out.println( "属性iDemoService已注入:" + (o != null));
System.out.println( "属性iDemoService已注入:" + o);
System.out.println();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return bean;
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DemoController){
System.out.println();
System.out.println("BeanPostProcessor:" + "postProcessAfterInitialization");
Field field = null;
try {
field = bean.getClass().getDeclaredField("iDemoService");
field.setAccessible(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
try {
Object o = field.get(bean);
System.out.println( "属性iDemoService已注入:" + (o != null));
System.out.println( "属性iDemoService已注入:" + o);
System.out.println();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return bean;
}
}

2、运行

初始化时的顺序如下:

关闭容器时,执行顺序为:

3、扩展

可能部分朋友,还有这样的需求,即,在程序启动完成后,做点事情,比如,预热缓存之类的,那么,你可以这样:

 import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service; /**
* desc:
*
* @author : caokunliang
* creat_date: 2018/11/20 0020
* creat_time: 14:50
**/
@Service
public class InitRunner implements ApplicationListener<ContextRefreshedEvent> { @Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
//root application context
if (contextRefreshedEvent.getApplicationContext().getParent() == null) {
/**
* 这里既是 root 容器初始化完毕后,会进入该分支,
*/
todo。。。 }else {
/**
* 如果是spring mvc的话, dispatchServlet对应的 applicationContext 会进入这个分支
*/ ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
todo。。。
} }
}

上面是针对启动时做的事情,关闭时,如果想做点事情,可以这样:

 package com.ceiec.webservice.init;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Service; /**
* desc:
*
* @author : caokunliang
* creat_date: 2018/11/20 0020
* creat_time: 14:50
**/
@Service
public class CloseRunner implements ApplicationListener<ContextClosedEvent> {
private static final Logger logger = LoggerFactory.getLogger(CloseRunner.class); @Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
logger.info("clean resources when close applicationContext");
if(contextClosedEvent.getApplicationContext().getParent() == null){ } }
}

4、spring boot 的扩展

针对spring boot,也可以注册我们的 listener来参与生命周期。

实现 org.springframework.boot.SpringApplicationRunListener 即可,并将自定义的 listener 配置到 meta-inf 下的 spring.factories 文件。

举例如下:

 package com.ceiec.router.config;

 import com.ceiec.router.config.servletconfig.MyServletContext;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource; import javax.servlet.ServletContext;
import java.util.Map; /**
* desc:
*
* @author : caokunliang
* creat_date: 2019/5/24 0024
* creat_time: 20:07
**/
@Data
@Slf4j
public class MyListener implements SpringApplicationRunListener { public MyListener(SpringApplication application, String[] args) {
super();
} @Override
public void starting() { } @Override
public void environmentPrepared(ConfigurableEnvironment environment) {
MutablePropertySources propertySources = environment.getPropertySources();
for (PropertySource<?> propertySource : propertySources) {
Object value = propertySource.getProperty("spring.liveBeansView.mbeanDomain"); if (value != null) {
MapPropertySource source = (MapPropertySource) propertySource;
Map<String, Object> map = source.getSource();
map.remove("spring.liveBeansView.mbeanDomain"); log.info("spring.liveBeansView.mbeanDomain: after: {}",propertySource.getProperty("spring.liveBeansView.mbeanDomain"));
}
}
} @Override
public void contextPrepared(ConfigurableApplicationContext context) {
log.info("contextPrepared");
ServletContext servletContext = new MyServletContext();
ServletWebServerApplicationContext applicationContext = (ServletWebServerApplicationContext) context;
applicationContext.setServletContext(servletContext);
} @Override
public void contextLoaded(ConfigurableApplicationContext context) {
//Not used.
} @Override
public void started(ConfigurableApplicationContext context) { } @Override
public void running(ConfigurableApplicationContext context) { } @Override
public void failed(ConfigurableApplicationContext context, Throwable exception) { } }

源码在交友网站: https://github.com/cctvckl/spring-bean-lifecycle

最新文章

  1. Sicily 1153: 马的周游问题(DFS+剪枝)
  2. AX 与Citrix打印机问题
  3. 【贪心】 BZOJ 3252:攻略
  4. css3 动画运动路径
  5. 键盘控制div上下左右移动 (转)
  6. JS获取table表格任意单元格值
  7. Web、WCF和WS通过Nginx共享80端口
  8. JVM常量池和八种基本数据及字符串
  9. PRBS
  10. 可由inetd启动的协议无关时间获取服务器程序
  11. 【iCore1S 双核心板_FPGA】例程三:计数器实验——计数器的使用
  12. THML文档布局元素
  13. Delphi中Json格式读写
  14. ffplay源码分析2-数据结构
  15. vue学习之五生命周期
  16. make menuconfig时出现 #include CURSES_LOC错误
  17. H5实现拍照上传功能
  18. python3【基础】-list&amp;tuple
  19. MFC--根据串口采集的数据借助GDI绘制曲线
  20. SQL Server 跨数据库事务

热门文章

  1. Python目录教程集和资料分享
  2. Win10(64位)安装汇编环境(MASM)
  3. C sharp #004# 进度条与Timer
  4. ETCD:运行时重新配置
  5. 请求时发送OPTIONS请求
  6. Ext.ux.UploadDialog上传大文件 HTTP 错误 413.1 - Request Entity Too Large Web 服务器拒绝为请求提供服务,因为该请求实体过大。Web 服务器无法为请求提供服务,因为它正尝试与客户证书进行协商,但请求实体过大。
  7. mysql登陆时出现ERROR 2013 (HY000): Lost connection to MySQL server at &#39;reading initial communication packet&#39;, system error: 0
  8. iOS----------文字逐个显示
  9. JavaScript 是如何运行的?
  10. java 读取 excel 表格内容