Spring中的各种Utils(六):AopProxyUtils详解(转载)

原文出处:https://blog.csdn.net/wolfcode_cn/article/details/80660478

原创文章,转载请注明出处

本节介绍AopProxyUtils,这个工具类方法本身很少,但是涉及到很多AOP相关重要接口,所以本节主要会对涉及到的接口进行初步的介绍;

Class<?> ultimateTargetClass(Object candidate)
获取一个代理对象的最终对象类型,先做一个测试(测试的准备工作参考https://www.jianshu.com/p/a4a815f42cd2):

  1.  @Test
  2.  public void testUltimateTargetClass() {
  3.  // class cn.wolfcode.springboot.utilstest.MyComponent
  4.  System.out.println(AopProxyUtils.ultimateTargetClass(component));
  5.  // class cn.wolfcode.springboot.utilstest.EmployeeServiceImpl
  6.  System.out.println(AopProxyUtils.ultimateTargetClass(service));
  7.  }

分别获取了代理对象的最终对象类型;我们来看看该方法的实现:

  1.  public static Class<?> ultimateTargetClass(Object candidate) {
  2.  Assert.notNull(candidate, "Candidate object must not be null");
  3.  //current用于判断;
  4.  Object current = candidate;
  5.  Class<?> result = null;
  6.  //直到当前获得的对象不是TargetClassAware类型,TargetClassAware后面介绍;
  7.  while (current instanceof TargetClassAware) {
  8.  //获得当前对象(一个代理对象)代理的目标对象(这个对象可能还是一个代理对象)类型;
  9.  result = ((TargetClassAware) current).getTargetClass();
  10.  Object nested = null;
  11.  //如果当前获取的目标对象是一个Advised类型对象;
  12.  if (current instanceof Advised) {
  13.  //通过getTargetSource方法获取代理目标;
  14.  TargetSource targetSource = ((Advised) current).getTargetSource();
  15.  //如果代理目标对象是一个SingletonTargetSource;
  16.  if (targetSource instanceof SingletonTargetSource) {
  17.  //获取当前代理对象的真正目标对象(可能还是一个代理对象)
  18.  nested = ((SingletonTargetSource) targetSource).getTarget();
  19.  }
  20.  }
  21.  current = nested;
  22.  }
  23.  //如果获取到的目标对象是一个cglib代理对象,获取父类类型(才是目标类型)
  24.  if (result == null) {
  25.  result = (AopUtils.isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
  26.  }
  27.  return result;
  28.  }

其实这段代码本身不难,但是这里面涉及到了很多SpringAOP中的接口,比如TargetClassAware,Advised,TargetSource,SingletonTargetSource等;这些接口都是SpringAOP中的一些重要接口,在这里我们做一个简单介绍,更多的内容后面单开文章介绍;

我们先看下TargetClassAware,Advised和TargetSource三个接口:
TargetClassAware:所有的Aop代理对象或者代理工厂(proxy factory)都要实现的接口,该接口用于暴露出被代理目标对象类型;

TargetSource:该接口代表一个目标对象,在aop调用目标对象的时候,使用该接口返回真实的对象。比如该接口的一个实现:PrototypeTargetSource,那就是每次调用都返回一个全新的对象实例;

Advised:该接口用于保存一个代理的相关配置(简单理解为这个对象保存了怎么创建一个代理对象的信息),比如这个代理配置相关的拦截器,建议(advisor)或者增强器(advice);所有的代理对象都实现了该接口(我们就能够通过一个代理对象获取这个代理对象怎么被代理出来的相关信息);

我们可以来做个测试:

  1.  @Test
  2.  public void testUltimateTargetClass2() {
  3.  /**
  4.  * interface cn.wolfcode.springboot.utilstest.IEmployeeService interface
  5.  * cn.wolfcode.springboot.utilstest.IAddition interface
  6.  * org.springframework.aop.SpringProxy interface
  7.  * org.springframework.aop.framework.Advised interface 
  8. * java.io.Serializable
  9.  */
  10.  System.out.println(StringUtils.collectionToDelimitedString(
  11.  ClassUtils.getAllInterfacesAsSet(service), "\r\n"));
  12.  /**
  13.  * interface org.springframework.aop.SpringProxy interface
  14.  * org.springframework.aop.framework.Advised interface
  15.  * org.springframework.cglib.proxy.Factory
  16.  */
  17.  System.out.println(StringUtils.collectionToDelimitedString(
  18.  ClassUtils.getAllInterfacesAsSet(component), "\r\n"));
  19.  }

可以看到,不管是JDKproxy,还是cglib proxy,代理出来的对象都实现了org.springframework.aop.framework.Advised接口;

三个接口的关系为:

image.png

接口中的方法我们先不研究,我们明确一个点来辅助我们理解这个关系,那就是Advised接口中的getTargetSource返回的就是TargetSource。意思就是Advised和TargetSource接口虽然在继承关系上,都是继承了TargetClassAware接口,看似平级关系,实际上确实组合关系:

  1.  @Test
  2.  public void testUltimateTargetClass3() {
  3.  if (service instanceof Advised) {
  4.  TargetSource ts = ((Advised) service).getTargetSource();
  5.  //class org.springframework.aop.target.SingletonTargetSource
  6.  System.out.println(ts.getClass());
  7.  }
  8.  }

可以看到,得到的最终是一个SingletonTargetSource,那么就是说这种TargetSource就是每次调用,都返回相同对象;

而Advised里面包含的Advisor和Advice,我们后面再详细介绍;
希望通过这个介绍,对这三个接口有更深入的理解;


Class<?>[] completeProxiedInterfaces(AdvisedSupport advised)
很牛逼的方法来了,判断一个advised真正需要代理的目标接口列表。简单理解,比如在spring使用JDK proxy做代理的时候,这个方法返回的类型列表就是真正需要交给Proxy.newProxyInstance方法的接口列表,我们先来简单看看这个流程在Spring中的实现:

  1.  public Object getProxy(ClassLoader classLoader) {
  2.  Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised);
  3.  findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
  4.  return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
  5.  }

这段代码摘取自JdkDynamicAopProxy,这个就是我们常常说的Spring中针对实现了接口的对象,使用JDK的动态代理完成的真正代码,可以看到,传给newProxyInstance方法第二个参数的接口列表,就是通过completeProxiedInterfaces方法获取的。足见该方法的重要性;

该方法具体的实现我们这里先不介绍,因为涉及到另一个AOP中重要的类:AdvisedSupport,该类是Spring中用于搜集代理配置的对象(是不是感觉这句话很熟,是的,这个类就是实现了上面介绍的Advised接口,但是注意,代理出来的类可以转成Advised,但是不是AdvisedSupport);


Class<?>[] proxiedUserInterfaces(Object proxy) 
该方法用于获取一个代理对象中的用户定义的接口,即非(Advised接口体系)之外的其他接口;

  1.  @Test
  2.  public void testProxiedUserInterfaces() {
  3.  /**
  4.  * interface cn.wolfcode.springboot.utilstest.IEmployeeService interface
  5.  * cn.wolfcode.springboot.utilstest.IAddition interface
  6.  * org.springframework.aop.SpringProxy interface 
  7. * org.springframework.aop.framework.Advised interface
  8.  * java.io.Serializable
  9.  */
  10.  System.out.println(StringUtils.collectionToDelimitedString(
  11.  ClassUtils.getAllInterfacesAsSet(service), "\r\n"));
  12.   
  13.  /**
  14.  * interface cn.wolfcode.springboot.utilstest.IEmployeeService interface
  15.  * cn.wolfcode.springboot.utilstest.IAddition
  16.  */
  17.  System.out.println(StringUtils.arrayToDelimitedString(
  18.  AopProxyUtils.proxiedUserInterfaces(service), "\r\n")); 
  19.   
  20. }

可以看到,只得到了用户定义的接口,Spring自动增加的系统接口没有包含;
这个方法的实现也很有意思:

  1.  public static Class<?>[] proxiedUserInterfaces(Object proxy) {
  2.  //得到所有接口
  3.  Class<?>[] proxyInterfaces = proxy.getClass().getInterfaces();
  4.  int nonUserIfcCount = 0;
  5.  //如果是代理,一定实现了SpringProxy;
  6.  if (proxy instanceof SpringProxy) {
  7.  nonUserIfcCount++;
  8.  }
  9.  //如果是代理,可能实现了Advised;
  10.  if (proxy instanceof Advised) {
  11.  nonUserIfcCount++;
  12.  }
  13.  Class<?>[] userInterfaces = new Class<?>[proxyInterfaces.length - nonUserIfcCount];
  14.  //拷贝proxyInterfaces中从第0位~第proxyInterfaces.length - nonUserIfcCount个
  15.  //去掉尾巴上的nonUserIfcCount个;
  16.  System.arraycopy(proxyInterfaces, 0, userInterfaces, 0, userInterfaces.length);
  17.  Assert.notEmpty(userInterfaces, "JDK proxy must implement one or more interfaces");
  18.  return userInterfaces;
  19.  }

boolean equalsProxiedInterfaces(AdvisedSupport a, AdvisedSupport b)
判断两个(即将)代理出来的对象是否拥有相同接口;

boolean equalsAdvisors(AdvisedSupport a, AdvisedSupport b)
判断两个(即将)代理出来的对象是否拥有相同的建议者(Advisor);

boolean equalsInProxy(AdvisedSupport a, AdvisedSupport b)
判断两个(即将)代理出来的对象是否相同;

小结

AopProxyUtils中的方法不多,但是其中的ultimateTargetClass和completeProxiedInterfaces方法确是Spring AOP中比较重要的方法,也给了我们一个入手观察Spring AOP真正实现过程的一个突破口;我认为这个,才是AopProxyUtils给我们的价值;

posted @ 2018-07-14 17:54  青青子衿J  阅读(2380)  评论(0)    收藏  举报