上篇中的自定义Advisor是实现的AbstractPointcutAdvisor,Advisor其实还有一个接口级别的IntroductionAdvisor

                   

这个好像用的很少,网上搜了一些资料,说是基于Class的增强,接口的功能扩展,在原有的功能上增加一些其他接口定义的方法,可以将当前代理对象向上转型,然后调用接口中的方法

这个功能暂时还不知道适用于什么场景

示例:

//定义一个some类,只有doSome方法
@Component
public class Some {
public void doSome() {
System.out.println("do some...");
};
}
//定义一个other接口,提供doOther功能
public interface IOther {
public void doOther();
}
//定义一个通知器
@Component
public class OtherIntroductionAdvisor extends DefaultIntroductionAdvisor { public OtherIntroductionAdvisor() {
super(new OtherIntroductionInterceptor());
} // createBean的时候判断bean是否需要被代理
@Override
public boolean matches(Class<?> clazz) {
//只代理some对象
boolean assignableFrom = clazz.isAssignableFrom(Some.class);
return assignableFrom;
}
}
//定义一个通知,要实现IOther接口
public class OtherIntroductionInterceptor implements IntroductionInterceptor, IntroductionInfo, IOther { //实现doOther方法
@Override
public void doOther() {
System.out.println("do other ...");
} //扩展功能的实现
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
//当前方法的声明类是否实现了需要拓展功能的接口
if (implementsInterface(invocation.getMethod().getDeclaringClass())) {
//调用this的此方法
return invocation.getMethod().invoke(this, invocation.getArguments());
}
return invocation.proceed();
} @Override
public boolean implementsInterface(Class<?> ifc) {
Class<?>[] interfaces = this.getInterfaces();
for (Class clazz : interfaces) {
if (ifc.isInterface() && ifc.isAssignableFrom(clazz)) {
return true;
}
}
return false;
} @Override
public Class<?>[] getInterfaces() {
return new Class[] {IOther.class};
}
}

在创建代理对象时,将拓展功能的接口set进去,所以代理对象上转型才不会报错

org.springframework.aop.framework.CglibAopProxy#getProxy(java.lang.ClassLoader)

