package com.jnliok.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】-----------");
}
}
版权归属:
天明
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区