Spring annotation driven (life cycle): bean life cycle, the original code view the principle and use of BeanPostProcessor

bean life cycle

Mainly focus on the process of bean creation, initialization, and destruction

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();
	} 
}

Corresponding 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...");
	}
}

Post processor

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;
	}
}

test

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容器关闭");
	}
}

result

八月 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容器关闭

Original code view BeanPostProcessor principle

First of all I want to initialize the container
Insert picture description here
into the container to see how to initialize: refresh container
Insert picture description here
calls finishBeanFactoryInitialization refresh method to initialize container inside the factory
Insert picture description here
and then want to initialize a single instance of the bean
Insert picture description here
wants bean
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
finally to create, because this is a new container
Insert picture description here
to create the bean
populateBean(beanName, mbd, instanceWrapper);is assigned to the bean
the latter
exposedObject = initializeBean(beanName, exposedObject, mbd);is beginning to initialize
Insert picture description here
go initializeBean
which is the first implementation of applyBeanPostProcessorsBeforeInitialization
then initialize, execute invokeInitMethods
then perform applyBeanPostProcessorsAfterInitialization
Insert picture description here
enter applyBeanPostProcessorsBeforeInitialization look at
it is through each BeanPostProcessor
one by one to perform postProcessBeforeInitialization
Once out of the for loop will return null, the latter will not be executed beanProcessor.postProcessBeforeInitialization
Insert picture description here

The use of BeanPostProcessor at the bottom of spring

bean assignment, inject other components, @ Autowired, lifecycle annotations are BeanPostProcessor act
such as @PostConstruct and notes used above,
we look at the original code corresponding implementation class InitDestroyAnnotationBeanPostProcessor
Insert picture description here
here to make a break
Insert picture description here
to find it performed in front Method,
find the annotation of the corresponding life cycle findLifecycleMetadata
Insert picture description here
and then use reflection to execute the initialization method
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43416157/article/details/108287587