前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Spring5 - Bean的初始化和销毁的4种方式

Spring5 - Bean的初始化和销毁的4种方式

作者头像
小小工匠
发布2021-08-17 15:56:21
发布2021-08-17 15:56:21
57300
代码可运行
举报
文章被收录于专栏:小工匠聊架构小工匠聊架构
运行总次数:0
代码可运行

概述

  • 针对单实例bean的话,容器启动的时候,bean的对象就创建了,而且容器销毁的时候,也会调用Bean的销毁方法
  • 针对原型bean的话,容器启动的时候,bean是不会被创建的而是在获取bean的时候被创建,而且bean的销毁不受 IOC容器的管理.

方式一: 自行指定bean的初始化方法和bean的销毁方法

【beans】

代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.base.lifeCycle;


public class A1 {
	public void init(){
		System.out.println("A1 init");
	}

	public void destory(){
		System.out.println("A1 destory");
	}
}
代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.base.lifeCycle;


public class A2 {

	public void init(){
		System.out.println("A2 init");
	}

	public void destory(){
		System.out.println("A2 destory");
	}
}

【配置类】

代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.base.lifeCycle;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;


@Configuration
public class LCConfig {

	@Bean(initMethod = "init",destroyMethod = "destory")
	public A1 a1(){
		return  new A1();
	}

	@Scope("prototype")
	@Bean
	public A2 a2(){
		return  new A2();
	}
}

【测试】

代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.base.lifeCycle;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.concurrent.TimeUnit;

/**
 * @author 小工匠
 * @version 1.0
 * @description: TODO
 * @date 2020/10/11 22:58
 * @mark: show me the code , change the world
 */
public class LCTest {
	public static void main(String[] args) throws InterruptedException {
		AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(LCConfig.class);

		TimeUnit.SECONDS.sleep(3);
		// 容器销毁
		ac.close();
	}
}

方式二: 通过 InitializingBean和DisposableBean 接口实现bean的初始化以及销毁方法

代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.base.lifeCycle;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * @author 小工匠
 * @version 1.0
 * @description: TODO
 * @date 2020/10/11 23:06
 * @mark: show me the code , change the world
 */
public class A3 implements InitializingBean, DisposableBean {

	public A3() {
		System.out.println("A3 init");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("A3 destroy");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("A3 afterPropertiesSet");
	}
}

执行上面的测试代码


方式三: 通过JSR250规范 提供的注解@PostConstruct 和@ProDestory标注的方法

代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.base.lifeCycle;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author 小工匠
 * @version 1.0
 * @description: TODO
 * @date 2020/10/11 23:12
 * @mark: show me the code , change the world
 */
public class A4 {

	public A4() {
		System.out.println("A4 Construct ");
	}

	@PostConstruct
	public void init(){
		System.out.println("A4 init");
	}

	@PreDestroy
	public void destory(){
		System.out.println("A4 destory");
	}
}

测试结果


方式四:通过Spring的BeanPostProcessor的 bean的后置处理器会拦截所有bean创建过程

代码语言:javascript
代码运行次数:0
运行
复制
public class A5 {
	public A5() {
		System.out.println("A5 Construct");
	}

	public void init(){
		System.out.println("A5 init");
	}

	public void destory(){
		System.out.println("A5 destory");
	}
}

【bean後置處理器】

代码语言:javascript
代码运行次数:0
运行
复制
package com.artisan.base.lifeCycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;


public class A5BeanPostProcessor implements BeanPostProcessor {
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

		if (bean instanceof  A5){
			System.out.println("A5BeanPostProcessor...postProcessBeforeInitialization:"+beanName);
		}


		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

		if (bean instanceof  A5){
			System.out.println("A5BeanPostProcessor...postProcessAfterInitialization:"+beanName);
		}

		return bean;
	}
}

【config】

【test】

简单说一下 BeanPostProcessor的执行时机

看看invokeInitMethods


本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/10/11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 方式一: 自行指定bean的初始化方法和bean的销毁方法
  • 方式二: 通过 InitializingBean和DisposableBean 接口实现bean的初始化以及销毁方法
  • 方式三: 通过JSR250规范 提供的注解@PostConstruct 和@ProDestory标注的方法
  • 方式四:通过Spring的BeanPostProcessor的 bean的后置处理器会拦截所有bean创建过程
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档