何为BeanPostProcessor?
称为Bean的后置处理器,在Bean实例化前后对bean“赋能”。
重点关注以下五个接口
1、BeanPostProcessor
- postProcessBeforeIninialization
- postProcessAfterInitialization
2、InstantationAwareBeanPostProcessor
- processProperties
- processPropertyValue
3、SmartInstantationAwareBeanPostProcessor
- predictBeanType
- determineCanditateConstructors
- getEarlyRenference
4、MergedBeanDefinitionBeanPostProcessor
- postProcessMergedBeanDefinition
- resetBeanDefinition
5、DestructionAwareBeanPostProcessor
- postProcessBeforeDestruction
- requiresDestruction
AutowiredAnnotationBeanPostProcessor
自动注入支持的类型 @Autowired @Value @Inject
determineCandidateConstructors()方法
1、支持Lookup注解,扫描正在实例化的类中的方法,看看方法上是否有@Lookup注解,如果有创建一个LookupOverride对象,则mbd.getMethodOverrides().addOverride(override);
2、获取实例化类中的所有构造器,rawCandidates = beanClass.getDeclaredConstructors();
3、获取到构造函数上的@Autowired注解信息。
4、返回有@Autowired注解的构造函数的数组。
这个方法主要是用来支持@Lookup和获取所有的有@Autowired注解的构造方法,用以确定bean实例化使用的构造方法是哪一个。
postProcessMergedBeanDefinition()方法
收集所有有@Autowired @Value @Inject的方法和属性,封装成InjectionMetadata对象,放入BD中
resetBeanDefinition()方法
postProcessProperties()方法
实现DI(依赖注入)
属性注入:
方法参数注入:
postProcessPropertyValues()方法
实现DI(依赖注入)
CommonAnnotationBeanPostProcessor的父类InitDestroyAnnotationBeanPostProcessor
这个类主要负责bean的生命周期回调
postProcessMergedBeanDefinition()方法
将bean的生命周期回调方法封装成LifecycleMetadata,放入map中缓存中。
bean的生命周期回调
1、实现InitializingBean接口,重写afterPropertiesSet()方法
2、实现DisposableBean接口,重写destory()方法
3、方法加上@PostConstruct注解
4、方法加上@PreDestroy注解
postProcessBeforeInitialization()方法
执行bean的initMethods
postProcessBeforeDestruction()方法
执行bean的destoryMethods
CommonAnnotationBeanPostProcessor
postProcessMergedBeanDefinition()方法
1、先调用父类收集生命周期回调方法,
2、再实现对@Resource注解的支持
resetBeanDefinition()方法
postProcessProperties()方法
@Resource DI依赖注入
postProcessPropertyValues()方法
@Resource DI依赖注入
Spring定义了这么多的BeanPostProcessor,哪些是Spring容器启动时,默认会使用的呢?
默认就会使用的几个bean后置处理器有:
- ApplicationContextAwareProcessor
- AutowiredAnnotationBeanPostProcessor
- CommonAnnotationBeanPostProcessor
- EventListenerMethodProcessor
☆ 当我们需要使用AOP时,在@Configuration类上开启允许AOP时,会使用另一个后置处理器:AnnotationAwareAspectJAutoProxyCreator
何时被放入到BeanFactory中的呢?
在实例化AnnotationConfigApplicationContext#reader的时候,将AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就放入到了BeanFactory中的beanDefinitionMap
在AbstractApplicationContext#prepareBeanFactory中直接将ApplicationContextAwareProcessor放入到BeanFactory中的beanPostProcessors集合中
ApplicationContextAwareProcessor有什么作用呢?
如果bean的Class是这些接口的子类,那么就会对这个bean进行“赋能”。
- org.springframework.context.ApplicationContextAware
- org.springframework.context.MessageSourceAware
- org.springframework.context.ApplicationEventPublisherAware
- org.springframework.context.ResourceLoaderAware
- org.springframework.context.EmbeddedValueResolverAware
- org.springframework.context.EnvironmentAware
org.springframework.context.support.ApplicationContextAwareProcessor#postProcessBeforeInitialization
↓
org.springframework.context.support.ApplicationContextAwareProcessor#invokeAwareInterfaces
AutowiredAnnotationBeanPostProcessor有什么作用呢?
@Autowired 依赖注入
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
↓
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyMergedBeanDefinitionPostProcessors
↓
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#postProcessMergedBeanDefinition
这篇文章可以参考:https://www.jianshu.com/p/f6c3464fa84a
CommonAnnotationBeanPostProcessor有什么作用呢?
这篇文章可以参考:https://www.cnblogs.com/teach/p/12688608.html
ImportAwareBeanPostProcessor有什么作用呢?
加了@Configuration注解的类,会进行Cglib代理,而这个代理类实现了EnhancedConfiguration接口,所以ImportAwareBeanPostProcessor会对这些类的bean进行“赋能”。
org.springframework.context.annotation.ConfigurationClassPostProcessor.ImportAwareBeanPostProcessor
上一篇 二、Spring内置的BeanFactoryPostProcessor及其作用