Spring AOP 实现原理
什么是AOP
AOP使用场景
实现AOP的技术
如何使用Spring AOP
Spring AOP代理对象的生成
InvocationHandler是JDK动态代理的核心
public Object invoke(Object proxy,Method method,Object[] args) throwsThrowable {MethodInvocation invocation =null;Object oldProxy =null;boolean setProxyContext =false;TargetSource targetSource =this.advised.targetSource;Class targetClass =null;Object target =null;try{//eqauls()方法,具目标对象未实现此方法if(!this.equalsDefined &&AopUtils.isEqualsMethod(method)){return(equals(args[0])?Boolean.TRUE :Boolean.FALSE);}//hashCode()方法,具目标对象未实现此方法if(!this.hashCodeDefined &&AopUtils.isHashCodeMethod(method)){return newInteger(hashCode());}//Advised接口或者其父接口中定义的方法,直接反射调用,不应用通知if(!this.advised.opaque &&method.getDeclaringClass().isInterface()&&method.getDeclaringClass().isAssignableFrom(Advised.class)){// Service invocations onProxyConfig with the proxy config...returnAopUtils.invokeJoinpointUsingReflection(this.advised,method, args);}Object retVal =null;if(this.advised.exposeProxy){// Make invocation available ifnecessary.oldProxy =AopContext.setCurrentProxy(proxy);setProxyContext =true;}//获得目标对象的类target = targetSource.getTarget();if(target !=null){targetClass = target.getClass();}//获取可以应用到此方法上的Interceptor列表List chain =this.advised.getInterceptorsAndDynamicInterceptionAdvice(method,targetClass);//如果没有可以应用到此方法的通知(Interceptor),此直接反射调用 method.invoke(target, args)if(chain.isEmpty()){retVal =AopUtils.invokeJoinpointUsingReflection(target,method, args);}else{//创建MethodInvocationinvocation = newReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);retVal = invocation.proceed();}// Massage return value if necessary.if(retVal !=null&& retVal == target &&method.getReturnType().isInstance(proxy)&&!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())){// Special case: it returned"this" and the return type of the method// is type-compatible. Notethat we can't help if the target sets// a reference to itself inanother returned object.retVal = proxy;}return retVal;}finally{if(target !=null&&!targetSource.isStatic()){// Must have come fromTargetSource.targetSource.releaseTarget(target);}if(setProxyContext){// Restore old proxy.AopContext.setCurrentProxy(oldProxy);}}}
参考文章
Spring AOP 实现原理的更多相关文章
- 【Spring】Spring AOP实现原理
Spring AOP实现原理 在之前的一文中介绍过Spring AOP的功能使用,但是没有深究AOP的实现原理,今天正好看到几篇好文,于是就自己整理了一下AOP实现的几种方式,同时把代理模式相关知识也 ...
- 何为代理?jdk动态代理与cglib代理、spring Aop代理原理浅析
原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...
- Spring Aop底层原理详解
Spring Aop底层原理详解(来源于csdn:https://blog.csdn.net/baomw)
- spring AOP底层原理实现——jdk动态代理
spring AOP底层原理实现——jdk动态代理
- Spring AOP底层原理
------------------siwuxie095 Spring AOP 底层原理 AOP 即 Aspect Or ...
- jdk动态代理与cglib代理、spring Aop代理原理-代理使用浅析
原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ...
- Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现
前文,我们分析了Spring IOC的初始化过程和Bean的生命周期等,而Spring AOP也是基于IOC的Bean加载来实现的.本文主要介绍Spring AOP原理解析的切面实现过程(将切面类的所 ...
- Spring框架系列(10) - Spring AOP实现原理详解之AOP代理的创建
上文我们介绍了Spring AOP原理解析的切面实现过程(将切面类的所有切面方法根据使用的注解生成对应Advice,并将Advice连同切入点匹配器和切面类等信息一并封装到Advisor).本文在此基 ...
- Spring框架系列(11) - Spring AOP实现原理详解之Cglib代理实现
我们在前文中已经介绍了SpringAOP的切面实现和创建动态代理的过程,那么动态代理是如何工作的呢?本文主要介绍Cglib动态代理的案例和SpringAOP实现的原理.@pdai Spring框架系列 ...
随机推荐
- phonegap 退出确认
实现 再按一次退出 ,这里只针对 主active继承 DroidGap 或者CordovaActive 以下有2种 方案1: 重写CordovaWebView类 新建类NobackWebView p ...
- 内容在某div中滚动
1.设定外层div在屏幕中的高度. 2.设置div刷新style="-webkit-overflow-scrolling: touch; overflow: scroll“
- Aspose.Cells 读取Excel数据到DataTable
C#代码: Workbook workbook = new Workbook(); workbook.Open(excelfile); Cells cells = workbook.Worksheet ...
- Android Service 服务
一. Service简介 Service是android 系统中的四大组件之一(Activity.Service.BroadcastReceiver.ContentProvider),它跟Activi ...
- DBus学习笔记
摘要:DBus作为一个轻量级的IPC被越来越多的平台接受,在MeeGo中DBus也是主要的进程间通信方式,这个笔记将从基本概念开始记录笔者学习DBus的过程 [1] DBus学习笔记一:DBus学习的 ...
- cp: omitting directory”错误的解释和解决办法
在linux下拷贝的时候有时候会出现cp:omitting directory的错误 ,例如 cp:omitting directory "bbs" 说明bbs目录下面还有目录,不 ...
- $("#province").val();取不到select的值求解
MVC下的razor视图开发中无法取到select的值问题求解 cshtml 如下 <select name="province" id="province&quo ...
- uoj #58. 【WC2013】糖果公园(树上莫队算法+修改操作)
[题目链接] http://uoj.ac/problem/58 [题意] 有一棵树,结点有自己的颜色,若干询问:u,v路径上的获益,并提供修改颜色的操作. 其中获益定义为Vc*W1+Vc*W2+…+V ...
- 实例化spring容器
方法一:在类路径下寻找配置来实例化容器 ApplicationContext ctx = new String[]{"beans.xml"}); 方法二:在文件系统路径下寻找配置文 ...
- Spark connect to Database
Cannect to Cassandra: 用spark-cassandra-connector, 注意spark,cassandra和connector的版本要配套,Cassandra至少要版本2以 ...