springAop原理之Advised接口族

Advised:已经被建议的对。指的就是advice和Pointcut被Advisor关联了起来。这些Advisor会和aop代理的target绑定,当aop代理执行某一个方法是,会从所有的Advisor中匹配出和该方法匹配的Advoisor,这些Advisor会被包装厂MethodInterceptor返回。JdkDynamicAopProxy或基于CGLIB的ObjenesisCglibAopProxy会依次执行这些拦截器。下面就是JdkDynamicAopProxy中invoke方法获取这些方法拦截器并委托给ReflectiveMethodInvocation执行的源码。ReflectiveMethodInvocation采用责任链模式,依次执行每一个MethodInterceptor。

List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

// Check whether we have any advice. If we don't, we can fallback on direct
// reflective invocation of the target, and avoid creating a MethodInvocation.
if (chain.isEmpty()) {
   // We can skip creating a MethodInvocation: just invoke the target directly
   // Note that the final invoker must be an InvokerInterceptor so we know it does
   // nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
   Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
   retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
}
else {
   // We need to create a method invocation...
   invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
   // Proceed to the joinpoint through the interceptor chain.
   retVal = invocation.proceed();
}

我们知道spring生成的代理对象是基于JDK的JdkDynamicAopProxy或基于CGLIB的ObjenesisCglibAopProxy,spring内部是委托给DefaultAopProxyFactory生成代理对象,源码如下:

public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
   if (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.");
      }
      if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
         return new JdkDynamicAopProxy(config);
      }
      return new ObjenesisCglibAopProxy(config);
   }
   else {
      return new JdkDynamicAopProxy(config);
   }
}

该方法需要一个AdvisedSupport类的对象。

AdvisedSupport:AOP代理配置管理器的基类。 这些本身不是AOP代理,但此类的子类通常是直接从中获得AOP代理实例的工厂。 此类可释放Advices和Advisor的内部管理子类,但实际上并没有实现代理的创建方法,而是由子类提供。此类用于保存代理的快照。

AdvisedSupport实现了Advised中处理Advisor和Advice的方法,添加Advice时会被包装成一个Advisor,默认使用的Advisor是DefaultPointcutAdvisor,DefaultPointcutAdvisor默认的Pointcut是TruePointcut(转换为一个匹配所有方法调用的Advisor与代理对象绑定)。

AdvisedSupport同时会缓存对于某一个方法对应的所有Advisor(Map<MethodCacheKey, List<Object>> methodCache),当Advice或Advisor发生变化时,会清空该缓存。

ProxyCreatorSupport继承了AdvisedSupport,ProxyCreatorSupport正是实现代理的创建方法,ProxyCreatorSupport有一个成员变量AopProxyFactory,而该变量的值默认就是DefaultAopProxyFactory
public ProxyCreatorSupport() {
   this.aopProxyFactory = new DefaultAopProxyFactory();
}
ProxyCreatorSupport:List<AdvisedSupportListener> listeners spring并没有实现该接口的类,主要就是在第一次创建代理时或代理发生变化时触发。
ProxyCreatorSupport:boolean active创建第一个AOP代理后设置为true【createAopProxy()】
ProxyFactory继承ProxyCreatorSupport,spring默认创建Proxy对象的一个工厂类。该类有多个重载的构造方法和多个重载的获取代理的方法。

ProxyFactoryBean:以声明/编程的方式使用来实现AOP;

猜你喜欢

转载自blog.csdn.net/sinat_33472737/article/details/105248639