有以下几个疑问?
基于xml的方式我们知到,填充bean属性阶段将xml中定义的值填入。那么问题:
@Autowire
注解的属性何时注入?也是填充bean属性阶段?答:是的属性填充都是在填充阶段,即
populateBean
方法中进行的@Autowire
、@Resource
等如何实现属性注入的呢?答:定义特殊的BeanPostProcessor,这些BeanPostProcessor是
MergedBeanDefinitionPostProcessor
和InstantiationAwareBeanPostProcessor
的子类在填充阶段就开始使用了。Spring中开启注解的配置是
<context:annotation-config/>
,那它的作用是啥?这个配置的作用就是引入上面所说的特殊的BeanPostProcessor,来完成对注解的解析 主要有:
AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
———————————————— 版权声明:本文为CSDN博主「Beckio」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/Beckio/article/details/79421335
调用途径解析
函数式接口,作为一个入参传入
getSingleton()
方法中 ```java @FunctionalInterface public interface ObjectFactory{ /**
- Return an instance (possibly shared or independent)
- of the object managed by this factory.
- @return the resulting instance
- @throws BeansException in case of creation errors */ T getObject() throws BeansException;
}
2. `getSingleton`方法在方法内部,会调用这个lamda,实际进入`return createBean`这句
```java
sharedInstance = getSingleton(beanName, () -> {
//getSingleton 最终是触发这个 方法的一个调用
try {
//createBean
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
createBean
方法最后进入doCreateBean
就是整个Bean的创建阶段的生命周期
生命周期-创建阶段
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
// Instantiate the bean.
BeanWrapper instanceWrapper = createBeanInstance(beanName, mbd, args);
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
populateBean(beanName, mbd, instanceWrapper);
exposedObject = initializeBean(beanName, exposedObject, mbd);
//判断提前引用出去的对象,是否有地址上的变化
if (earlySingletonExposure) {
}
//注册实现了DisposableBean的bean
registerDisposableBeanIfNecessary(beanName, bean, mbd);
return exposedObject;
}
- 实例化bean对象
- 提前暴露bean对象在三级缓存中
- 填充属性
初始化bean
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
invokeAwareMethods(beanName, bean);
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
invokeInitMethods(beanName, wrappedBean, mbd);
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
return wrappedBean;
}
- 调用
Aware
接口的实现方法 调用
BeanPostProcessor
的postProcessBeforeInitialization
方法public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current =
processor.postProcessBeforeInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
调用初始化方法
调用
BeanPostProcessor
的postProcessAfterInitialization
方法@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessAfterInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
判断提前被引用的bean对象是否有变化
- 若实现了
DisposableBean
则注册一下