(Spring)AOP源码解析

147 阅读5分钟

Spring AOP理解

OOP表示面向对象编程,是一种编程思想,AOP表示面向切面编程,也是一种编程思想,Spring提供了一套机制,可以让我们更加容易的来进行AOP,所以这套机制我们也可以称之为 Spring AOP。

动态代理

说AOP一定是绕不开动态代理的,代理模式的解释:为其他对象提供一种代理以控制对这个对象的访问,增强一个类中的某个方法,对程序进行扩展。
比如现在有个类:UserService

public class UserService {
    public void test(){
        System.out.println("test!");
    }
}

我们想使用他的test方法是需要先实例化一个UserService对象然后去调用test()。
如果我们现在想在不修改UserService类的源码前提下,给test()增加额外逻辑,那么就可以使用动态代理机制来创建UserService对象了。

Cglib:

利用ASM框架(字节码编程),对代理对象类生成的class文件加载进来,通过修改其字节码生成子类来处理:对指定的类生成一个子类,覆盖其中的方法,并覆盖其中方法的增强,但是因为采用的是继承,所以该类或方法最好不要生成final,对于final类或方法,是无法继承的。

JDK:

利用拦截器(必须实现InvocationHandler)加上反射机制生成一个代理接口的匿名类,在调用具体方法前调用InvokeHandler来处理:只能对实现了接口的类生成代理,而不能针对类。

ProxyFactory:

通过ProxyFactory,我们可以不再关心到底是用cglib还是jdk动态代理了,ProxyFactory会帮我们去判断,如果UserService实现了接口,那么ProxyFactory底层就会用jdk动态代理,如果没有实现接口,就会用cglib技术。

UserService target = new UserService();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(target);
proxyFactory.addAdvice(new MethodInterceptor() {
   @Nullable
   @Override
   public Object invoke(@NotNull MethodInvocation invocation) throws Throwable {
      System.out.println("before...");
      Object result = invocation.proceed();
      System.out.println("after...");
      return result;
   }
});
UserService proxy = (UserService) proxyFactory.getProxy();
proxy.test();

上述就是一个简单的使用例子,这里有一个setTarget,代表是源目标,还有一个Advice

Advice:

  1. Before Advice:方法之前执行
  2. After returning advice:方法return后执行
  3. After throwing advice:方法抛异常后执行
  4. After (finally) advice:方法执行完finally之后执行,这是最后的,比return更后
  5. Around advice:这是功能最强大的Advice,可以自定义执行顺序

Advisor

跟Advice类似的还有一个Advisor的概念,一个Advisor是有一个Pointcut和一个Advice组成的,通过Pointcut可以指定要需要被代理的逻辑,比如一个UserService类中有两个方法,按上面的例子,这两个方法都会被代理,被增强,那么我们现在可以通过Advisor,来控制到具体代理哪一个方法, 比如:

UserService target = new UserService();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(target);
proxyFactory.addAdvisors(new PointcutAdvisor() {
   @Override
   public Pointcut getPointcut() {
      return new StaticMethodMatcherPointcut() {
         @Override
         public boolean matches(Method method, Class<?> targetClass) {
            return method.getName().equals("test");
         }
      };
   }

   @Override
   public Advice getAdvice() {
      return new MethodInterceptor() {
         @Nullable
         @Override
         public Object invoke(@NotNull MethodInvocation invocation) throws Throwable {
            System.out.println("before");
            Object proceed = invocation.proceed();
            System.out.println("after");
            return proceed;
         }
      };
   }

   @Override
   public boolean isPerInstance() {
      return false;
   }
});
UserService proxy = (UserService) proxyFactory.getProxy();
proxy.test();

上面代码表示,产生的代理对象,只有在执行test这个方法时才会被增强,会执行额外的逻辑,而在执行其他方法时是不会增强的。

ProxyFactory选择cglib或jdk动态代理原理

@Override
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
   // 如果ProxyFactory的isOptimize为true,Spring认为cglib比jdk动态代理要快
   // 或者isProxyTargetClass为true,
   // 或者被代理对象没有实现接口,
   // 或者只实现了SpringProxy这个接口
   // 那么则利用Cglib进行动态代理,但如果被代理类是接口,或者被代理类已经是进行过JDK动态代理而生成的代理类了则只能进行JDK动态代理

   // 其他情况都会进行JDK动态代理,比如被代理类实现了除SpringProxy接口之外的其他接口

   // 是不是在GraalVM虚拟机上运行
   if (!NativeDetector.inNativeImage() &&
         (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config))) {

      Class<?> targetClass = config.getTargetClass();
      if (targetClass == null) {
         throw new AopConfigException("TargetSource cannot determine target class: " +
               "Either an interface or a target is required for proxy creation.");
      }
      // targetClass是接口,直接使用Jdk动态代理
      if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
         return new JdkDynamicAopProxy(config);
      }
      // 使用Cglib
      return new ObjenesisCglibAopProxy(config);
   }
   else {
   // 使用Jdk动态代理
      return new JdkDynamicAopProxy(config);
   }
}

