BeanPostProcessor接口
1 | public interface BeanPostProcessor { |
BeanPostProcessor的注册
1 | //TODO |
BeanPostProcessor的调用
图
"详细
factory.getObject后调用postprocessor.postProcessAfterInitialization
doCreateBean之前调用InstantiationAwareBeanPostProcessor
1 | protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) { |
1 | protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) |
1 |
|
- createBeanInstance中在调用构造器之前调用Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName)
1 | protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(Class<?> beanClass, String beanName) |
- 在实例化后,(缓存实例化)填充属性前调用applyMergedBeanDefinitionPostProcessors来允许修改MergedBeanDefinition
1 | protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) |
- 在缓存实例化引用的时候,允许对缓存的实例化进行修改/代理
1 | addSingletonFactory(beanName, new ObjectFactory<Object>() { |
1 | protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) { |
- 在autowireByName/autowireByType前后,可以让InstantiationAwareBeanPostProcessors有机会改动bean
1 | if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { |
1 | if (hasInstAwareBpps) { |
- 在初始化bean(initializeBean)的时候,在调用the bean implements InitializingBean or a custom init method前后调用applyBeanPostProcessorsBeforeInitialization,applyBeanPostProcessorsAfterInitialization
1 | public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) |
1 | public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) |
BeanPostProcessor的应用
- 创建AOP代理类AnnotaionAwareAspectJAutoProxyCreator