Spring:Aop before after afterReturn afterThrowing around 的原理
在写这篇文章前,在网上看了一下大多数的文章,在说这一块时,都是用语言来表达的。before、after、afterReturn、afterThrowing 这四个用语言是可以说清楚的,但 around用语言来讲可就晦涩难懂了。
这里,我用代码来展示before、after、afterReturn、afterThrowing 、around是如何执行的。
不使用Around时,实现可以理解成这样的:
public class XxxProxy implement InvocationHandler {
private Target target;
private Interceptor interceptor;
public Object invoke(Object proxy, Method method, Object[] args) throws Exception{
interceptor.before();
boolean hasReturnValue = method.getReturnType!=void.class;
try{
Object result = null;
if(hasReturnValue){
result = method.invoke(target, args);
}else{
method.invoke(target, args);
}
interceptor.afterReturn();
return result;
}catch(Exception ex){
interceptor.afterThrowing();
throw ex;
}finally{
interceptor.after();
}
}
}
使用Around时,实现可以理解成这样的:
public class XxxProxy implement InvocationHandler {
private Target target;
private Interceptor interceptor;
public Object invoke(Object proxy, Method method, Object[] args) throws Exception{
interceptor.before();
boolean hasReturnValue = method.getReturnType!=void.class;
try{
ProxyMethodInvocation methodInvocation = new ReflectiveMethodInvocation(proxy, method, target, args);
ProceedingJoinPoint proceed = new MethodInvocationProceedingJoinPoint(target, parameters);
Object result = null;
// 通过Around 来执行 真实的方法调用
if(hasReturnValue){
result = interceptor.around(proceed); // 在编写around方法中必须得调用 proceed.procced();
}else{
interceptor.around(proceed);
}
interceptor.afterReturn();
return result;
}catch(Exception ex){
interceptor.afterThrowing();
throw ex;
}finally{
interceptor.after();
}
}
}
public class ReflectiveMethodInvocation implements ProxyMethodInvocation {
private Object proxy;
private Object target;
private Method method;
private Object[] args;
public ReflectiveMethodInvocation(Object proxy, Method method, Object target, Object[] args){
this.proxy = proxy;
this.method = method;
this.target = target;
this.args = args;
}
public Method getMethod(){
return this.method;
}
public Object[] getArgs(){
return args;
}
public Object[] getTarget(){
return target;
}
}
public class MethodInvocationProceedingJoinPoint implements ProceedingJoinPoint{
private ProxyMethodInvocation methodInvocation;
public MethodInvocationProceedingJoinPoint(ProxyMethodInvocation methodInvocation){
this.methodInvocation = methodInvocation;
}
public Object procced(){
Object target = methodInvocation.getTarget();
Object[] args = methodInvocation.getArgs();
Method method = methodInvocation.getMethod();
return method.invoke(target, args);
}
}
Spring:Aop before after afterReturn afterThrowing around 的原理的更多相关文章
- 基于注解的Spring AOP的配置和使用
摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...
- 基于注解的Spring AOP的配置和使用--转载
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...
- Spring AOP 注解和xml实现 --转载
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...
- Spring AOP 五大通知类型
1.前置通知 在目标方法执行之前执行执行的通知. 前置通知方法,可以没有参数,也可以额外接收一个JoinPoint,Spring会自动将该对象传入,代表当前的连接点,通过该对象可以获取目标对象 和 目 ...
- Spring AOP详细介绍
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,是个比较经典的例子. 一 AOP的基本概念 (1)Asp ...
- spring AOP自定义注解方式实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- 关于 Spring AOP (AspectJ) 该知晓的一切
关联文章: 关于Spring IOC (DI-依赖注入)你需要知道的一切 关于 Spring AOP (AspectJ) 你该知晓的一切 本篇是年后第一篇博文,由于博主用了不少时间在构思这篇博文,加上 ...
- Spring AOP 切面编程记录日志和接口执行时间
最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...
- spring AOP自定义注解 实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
随机推荐
- Thinking in Unity3D:材质系统概览
关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的引擎 ...
- Module Zero学习目录
Module-Zero是实现了ASP.NET Boilerplate框架抽象概念的模块,对于企业web应用也添加了一些有用的东西: 实现了ASP.NET Identity框架的用户和角色管理. 提供了 ...
- Entity Framework 6 Recipes 2nd Edition(13-4)译 -> 有效地创建一个搜索查询
问题 你想用LINQ写一个搜索查询,能被转换成更有效率的SQL.另外,你想用EF的CodeFirst方式实现. 解决方案 假设你有如下Figure 13-6所示的模型 Figure 13-6. A s ...
- Atitit 2016年attilax事业成就表
Atitit 2016年attilax事业成就表 1.1. 项目管理模型---Rem模型2 1.2. 项目管理模型---vsi模型val specs implt3 1.3. 研发体系完善(gui与游戏 ...
- 升级xcode8 之后遇到的一些问题
昨天趁着快下班,就将xcode升级为8了,运行起来并没有什么问题,今天一早过来运行,结果,模拟器打不开了.... 1. unable to boot the Simulator 解决办法:重启Mac时 ...
- Linux 服务器 安装 memcached
linux centos 一.memcached的安装 1.下载 memcached-1.4.33.tar.gz.libevent-2.0.22-stable.tar.gz 安装 memcached ...
- .NET中的逆变协变
MSDN上的说法: 协变和逆变都是术语,前者指能够使用比原始指定的派生类型的派生程度更小(不太具体的)的类型,后者指能够使用比原始指定的派生类型的派生程度更大(更具体的)的类型----------(注 ...
- 劈荆斩棘:Gitlab 部署 CI 持续集成
阅读目录: install configue gitlab-ci-multi-runner restore nuget packages bulid .sln run unit tests confi ...
- spring boot(七):springboot+mybatis多数据源最简解决方案
说起多数据源,一般都来解决那些问题呢,主从模式或者业务比较复杂需要连接不同的分库来支持业务.我们项目是后者的模式,网上找了很多,大都是根据jpa来做多数据源解决方案,要不就是老的spring多数据源解 ...
- LCM性质 + 组合数 - HDU 5407 CRB and Candies
CRB and Candies Problem's Link Mean: 给定一个数n,求LCM(C(n,0),C(n,1),C(n,2)...C(n,n))的值,(n<=1e6). analy ...