@Override
public Object getProxy(@Nullable ClassLoader classLoader) {
if (logger.isDebugEnabled()) {
logger.debug("Creating CGLIB proxy: target source is " + this.advised.getTargetSource());
} try {
Class<?> rootClass = this.advised.getTargetClass();
Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy"); Class<?> proxySuperClass = rootClass;
if (ClassUtils.isCglibProxyClass(rootClass)) {
proxySuperClass = rootClass.getSuperclass();
Class<?>[] additionalInterfaces = rootClass.getInterfaces();
for (Class<?> additionalInterface : additionalInterfaces) {
this.advised.addInterface(additionalInterface);
}
} // Validate the class, writing log messages as necessary.
validateClassIfNecessary(proxySuperClass, classLoader); // Configure CGLIB Enhancer...
Enhancer enhancer = createEnhancer();
if (classLoader != null) {
enhancer.setClassLoader(classLoader);
if (classLoader instanceof SmartClassLoader &&
((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {
enhancer.setUseCache(false);
}
}
enhancer.setSuperclass(proxySuperClass);
// 获取接口信息并且set到代理对象的接口中
enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setStrategy(new ClassLoaderAwareUndeclaredThrowableStrategy(classLoader)); Callback[] callbacks = getCallbacks(rootClass);
Class<?>[] types = new Class<?>[callbacks.length];
for (int x = 0; x < types.length; x++) {
types[x] = callbacks[x].getClass();
}
// fixedInterceptorMap only populated at this point, after getCallbacks call above
enhancer.setCallbackFilter(new ProxyCallbackFilter(
this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
enhancer.setCallbackTypes(types); // Generate the proxy class and create a proxy instance.
return createProxyClassAndInstance(enhancer, callbacks);
}
catch (CodeGenerationException | IllegalArgumentException ex) {
throw new AopConfigException("Could not generate CGLIB subclass of " + this.advised.getTargetClass() +
": Common causes of this problem include using a final class or a non-visible class",
ex);
}
catch (Throwable ex) {
// TargetSource.getTarget() failed
throw new AopConfigException("Unexpected AOP exception", ex);
}
}

3.AOP中的IntroductionAdvisor的更多相关文章

  1. Spring AOP中定义切点(PointCut)和通知(Advice)

    如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...

  2. 正确理解Spring AOP中的Around advice

    Spring AOP中,有Before advice和After advice,这两个advice从字面上就可以很容易理解,但是Around advice就有点麻烦了. 乍一看好像是Before ad ...

  3. Spring AOP中的动态代理

    0  前言 1  动态代理 1.1 JDK动态代理 1.2 CGLIB动态代理 1.2.1 CGLIB的代理用法 1.2.2 CGLIB的过滤功能 2  Spring AOP中的动态代理机制 2.1  ...

  4. Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)

    本文例子完整源码地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/Spring%20AOP%E9%A ...

  5. 笔记13 AOP中After和AfterReturning的区别

    AOP中 @Before @After @AfterThrowing@AfterReturning的执行顺序 public Object invoke(Object proxy, Method met ...

  6. spring aop中pointcut表达式完整版

    spring aop中pointcut表达式完整版 本文主要介绍spring aop中9种切入点表达式的写法 execute within this target args @target @with ...

  7. Spring AOP中的JDK和CGLib动态代理哪个效率更高?

    一.背景 今天有小伙伴面试的时候被问到:Spring AOP中JDK 和 CGLib动态代理哪个效率更高? 二.基本概念 首先,我们知道Spring AOP的底层实现有两种方式:一种是JDK动态代理, ...

  8. 转:Spring AOP中的动态代理

    原文链接:Spring AOP中的动态代理 0  前言 1  动态代理 1.1 JDK动态代理 1.2 CGLIB动态代理 1.2.1 CGLIB的代理用法 1.2.2 CGLIB的过滤功能 2  S ...

  9. Spring AOP 中@Pointcut的用法

    Spring Aop中@pointCut的用法,格式:execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? nam ...

随机推荐

  1. easyUi 的form和validate组件

    以下代码不能运行,只是我在学习过程中记录的笔记,但代码可以用!!! 可以按照需要截取. <%@ page language="java" contentType=" ...

  2. Caffe之layer_factory

    之前在测试NN中各个层的时间的时候,遇到一个非常奇怪的问题,分别使用Caffe自己的gpu方法和cuDNN方法,在卷积上性能差异非常大,但是在pooling层上基本没有变化.抽空检查了代码之后,发现是 ...

  3. python笔记:学习设置Python虚拟环境+配置 virtualenvwarpper+创建Python3.6的虚拟环境+安装numpy

    虚拟环境它是一个虚拟化,从电脑独立开辟出来的环境.就是借助虚拟机docker来把一部分内容独立出来,我们把这部分独立出来的东西称作“容器”,在这个容器中,我们可以只安装我们需要的依赖包,各个容器之间互 ...

  4. IDEA中使用git合并分支的过程报错:cant checkout because of unmerged files

    使用idea的git插件控制代码分支合并时,由于操作不当,报错了,控制台报错如下: cant checkout because of unmerged files,you have to resolv ...

  5. (转)再过半小时,你就能明白kafka的工作原理了

    为什么需要消息队列 周末无聊刷着手机,某宝网APP突然蹦出来一条消息“为了回馈老客户,女朋友买一送一,活动仅限今天!”.买一送一还有这种好事,那我可不能错过!忍不住立马点了去.于是选了两个最新款,下单 ...

  6. 如何使用NugetPackageExplorer 创建Nuget发布包,简易版

    在上一篇博客中,详细介绍了个人Nuget服务器的搭建.这篇博客中,将详细介绍一下如何使用NugetPackageExplorer工具制作可以发布到Nuget服务器上包. 直奔主题 在开始之前,需要下载 ...

  7. 理解Event冒泡模型

    本文探索一下Event的冒泡过程和初学遇到的几个小bug DOM Event概述 Event接口是检测在DOM中的发生的所有事件,我们一直在用,而且从DOM的很早的版本就一直在用着.早期的网景(后来的 ...

  8. C3的坑之inline-block

    最近开始复习css一直在踩坑,今天分享一个inline-block 关于inline-block可能很多人都不熟悉,布局这方面很多人用的都是flex或者浮动,flex很强大毋庸置疑的可是关于兼容性就不 ...

  9. FreeRTOS任务优先级说明

    https://www.cnblogs.com/yangguang-it/p/7156445.html#4217682 下面对 FreeRTOS 优先级相关的几个重要知识点进行下说明,这些知识点在以后 ...

  10. 防止sql注入的参数化查询

    参数化查询为什么能够防止SQL注入 http://netsecurity.51cto.com/art/201301/377209.htm OleDbDataAdapter Class http://m ...