Spring 5.x Source trip thirty-five getBean a processor extension points
Flowchart extension point processor
First brought this figure, in the process of creating a basic bean that these, there may be other places I missed, to see the great God reminded ah, next time up. Then we come to the next one of these extensions do something.
Before instantiating resolveBeforeInstantiation
Eat before instantiation
@Nullable
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
Object bean = null;
if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
// Make sure bean class is actually resolved at this point.
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {//非合成的,且有InstantiationAwareBeanPostProcessors
Class<?> targetType = determineTargetType(beanName, mbd);//获取类型
if (targetType != null) {
bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
if (bean != null) {
bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
}
}
}
mbd.beforeInstantiationResolved = (bean != null);//是否解析了
}
return bean;
}
Instantiated before applyBeanPostProcessorsBeforeInstantiation
InstantiationAwareBeanPostProcessor
Processor interface, such as an object can replace the other.
@Nullable
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
if (result != null) {
return result;
}
}
}
return null;
}
applyBeanPostProcessorsAfterInitialization initialization
The main front is returned if there is not empty, it will execute this to initialize.
@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;
}
Actual extension points
We just try extension point, I want to MyConfig
class replaced. Replaced and spring
without a class-dependent:
MyInstantiationAwareBeanPostProcessor extension classes
postProcessBeforeInstantiation
Method returns MyBeforeInstantiation
the object, and then postProcessAfterInitialization
initialization properties age
.
@Component
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor
{
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
return new MyBeforeInstantiation();
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
MyBeforeInstantiation myBeforeInstantiation=(MyBeforeInstantiation)bean;
myBeforeInstantiation.age=10;
return null;
}
}
Test code
@Test
public void BeanDefinitionRegistryPostProcessorTest0() throws Exception {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(MyConfig.class);
applicationContext.refresh();
MyBeforeInstantiation myBeforeInstantiation = applicationContext.getBean(MyBeforeInstantiation.class);
System.out.println(myBeforeInstantiation.age);
In MyConfig
previous instances of replacement:
After initialized age
:
Last singleton becomes this:
Take out:
Well, here today, and I hope to help study and understand, the great God saw do not spray, only their learning comprehension, ability limited him to bear.