AOP原理
看给容器当中注入了什么组件?这个组件的功能是什么,什么时候开始工作?
1.@EnableAspectJAutoProxy
@EnableAspectJAutoProxy开启基于注解的AOP
@Import(AspectJAutoProxyRegistrar.class) 为容器中注册bean
AspectJAutoProxyRegistrar 45
AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
AopConfigUtils 121
注册bean的类型是:AnnotationAwareAspectJAutoProxyCreator.class
注册bean的id是:internalAutoProxyCreator
2.AnnotationAwareAspectJAutoProxyCreator
继承关系
AnnotationAwareAspectJAutoProxyCreator
AspectJAwareAdvisorAutoProxyCreator
AbstractAdvisorAutoProxyCreator
AbstractAutoProxyCreator implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware
SmartInstantiationAwareBeanPostProcessor(bean的后置处理器 在bean初始化前后做事)
BeanFactoryAware:自动装配BeanFactory
SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor
InstantiationAwareBeanPostProcessor extends BeanPostProcessor
AbstractAutoProxyCreator
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
Object cacheKey = getCacheKey(beanClass, beanName);
if (beanName == null || !this.targetSourcedBeans.contains(beanName)) {
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return null;
}
}
// Create proxy here if we have a custom TargetSource.
// Suppresses unnecessary default instantiation of the target bean:
// The TargetSource will handle target instances in a custom fashion.
if (beanName != null) {
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
this.targetSourcedBeans.add(beanName);
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
}
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) {
return true;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
3.流程分析
3.1 源码跟踪
1. 传入配置类,创建IOC容器 (AnnotationConfigApplicationContext 81)
2. 注册配置类之后,调用refresh()刷新容器(AnnotationConfigApplicationContext 84)
3. registerBeanPostProcessors(beanFactory)注册bean的后置处理器,来拦截bean的创建(AbstractApplicationContext 528)
3.1 先获取IOC容器已经定义了需要创建对象的所有BeanPostProcessor(PostProcessorRegistrationDelegate 188)
3.2 注册BeanPostProcessorChecker后置处理器(PostProcessorRegistrationDelegate 194)
3.3 优先注册实现了PriorityOrdered接口的BeanPostProcessor(PostProcessorRegistrationDelegate 219)
3.4 再给容器中注册实现了Ordered接口的BeanPostProcessor(PostProcessorRegistrationDelegate223)
3.5 注册没实现优先级接口的BeanPostProcessor(PostProcessorRegistrationDelegate 235)
3.6 注册BeanPostProcessor,实际上就是创建BeanPostProcessor对象,保存在容器中
以创建internalAutoProxyCreator的BeanPostProcessor【AnnotationAwareAspectJAutoProxyCreator】为例子
3.6.1 创建bean (PostProcessorRegistrationDelegate 225->AbstractBeanFactory 202 )
3.6.1.1 AbstractBeanFactory 202
@Override
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
return doGetBean(name, requiredType, null, false);
}
3.6.1.2 AbstractBeanFactory 302 创建一个单实例bean
3.6.1.3 DefaultSingletonBeanRegistry 230
singletonObject = singletonFactory.getObject();
3.6.1.4 AbstractBeanFactory 306
return createBean(beanName, mbd, args);
3.6.1.5 AbstractAutowireCapableBeanFactory 483
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
3.6.2 populateBean;给bean的各种属性赋值 (AbstractAutowireCapableBeanFactory 553)
3.6.3 initializeBean:初始化bean (AbstractAutowireCapableBeanFactory 555)
3.6.3.1 invokeAwareMethods():处理Aware接口的方法回调(AbstractAutowireCapableBeanFactory 1615)
3.6.3.2 applyBeanPostProcessorsBeforeInitialization():应用后置处理器的postProcessBeforeInitialization (AbstractAutowireCapableBeanFactory 1620)
3.6.3.3 invokeInitMethods();执行bean自定义的初始化方法(AbstractAutowireCapableBeanFactory 1624)
3.6.3.4 applyBeanPostProcessorsAfterInitialization();应用后置处理器的postProcessAfterInitialization(AbstractAutowireCapableBeanFactory 1633)
3.6.4 BeanPostProcessor(AnnotationAwareAspectJAutoProxyCreator)创建成功;--》aspectJAdvisorsBuilder
3.7 把BeanPostProcessor注册到BeanFactory中 beanFactory.addBeanPostProcessor(postProcessor);(PostProcessorRegistrationDelegate 251)
===步骤三是创建和注册AnnotationAwareAspectJAutoProxyCreator的过程===
4 finishBeanFactoryInitialization(beanFactory);完成BeanFactory初始化工作;创建剩下的单实例bean(AbstractApplicationContext 543-->867)
4.1 遍历获取容器中所有的Bean,依次创建对象getBean(beanName);(DefaultListableBeanFactory 735)
4.2 创建bean(只要创建好的bean都会被容器给缓存起来)
AnnotationAwareAspectJAutoProxyCreator在所有bean创建之前会有一个拦截,InstantiationAwareBeanPostProcessor,会调用postProcessBeforeInstantiation()
getBean->doGetBean()->getSingleton()
4.2.1 先从缓存中获取bean,如果能获取到说明bean是之前创建的,直接使用否则再创建。(DefaultListableBeanFactory 761-->AbstractBeanFactory 243-->AbstractBeanFactory 302-->AbstractBeanFactory 306 )
4.2.2 createBean(); 创建bean
AnnotationAwareAspectJAutoProxyCreator在所有bean创建之前会有一个拦截,InstantiationAwareBeanPostProcessor,会调用postProcessBeforeInstantiation()
4.2.2.1 resolveBeforeInstantiation() 给后置处理一个机会返回一个代理对象,如果不能返回就继续(AbstractAutowireCapableBeanFactory 473-->1011)
4.2.2.2 doCreateBean(beanName, mbdToUse, args);真正的去创建一个bean实例;和3.6流程一样