Bean的初始化流程
通过main函数启动spring容器加载bean时,会有以下流程:1、通过实现BeanFactoryPostProcessor并覆盖postProcessBeanFactory()可以在spring容器初始化时拿到ConfigurableListableBeanFactory的实例,ConfigurableListableBeanFactory是所有bean的容器,也是BeanFactory的基础
通过main函数启动spring容器加载bean时,会有以下流程:
1、通过实现BeanFactoryPostProcessor并覆盖postProcessBeanFactory()可以在spring容器初始化时拿到ConfigurableListableBeanFactory的实例,ConfigurableListableBeanFactory是所有bean的容器,也是BeanFactory的基础实现。此处可以对ConfigurableListableBeanFactory做扩展。
2、执行当前bean的构造函数
3、实现了BeanNameAware的接口会执行setBeanName()可以拿到bean的id
4、实现了BeanFactoryAware的接口会执行setBeanFactory()可以拿到beanFactory实例
5、执行init-method即@PostConstruct
6、实现了InitializingBean的接口的会执行afterPropertiesSet(),该方法没有参数,仅仅是作为通知
7、继承了InstantiationAwareBeanPostProcessorAdapter并重写了其中的postProcessBeforeInstantiation方法则会拿到bean的Class类型实例和beanName
8、继承了InstantiationAwareBeanPostProcessorAdapter并重写了其中的postProcessAfterInstantiation方法则会拿到bean的实例和beanName
9、继承了InstantiationAwareBeanPostProcessorAdapter并重写了其中的postProcessPropertyValues方法则会拿到bean的实例、bean的名称、属性值、属性描述符
10、继承了InstantiationAwareBeanPostProcessorAdapter并重写了其中的postProcessAfterInitialization方法则会拿到bean的实例和beanName
11、执行destroy前的回调
12、destroy之后的回调
一个栗子:
MyBean:
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.beans.PropertyDescriptor;
@Service
public class MyBean implements BeanPostProcessor, InstantiationAwareBeanPostProcessor, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
@Autowired
private MyBeanFactoryPostProcessor myBeanFactoryPostProcessor;
public MyBean(){
System.out.println("MyBean");
}
@PostConstruct
public void init(){
System.out.println("init");
}
@PreDestroy
public void preDestroy(){
System.out.println("preDestroy");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("setBeanFactory");
}
@Override
public void setBeanName(String s) {
System.out.println("setBeanName");
}
@Override
public void destroy() throws Exception {
System.out.println("destroy");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet");
}
}
MyBeanFactoryPostProcessor:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Service;
@Service
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.println("configurableListableBeanFactory");
}
}
MyInstantiationAwareBeanPostProcessor
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.stereotype.Service;
import java.beans.PropertyDescriptor;
/**
* 通过继承InstantiationAwareBeanPostProcessorAdapter并覆盖其中的方法来实现多个扩展
*/
@Service
public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
// 接口方法、实例化Bean之前调用
@Override
public Object postProcessBeforeInstantiation(Class beanClass,String beanName) throws BeansException {
System.out.println("postProcessBeforeInstantiation");
return null;
}
// 接口方法、实例化Bean之后调用
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessAfterInitialization");
return bean;
}
// 接口方法、设置某个属性时调用
@Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
throws BeansException {
System.out.println("postProcessPropertyValues");
return pvs;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInstantiation");
return true;
}
}
TestMyBeanLifecycle
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMyBeanLifecycle {
public static void main(String[] args) {
System.out.println("加载spring容器");
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("application.xml");
MyBean myBean = (MyBean) classPathXmlApplicationContext.getBean("myBean");
//执行下优雅关机,否则bean的destroy方法不会被调用
classPathXmlApplicationContext.registerShutdownHook();
}
}
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:annotation-config />
<!--自动扫描含有@Service将其注入为bean -->
<context:component-scan base-package="com.daquan._202007._01.spring.beanlifecycle" />
</beans>
运行结果:
configurableListableBeanFactory
MyBean
setBeanName
setBeanFactory
init
afterPropertiesSet
postProcessBeforeInstantiation
postProcessAfterInstantiation
postProcessPropertyValues
postProcessAfterInitialization
postProcessBeforeInstantiation
postProcessAfterInstantiation
postProcessPropertyValues
postProcessAfterInitialization
preDestroy
destroy
更多推荐
所有评论(0)