侧边栏壁纸
  • 累计撰写 92 篇文章
  • 累计创建 35 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

spring bean生命周期

天明
2023-10-25 / 0 评论 / 0 点赞 / 26 阅读 / 3239 字 / 正在检测是否收录...
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】-----------");
	}

}
0

评论区