AOP是面向切面编程,是相当于OOP面向对象编程而言的。Spring的AOP存在的目的是为了解耦。AOP可以让一组类共享相同的行为。而OOP中只能通过继承类和实现接口来实现,但是这样的缺点是会使代码的耦合度增加,且类继承只能为单继承,阻碍更多行为添加到一组类上,AOP的出现弥补了OOP的不足。
使用@EnableAspectJAutoProxy注解开启Spring对AspectJ的支持。
添加@EnableAspectJAutoProxy注解,表示开启AOP代理自动配置,如果使用@EnableAspectJAutoProxy注解,表示使用cglib进行代理对象的生成。设置@EnableAspectJAutoProxy(exposeProxy=true)表示通过AOP框架暴露该代理对象,aopContext能够访问。
下面看一下@EnableAspectJAutoProxy这个类的源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
/**
* Indicate whether subclass-based (CGLIB) proxies are to be created as opposed
* to standard Java interface-based proxies. The default is {@code false}.
*/
// true--使用CGLIB基于类创建代理,false--使用java接口创建代理
boolean proxyTargetClass() default false;
/**
* Indicate that the proxy should be exposed by the AOP framework as a {@code ThreadLocal}
* for retrieval via the {@link org.springframework.aop.framework.AopContext} class.
* Off by default, i.e. no guarantees that {@code AopContext} access will work.
* @since 4.3.1
*/
// 是否通过aop矿建暴露该代理对象,aopContext能够访问
boolean exposeProxy() default false;
}
通过上面的代码可以看出@EnableAspectJProxy把AspectJAutoProxyRegistrar.class对象导入到了容器中。利用AspectJAutoProxyRegistrar给容器中注册了一个AnnotationAwareAspectAutoProxyCreator。
AspectJAutoProxyRegistrar
AspectAutoProxyRegistrar类中的registerBeanDefinitions方法主要实现了两个功能:
- 1.注册AnnontationAwareAspectJAutoProxyCreator
- 2.获取@EnableAspectJAutoProxy注解的属性信息
class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
/**
* Register, escalate, and configure the AspectJ auto proxy creator based on the value
* of the @{@link EnableAspectJAutoProxy#proxyTargetClass()} attribute on the importing
* {@code @Configuration} class.
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
//注册AnnotationAwareAspectJAutoProxyCreator
AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
//获取@EnableAspectJAutoProxy注解的属性信息
AnnotationAttributes enableAspectJAutoProxy = AnnotationConfigUtils.attributesFor(importingClassMetadata,EnableAspectJAutoProxy.class);
if (enableAspectJAutoProxy != null) {
if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
}
if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
}
}
}
}
总结
大致流程如下:
1、传入配置类,创建IOC容器
2、注册配置类,调用refresh()刷新容器
3、registerBeanPostProcessors(beanFactory):注册bean的后置处理器来方便拦截创建bean的创建
1)先获取ioc容器已经定义了的需要创建对象的所有BeanPostProcessor
2)给容器中加别的BeanPostProcessor
3)优先注册实现了PriorityOrdered接口的BeanPostProcessor
4)再给容器中注册实现了Ordered接口的BeanPostProcessor
5)注册没实现优先级接口的BeanPostProcessor
6)注册BeanPostProcessor。实际上就是创建BeanPostProcessor对象,保存在容器中
创建internalAutoProxyCreator的BeanPostProcessor【AnnotationAwareAspectJAutoProxyCreator】
1)创建Bean实例
2)populateBean(beanName,mbd,instanceWrapper);给bean的各种属性赋值
3)initializeBean:初始化bean
1)invokeAwareMethods():处理Aware接口的方法回调
2)applyBeanPostProcessorsBeforeInitialization():应用后置处理器的postProcessBeforeInitialization()
3)invokeInitMethods():执行自定义的初始化方法
4)applyBeanPostProcessorsAfterInitialization():执行后置处理器的postProcessAfterInitialization()
4)BeanPostProcessor(AnnotationAwareAspectJAutoProxyCreator)创建成功:–》aspectJAdvisorsBuilder
7)把BeanPostProcessor注册到BeanFactory中:beanFactory.addBeanPostProcessor(postProcessor);
4、finishBeanFactoryInitialIization(beanFactory);完成BeanFactory初始化工作;创建剩下的单实例bean
1)遍历获取容器中所有的Bean,依次创建对象getBean(beanName);
getBean->doGetBean()->getSingleton()
2)创建bean
1)先从缓存中获取当前bean,如果能获取到,说明是被创建过的,直接使用,否则再创建
只要创建好的Bean都会被缓存起来
2)createBean();创建bean
【BeanPostProcessor是在Bean对象创建完成初始化前后调用的】
【InstantiationAwareBeanPostProcessor是在创建Bean实例之前先尝试用后置处理器返回对象的】
1)resolveBeforeInstantiation(beanName, mbdToUse); 解析BeforeInstantiation
希望后置处理器在此能返回一个代理对象;如果能返回代理对象就使用,如果不能就继续
1)后置处理器先尝试返回对象
bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
拿到所有后置处理器,如果是InstantiationAwareBeanPostProcessor就执行postProcessorsBeforeInstantiation
if (bean != null) {
bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
}
2)doCreateBean(beanName, mbdToUse, args); 真正去创建一个bean实例;和3.6流程一样