@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}.
    */
   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
    */
   boolean exposeProxy() default false;

}

EnableAspectJAutoProxy注解中导入了AspectJAutoProxyRegistrar

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类型的Bean,beanName为AUTO_PROXY_CREATOR_BEAN_NAME
      AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);

      // 修改AnnotationAwareAspectJAutoProxyCreator中对应的属性
      AnnotationAttributes enableAspectJAutoProxy =
            AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
      if (enableAspectJAutoProxy != null) {
         if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
            AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
         }
         if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
            AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
         }
      }
   }

}

这个主要是将AnnotationAwareAspectJAutoProxyCreator注册为Bean,注册这个之后就会在初始化后去进行动态代理了

AnnotationAwareAspectJAutoProxyCreator源码解析

先看看类的继承关系

AnnotationAwareAspectJAutoProxyCreator.png

AbstractAutoProxyCreator#wrapIfNecessary

protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
   if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
      return bean;
   }

   // advisedBeans表示已经判断过了的bean,false表示此bean不需要进行Aop
   if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
      return bean;
   }

   // 当前正在创建的Bean不用进行AOP,比如切面Bean
   if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
      this.advisedBeans.put(cacheKey, Boolean.FALSE);
      return bean;
   }

   // Create proxy if we have advice.
   // 判断当前bean是否存在匹配的advice,如果存在则要生成一个代理对象
   // 此处根据类以及类中的方法去匹配到Interceptor(也就是Advice),然后生成代理对象,代理对象在执行的时候,还会根据当前执行的方法去匹配
   Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
   if (specificInterceptors != DO_NOT_PROXY) {
      // advisedBeans记录了某个Bean已经进行过AOP了
      this.advisedBeans.put(cacheKey, Boolean.TRUE);
      Object proxy = createProxy(
            bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
      this.proxyTypes.put(cacheKey, proxy.getClass());
      return proxy;
   }

   this.advisedBeans.put(cacheKey, Boolean.FALSE);
   return bean;
}

AbstractAdvisorAutoProxyCreator#getAdvicesAndAdvisorsForBean(获取advice)

@Override
@Nullable
protected Object[] getAdvicesAndAdvisorsForBean(
      Class<?> beanClass, String beanName, @Nullable TargetSource targetSource) {

   // 寻找匹配的Advisor
   List<Advisor> advisors = findEligibleAdvisors(beanClass, beanName);
   if (advisors.isEmpty()) {
      return DO_NOT_PROXY;
   }
   return advisors.toArray();
}

AbstractAutoProxyCreator#createProxy(创建代理对象)

protected Object createProxy(Class<?> beanClass, @Nullable String beanName,
      @Nullable Object[] specificInterceptors, TargetSource targetSource) {

   if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
      AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
   }

   ProxyFactory proxyFactory = new ProxyFactory();
   proxyFactory.copyFrom(this);
   
   //判断使用jdk代理还是CGLIB代理
   if (proxyFactory.isProxyTargetClass()) {
      // Explicit handling of JDK proxy targets (for introduction advice scenarios)
      if (Proxy.isProxyClass(beanClass)) {
         // Must allow for introductions; can't just set interfaces to the proxy's interfaces only.
         for (Class<?> ifc : beanClass.getInterfaces()) {
            proxyFactory.addInterface(ifc);
         }
      }
   }
   else {
      // No proxyTargetClass flag enforced, let's apply our default checks...
      if (shouldProxyTargetClass(beanClass, beanName)) {
         proxyFactory.setProxyTargetClass(true);
      }
      else {
         evaluateProxyInterfaces(beanClass, proxyFactory);
      }
   }

   Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
   // 在这一步会去判断advisors中是否存在IntroductionAdvisor,如果存在则会把对应的interface添加到proxyFactory中去
   proxyFactory.addAdvisors(advisors);
   proxyFactory.setTargetSource(targetSource);
   customizeProxyFactory(proxyFactory);

   proxyFactory.setFrozen(this.freezeProxy);
   if (advisorsPreFiltered()) {
      proxyFactory.setPreFiltered(true);
   }

   // Use original ClassLoader if bean class not locally loaded in overriding class loader
   ClassLoader classLoader = getProxyClassLoader();
   if (classLoader instanceof SmartClassLoader && classLoader != beanClass.getClassLoader()) {
      classLoader = ((SmartClassLoader) classLoader).getOriginalClassLoader();
   }
   return proxyFactory.getProxy(classLoader);
}