哪个网站可以做砍价,赣州新闻综合频道回放,论基层门户网站的建设,wordpress 兼容移动端我们平时做 AOP 开发的时候#xff0c;基本上都是增强某一个方法#xff0c;在某一个方法执行之前或者执行之后做一些事情#xff0c;这种叫做 PointcutAdvisor#xff0c;实际上#xff0c;Spring 中的 Advisor 大致可以分为两种类型#xff0c;除了 PointcutAdvisor 之…我们平时做 AOP 开发的时候基本上都是增强某一个方法在某一个方法执行之前或者执行之后做一些事情这种叫做 PointcutAdvisor实际上Spring 中的 Advisor 大致可以分为两种类型除了 PointcutAdvisor 之外还有另外一种 Advisor 叫做 IntroductionAdvisor因为最近想和小伙伴们聊一聊 Spring AOP 的源码看源码有一个前提就是得先掌握 Spring 的各种用法这样看源码的时候往往就有一种醍醐灌顶的感觉否则看源码的时候就容易懵
1. 实践
不同于 PointcutAdvisorIntroductionAdvisor 这种增强主要是针对一个类来增强。
接下来松哥写一个简单的案例小伙伴们来看下 IntroductionAdvisor 到底做了什么工作。
假设我有一个 Animal 接口如下
public interface Animal {void eat();
}这个动物具备吃的能力。
现在我还有一个 Dog如下
public interface Dog {void run();
}
public class DogImpl implements Dog{Overridepublic void run() {System.out.println(Dog run);}
}Dog 具备跑的能力注意Dog 和 Animal 之间并无继承/实现的关系。
现在我们通过 Spring 中的 IntroductionAdvisor就能让 Dog 具备 Animal 的能力我们来看下具体怎么做。
首先我们先来开发一个 Advice这个 Advice 同时也是 Animal 接口的实现类如下
public class AnimalIntroductionInterceptor implements IntroductionInterceptor, Animal {Overridepublic Object invoke(MethodInvocation invocation) throws Throwable {if (implementsInterface(invocation.getMethod().getDeclaringClass())) {return invocation.getMethod().invoke(this, invocation.getArguments());}return invocation.proceed();}Overridepublic void eat() {System.out.println(Animal eat);}Overridepublic boolean implementsInterface(Class? intf) {return intf.isAssignableFrom(this.getClass());}
}跟普通 AOP 一样当目标方法被拦截下来的时候这里的 invoke 方法会被触发在 invoke 方法中我们需要先调用 implementsInterface 方法进行判断如果被拦截下来的方法所属的类是 Animal 的话即 implementsInterface 方法返回 true 的情况this 其实就是 Animal那么就直接获取到 method 对象然后通过反射去调用就行了这样会就会导致这里的 eat 方法被触发否则说明是被拦截下来的方法本身那么就调用 invocation.proceed(); 让拦截器链继续往下执行即可。
接下来我们来定义 Advisor
Component
public class DogIntroductionAdvisor implements IntroductionAdvisor {Overridepublic ClassFilter getClassFilter() {return new ClassFilter() {Overridepublic boolean matches(Class? clazz) {return Dog.class.isAssignableFrom(clazz);}};}Overridepublic void validateInterfaces() throws IllegalArgumentException {}Overridepublic Advice getAdvice() {return new AnimalIntroductionInterceptor();}Overridepublic boolean isPerInstance() {return true;}Overridepublic Class?[] getInterfaces() {return new Class[]{Animal.class};}
}这里有几个方法需要实现
getClassFilter哪些类需要拦截在这里配置ClassFilter 松哥在上篇文章中已经讲过了这里只需要返回被拦截的类就行了不需要具体到哪个方法被拦截。getAdvice这个就是返回拦截下来后执行的通知我们就返回前面定义的通知即可这里有一个要求就是 这个 Advice 需要实现 Animal 接口。getInterfaces这个方法还比较重要生成代理对象的时候代理对象需要实现哪些接口就是从这个地方定义的这里返回 Animal所以将来我拿到手的代理对象就实现了 Animal 接口就能调用 Animal 中的方法了。isPerInstance这个方法暂时没有实现返回 true 就行了。validateInterfaces这个方法是做接口校验的我这里就不校验了。
好啦我的代码现在就写好了我们来测试看下
ClassPathXmlApplicationContext ctx new ClassPathXmlApplicationContext(introduction.xml);
Dog dog ctx.getBean(Dog.class);
dog.run();
System.out.println(Animal.class.isAssignableFrom(dog.getClass()) Animal.class.isAssignableFrom(dog.getClass()));
Animal animal (Animal) dog;
animal.eat();执行结果如下 我们拿到手的 dog 对象其实也是一个 Animal。
这就是 Spring AOP 中的 IntroductionAdvisor当一个类需要具备另一个类的能力的时候可以使用 IntroductionAdvisor。
2. 源码分析
那么这一切是怎么实现的呢
因为这篇文章我主要是想和小伙伴们分享 IntroductionAdvisor 的知识点所以关于 AOP 完整的创建流程我先不说在后续的文章中我会和大家做一个详细介绍我今天就来和大家聊一聊在 Spring AOP 执行的过程中究竟是如何处理 IntroductionAdvisor 的。
Spring AOP 中创建代理对象一般是通过后置处理器来完成从 AbstractAutoProxyCreator#postProcessAfterInitialization 方法开始大致时序图如下 我们就从 buildProxy 方法开始看起吧这个方法看名字就知道是用来构建代理对象的。
AbstractAutoProxyCreator#buildProxy
private Object buildProxy(Class? beanClass, Nullable String beanName,Nullable Object[] specificInterceptors, TargetSource targetSource, boolean classOnly) {//...Advisor[] advisors buildAdvisors(beanName, specificInterceptors);proxyFactory.addAdvisors(advisors);//...return (classOnly ? proxyFactory.getProxyClass(classLoader) : proxyFactory.getProxy(classLoader));
}这里有一个 buildAdvisors 方法这个方法是用来处理 Advisor 的我们自定义的 DogIntroductionAdvisor 将在这里被读取进来然后将之添加到 proxyFactory 对象中在添加的过程中会进行一些额外的处理proxyFactory#addAdvisors 最终会来到 AdvisedSupport#addAdvisors 方法中
public void addAdvisors(CollectionAdvisor advisors) {if (!CollectionUtils.isEmpty(advisors)) {for (Advisor advisor : advisors) {if (advisor instanceof IntroductionAdvisor introductionAdvisor) {validateIntroductionAdvisor(introductionAdvisor);}this.advisors.add(advisor);}adviceChanged();}
}在这里会遍历所有的 Advisor判断类型是不是 IntroductionAdvisor 类型的我们自定义的 DogIntroductionAdvisor 恰好就是 IntroductionAdvisor 类型的所以会进一步调用 validateIntroductionAdvisor 方法如下
private void validateIntroductionAdvisor(IntroductionAdvisor advisor) {advisor.validateInterfaces();Class?[] ifcs advisor.getInterfaces();for (Class? ifc : ifcs) {addInterface(ifc);}
}
public void addInterface(Class? intf) {if (!this.interfaces.contains(intf)) {this.interfaces.add(intf);adviceChanged();}
}小伙伴们看一下advisor.getInterfaces(); 实际上就调用到我们自定义的 DogIntroductionAdvisor 中的 getInterfaces 方法了所以这里会返回 Animal 接口然后这里会把 Animal 接口存入到 interfaces 这个变量中将来在生成 AOP 对象的时候会用到。
好啦现在回到 buildProxy 方法中该方法最终会执行到 proxyFactory.getProxy 方法该方法最终执行的时候要么是 JDK 动态代理要么是 CGLIB 动态代理我们分别来说一下。
2.1 JDK 动态代理
先说如果是 JDK 动态代理那么 proxyFactory.getProxy 方法就需要构建一个 JdkDynamicAopProxy 出来如下
public JdkDynamicAopProxy(AdvisedSupport config) throws AopConfigException {this.advised config;this.proxiedInterfaces AopProxyUtils.completeProxiedInterfaces(this.advised, true);findDefinedEqualsAndHashCodeMethods(this.proxiedInterfaces);
}参数 config 中就包含了我们前面说的要实现的接口所以这里 proxiedInterfaces 变量中保存的就是代理对象将来要实现的接口以我们前面的代码为例这里 proxiedInterfaces 的值如下 可以看到就包含了 Animal 接口。
最后调用 JdkDynamicAopProxy#getProxy 方法生成代理对象如下
Override
public Object getProxy(Nullable ClassLoader classLoader) {return Proxy.newProxyInstance(classLoader, this.proxiedInterfaces, this);
}这就是大家比较熟悉的 JDK 动态代理了可以看到生成的代理对象有五个接口生成的代理对象不仅仅是 Dog、Animal 的实例也是 SpringProxy 等的实例。现在大家就明白了为什么我们拿到手的 dog 对象还能强转成 Animal 了。
2.2 CGLIB 动态代理
再来看 CGLIB 动态代理的实现逻辑其实也差不多
public CglibAopProxy(AdvisedSupport config) throws AopConfigException {this.advised config;this.advisedDispatcher new AdvisedDispatcher(this.advised);
}
Override
public Object getProxy(Nullable ClassLoader classLoader) {return buildProxy(classLoader, false);
}
private Object buildProxy(Nullable ClassLoader classLoader, boolean classOnly) {//...enhancer.setSuperclass(proxySuperClass);enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));//...// Generate the proxy class and create a proxy instance.return (classOnly ? createProxyClass(enhancer) : createProxyClassAndInstance(enhancer, callbacks));
}可以看到其实跟 JDK 里边的思路差不多也是从 advised 中提取出来接口设置进去advised 也是在 CglibAopProxy 对象构建的时候传入进来的。
3. 小结
好了现在小伙伴们应该明白了什么是 IntroductionAdvisor 了吧说白了就是在生成代理对象的时候把我们在 Advisor 中设置好的接口也考虑进去这样生成的代理对象同时也是该接口的实现类当然在我们提供的 Advice 中必须也要实现该接口否则代理对象执行接口中的方法找不到具体实现的时候就会报错了。
感兴趣的小伙伴赶紧体验一把吧