Hook扩展点
- Hook钩子是一种编程思想,也是监听者模式的一种实现。在控制中心的执行过程(可理解也模板方法)中,会调用分阶段或者分层次来调用用户注册进来的Hook方法,用来修改或者监听控制中心的逻辑。
- 如果需要将控制中心的内容暴露出来,可以选择Hook或者Publish Event
Spring框架在org.springframework.context.support.AbstractApplicationContext#refresh
的时候,留下了非常多的Hook,用于灵活接口框架内部实现及开发者功能扩展。
Bean生命周期Hook
Bean的实例化会经过如下流程,最终调用AbstractAutowireCapableBeanFactory#createBean(java.lang.Class<T>)
实例化Bean
AbstractApplicationContext#refresh -> finishBeanFactoryInitialization为容器填充相关对象,实例化bean(实例化剩余的bean)
实现了后置处理器的bean均不在这里进行注册 -> preInstantiateSingletons生产单例 -> getBean -> doCreateBean。
Hook全部列表
@Component
public class AllBeanPostProcessor implements InstantiationAwareBeanPostProcessor,
SmartInstantiationAwareBeanPostProcessor,
MergedBeanDefinitionPostProcessor,
DestructionAwareBeanPostProcessor {
// 来源InstantiationAwareBeanPostProcessor Instantiation-实例化的意思,反射调用构造方法实例化
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
return true;
}
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
return null;
}
// 来源BeanPostProcessor Initialization-初始化,实例化后用户可以手动调用方法初始化
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
// 来源SmartInstantiationAwareBeanPostProcessor
@Override
public Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
return SmartInstantiationAwareBeanPostProcessor.super.predictBeanType(beanClass, beanName);
}
@Override
public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
return SmartInstantiationAwareBeanPostProcessor.super.determineCandidateConstructors(beanClass, beanName);
}
@Override
public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
return SmartInstantiationAwareBeanPostProcessor.super.getEarlyBeanReference(bean, beanName);
}
// 来源MergedBeanDefinitionPostProcessor
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
}
@Override
public void resetBeanDefinition(String beanName) {
MergedBeanDefinitionPostProcessor.super.resetBeanDefinition(beanName);
}
// 来源DestructionAwareBeanPostProcessor
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
}
@Override
public boolean requiresDestruction(Object bean) {
return true;
}
}
-
在反射实例化之前,会
applyBeanPostProcessorsBeforeInstantiation
确定是否在回调中实例化,如果实例化,则applyBeanPostProcessorsAfterInitialization
来执行全部BeanPostProcessor#postProcessAfterInitialization
来后置处理Bean。try { // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. Object bean = resolveBeforeInstantiation(beanName, mbdToUse); if (bean != null) { return bean; } } if (targetType != null) { bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName); if (bean != null) { bean = applyBeanPostProcessorsAfterInitialization(bean, beanName); } }
-
AbstractAutowireCapableBeanFactory#createBeanInstance
会通过determineConstructorsFromBeanPostProcessors
,选择合适的Constructor,没有指定则使用autowireConstructor
自动推理使用合适的构造方法。// Candidate constructors for autowiring? Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); }