package cn.zcdev.demo;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
/**
* 描述:Java容器对象常用生命周期方法测试
*
* @author [天明]
* @version: 0.0.1 2019年9月6日-上午12:09:03
*
*/
@Configuration
public class DemoBeanConfig implements
//
ApplicationContextAware,
//
InitializingBean,
//
DisposableBean,
//
ApplicationListener<ContextRefreshedEvent>
//
{
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("---------ApplicationContextAware#setApplicationContext()【0】-----------");
}
@PostConstruct
public void PostConstruct() {
System.out.println("----------@PostConstruct Bean初始化完成后执行【1】-----------");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("--------- InitializingBean#afterPropertiesSet()【2】-----------");
}
@Bean
public DemoBean demoBean() {
System.out.println("----------currentBean 【在2-3之间】-----------");
return new DemoBean();
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
// ContextRefreshedEvent:容器发布完成后执行,会执行多次!!!
// ContextClosedEvent:容器停止后执行,会执行多次!!!
// ContextStartedEvent:
// ContextStoppedEvent:
System.out.println("--------- ApplicationListener#ContextRefreshedEvent()【在2-3之间执行多次】-----------");
}
@PreDestroy
public void PreDestroy() {
System.out.println("----------@PreDestroy Bean卸载后执行【3】-----------");
}
@Override
public void destroy() throws Exception {
System.out.println("--------- DisposableBean#destroy()【4】-----------");
}
}
spring bean生命周期
本文链接:https://blog.jnliok.com/post/spring-bean-life-cycle.html
-- EOF --
Comments