0
点赞
收藏
分享

微信扫一扫

Spring源码分析 (getBean过程详解) (十四)


Spring中有一个闭环:

Spring源码分析 (getBean过程详解) (十四)_java-ee

getBean -> 创建依赖 -> getBean

过程:

1.createBeanInstance实例化 bean 2.populateBean属性注入 3.initializingBean初始化对象

createBeanInstance:getBean -> doGetBean -> createBean -> doCreateBean

initializingBean初始化对象: 1.激活Aware 2.后置处理器的应用 3.init方法

Spring源码分析 (getBean过程详解) (十四)_spring_02


文章目录

  • ​​1.过程一​​
  • ​​2.过程二​​
  • ​​3.过程三​​

1.过程一

Spring源码分析 (getBean过程详解) (十四)_ide_03

InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation()

// 1, 初始化之前
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("MyInstantiationAwareBeanPostProcessor postProcessBeforeInstantiation");
return null;
}

判断是否创建了对象

SmartInstantiationAwareBeanPostProcessor.determineCandidateConstructors()

返回我们要使用的构造器候选列表, 默认使用无参构造器, 然后创建Bean实例, 使用BeanWrapper

@Override
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
System.out.println("MySmartInstantiationAwareBeanPostProcessor determineCandidateConstructors");
return null;
}

2.过程二

Spring源码分析 (getBean过程详解) (十四)_java-ee_04

BeanWrapper为装饰者模式。

MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition()

合并bd信息, 再修改beanDefinition

// 合并bd信息, 再修改beanDefinition
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
System.out.println("MyMergedBeanDefinitionPostProcessor postProcessMergedBeanDefinition");
}

InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation()

初始化之后处理

@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
System.out.println("MyInstantiationAwareBeanPostProcessor postProcessAfterInstantiation");
return true;
}

InstantiationAwareBeanPostProcessor.postProcessProperties()

处理属性值, 利用反射进行赋值

@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
System.out.println("MyInstantiationAwareBeanPostProcessor postProcessProperties");
return null;
}

这里的PropertyValues 为迭代器模式。去赋值

3.过程三

Spring源码分析 (getBean过程详解) (十四)_初始化_05

AbstractAutowireCapableBeanFactory.invokeAwareMethods()

激活Aware

BeanPostProcessor.postProcessBeforeInitialization()

进行初始化之前的增强处理, 改变之前创建Bean 的实例

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcessor postProcessBeforeInitialization");
return bean;
}

InitializingBean.afterPropertiesSet()

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

BeanPostProcessor.postProcessAfterInitialization()

初始化之后的增强处理, 改变之前创建的Bean 实例

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcessor postProcessAfterInitialization");
return bean;
}



举报

相关推荐

0 条评论