深入理解Spring源码之bean的生命周期控制器BeanPostProcessor

 

 spring是借助ioc容器进行bean的初始化的,ioc的概念如下:

 

1、bean的生命周期:

   bean创建---初始化----销毁的过程
  容器管理bean的生命周期;
 我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
  
  构造(对象创建)
          单实例:在容器启动的时候创建对象
          多实例:在每次获取的时候创建对象
 BeanPostProcessor.postProcessBeforeInitialization


 初始化:
         对象创建完成,并赋值好,调用初始化方法。。。
  BeanPostProcessor.postProcessAfterInitialization
 销毁:
         单实例:容器关闭的时候
         多实例:容器不会管理这个bean;容器不会调用销毁方法

 

Spring对外提供可使用的bean的初始化方式:

 1)、指定初始化和销毁方法;
          通过@Bean指定init-method和destroy-method;
 2)、通过让Bean实现InitializingBean(定义初始化逻辑),
                  DisposableBean(定义销毁逻辑);

package com.atguigu.bean;

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

@Component
public class Cat implements InitializingBean,DisposableBean {
	
	public Cat(){
		System.out.println("cat constructor...");
	}

	@Override
	public void destroy() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("cat...destroy...");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("cat...afterPropertiesSet...");
	}

}


 3)、可以使用JSR250;
 4)、BeanPostProcessor【interface】:bean的后置处理器;
         在bean初始化前后进行一些处理工作;
         postProcessBeforeInitialization:在初始化之前工作
         postProcessAfterInitialization:在初始化之后工作

@ComponentScan("com.atguigu.bean")
@Configuration
public class MainConfigOfLifeCycle {
	//  多实例:容器不会管理这个bean;容器不会调用销毁方法
	@Scope("prototype")
	@Bean(initMethod="init",destroyMethod="detory")
	public Car car(){
		return new Car();
	}

}
package com.atguigu.bean;

import org.springframework.stereotype.Component;

@Component
public class Car {
	
	public Car(){
		System.out.println("car constructor...");
	}
	
	public void init(){
		System.out.println("car ... init...");
	}
	
	public void detory(){
		System.out.println("car ... detory...");
	}

}

 

package com.atguigu.test;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.atguigu.config.MainConfigOfLifeCycle;

public class IOCTest_LifeCycle {
	
	@Test
	public void test01(){
		//1、创建ioc容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
		System.out.println("容器创建完成...");
		
		//applicationContext.getBean("car");
		//关闭容器
		applicationContext.close();
	}

}

2、BeanPostProcessor 初始化bean的示意图: 

3、Spring底层对 BeanPostProcessor 的使用


         bean赋值,注入其他组件,@Autowired,生命周期注解功能,@Async,

 

BeanPostProcessor的实现类

 举个例子:

package com.atguigu.bean;

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

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class Dog implements ApplicationContextAware {
	
	//@Autowired
	private ApplicationContext applicationContext;
	
	public Dog(){
		System.out.println("dog constructor...");
	}
	
	//对象创建并赋值之后调用
	@PostConstruct
	public void init(){
		System.out.println("Dog....@PostConstruct...");
	}
	
	//容器移除对象之前
	@PreDestroy
	public void detory(){
		System.out.println("Dog....@PreDestroy...");
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext = applicationContext;
	}

}

 

把断点打在setApplicationContext可以看到底层是执行ApplicationContextAwareProcessor的invokeAwareInterfaces方法进行设置IOC容器的,而ApplicationContextAwareProcessor就是继承自BeanPostProcessor利用postProcessBeforeInitialization进行初始化调用invokeAwareInterfaces方法的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据与后端架构提升之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值