spring注解驱动(生命周期):bean生命周期,原码查看BeanPostProcessor原理和使用

bean生命周期

主要是关注bean创建,初始化,和销毁的过程

package jane.config;

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

import jane.bean.BenzCar;
import jane.bean.Car;

/*
 * bean的生命周期:
 * 	  bean创建--初始化--销毁的过程
 * 容器管理bean的生命周期,就是执行自定义的初始化和销毁的方法
 * 
 * 整个过程如下面:
 * --------------------------------------------------------
 * 构造(对象创建)
 * 		单实例:在容器启动的时候创建对象
 * 		多实例:在每次获取的时候创建对象
 * BeanPostProcessor.postProcessBeforeInitialization
 * 初始化:
 * 		对象创建完成并且赋值好,调用初始化的方法
 * BeanPostProcessor.postProcessAfterInitialization
 * 销毁:
 * 		这里特殊一下:
 * 		单实例:容器关闭的时候销毁
 * 		多实例:容器只负责创建这个bean,不会管理它的死亡,
 * 				所以就不会调用销毁的方法
 * 	
 * -------------------------------------------------------------	
 * 按照之前,我们指定初始化和销毁的方法是
 * 		init-method="" destroy-method=""
 * 现在是
 * 		方法一:@Bean(initMethod = "init",destroyMethod = "destroy")
 * 		方法二:通过让bean实现InitializingBean(里面的afterPropertiesSet方法定义初始化方法)
 * 				还有实现DisposableBean(里面的destroy定义销毁方法)
 * 		方法三:使用JSR250的注释
 * 				@PostConstruct:在bean创建完成并且属性赋值完成后来执行初始化方法
 * 				@PreDestroy :在容器销毁bean之前进行清理工作
 * 		方法四:写一个BeanPostProcessor接口的实现类,BeanPostProcessor也叫bean的后置处理器
 * 				里面的两个方法
 * 				postProcessBeforeInitialization:在初始化之前工作,刚刚对象创建后
 * 				postProcessAfterInitialization:在初始化之后工作,所有初始化做完后
 */
@ComponentScan("jane.bean")
@Configuration
public class MyConfig3
{
    
    
	@Bean(initMethod = "init",destroyMethod = "destroy")
	public Car car()
	{
    
    
		return new Car();
	} 
	@Bean
	public BenzCar benzcar()
	{
    
    
		return new BenzCar();
	} 
}

对应的bean

package jane.bean;

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

package jane.bean;

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

public class BenzCar implements InitializingBean,DisposableBean
{
    
    
	public BenzCar()
	{
    
    
		System.out.println("BenzCar constructor...");
	}
	public void destroy()
	{
    
    
		System.out.println("BenzCar destroy...");
	}
	@Override
	public void afterPropertiesSet() throws Exception
	{
    
    
		System.out.println("BenzCar init...");
	}
}

package jane.bean;

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

import org.springframework.stereotype.Component;

@Component
public class AudiCar
{
    
    
	public AudiCar()
	{
    
    
		System.out.println("AudiCar constructor...");
	}
	@PostConstruct
	public void init()
	{
    
    
		System.out.println("AudiCar init...");
	}
	@PreDestroy
	public void destroy()
	{
    
    
		System.out.println("AudiCar destroy...");
	}
}

后置处理器

package jane.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
//后置处理器,初始化前后进行处理工作,而且需要将后置处理器加入到容器中
@Component
public class MyBeanPostProcessor implements BeanPostProcessor
{
    
    

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
	{
    
    
		System.out.println("postProcessBeforeInitialization,名字: "+beanName+"工作");
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
	{
    
    
		System.out.println("postProcessAfterInitialization,名字: "+beanName+"工作");
		return bean;
	}
}

测试

package jane;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import jane.config.MyConfig3;

public class IOCTestLifeCycle
{
    
    
	@org.junit.Test
	public void test1()
	{
    
    
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig3.class);
		System.out.println("IOC容器创建完成");
		applicationContext.close();
		System.out.println("IOC容器关闭");
	}
}

结果

八月 28, 2020 10:53:22 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3630fb71: startup date [Fri Aug 28 22:53:22 CST 2020]; root of context hierarchy
postProcessBeforeInitialization,名字: org.springframework.context.event.internalEventListenerProcessor工作
postProcessAfterInitialization,名字: org.springframework.context.event.internalEventListenerProcessor工作
postProcessBeforeInitialization,名字: org.springframework.context.event.internalEventListenerFactory工作
postProcessAfterInitialization,名字: org.springframework.context.event.internalEventListenerFactory工作
postProcessBeforeInitialization,名字: myConfig3工作
postProcessAfterInitialization,名字: myConfig3工作
AudiCar constructor...
postProcessBeforeInitialization,名字: audiCar工作
AudiCar init...
postProcessAfterInitialization,名字: audiCar工作
car constructor...
postProcessBeforeInitialization,名字: car工作
car init...
postProcessAfterInitialization,名字: car工作
BenzCar constructor...
postProcessBeforeInitialization,名字: benzcar工作
BenzCar init...
postProcessAfterInitialization,名字: benzcar工作
IOC容器创建完成
八月 28, 2020 10:53:22 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3630fb71: startup date [Fri Aug 28 22:53:22 CST 2020]; root of context hierarchy
BenzCar destroy...
car destroy...
AudiCar destroy...
IOC容器关闭

原码查看BeanPostProcessor原理

首先想初始化容器
在这里插入图片描述
进去看看如何初始化容器:刷新容器
在这里插入图片描述
调用刷新容器里面的finishBeanFactoryInitialization方法初始化工厂
在这里插入图片描述
然后想初始化单实例bean
在这里插入图片描述
想得到bean
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
最后还是去创建的,因为这是一个新的容器
在这里插入图片描述
创建bean中
populateBean(beanName, mbd, instanceWrapper);是给bean赋值
后面的
exposedObject = initializeBean(beanName, exposedObject, mbd);是开始初始化
在这里插入图片描述
进去initializeBean
里面就是先执行applyBeanPostProcessorsBeforeInitialization
然后进行初始化,执行invokeInitMethods
然后再执行applyBeanPostProcessorsAfterInitialization
在这里插入图片描述
进入applyBeanPostProcessorsBeforeInitialization查看一下
它就是遍历每一个BeanPostProcessor
挨个去执行postProcessBeforeInitialization
一旦返回null就会跳出for循环,不会执行后面的beanProcessor.postProcessBeforeInitialization
在这里插入图片描述

spring底层对BeanPostProcessor的使用

bean赋值,注入其他组件,@Autowired,生命周期注解功能都是BeanPostProcessor起作用的
比如上面使用的@PostConstruct和注解,
我们原码查看一下对应的实现类InitDestroyAnnotationBeanPostProcessor
在这里插入图片描述
在这里打个断点
在这里插入图片描述
找它前面执行的方法,
找到对应的生命周期的注解findLifecycleMetadata
在这里插入图片描述
然后利用反射执行初始化方法
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43416157/article/details/108287587