Spring AOP 四大通知
Spring AOP 四大通知
Spring 3.X 以前
1.前置通知,实现 MethodBeforeAdvice 接口,重写
public void before(Method method, Object[] args, Object target) throws Throwable方法
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class TestMethodBeforeAdvice implements MethodBeforeAdvice {
/** arg0 方法
* arg1 参数
* arg2 操作对象
* */
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("前置通知:----》方法:"+arg0.getName()+"传入参数"+arg1+"操作象"+arg2);
}
}
2.后置通知,实现 AfterReturningAdvice 接口,重写
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable 方法
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class TestAfterReturningAdvice implements AfterReturningAdvice{
/**
* arg0:return 的返回值
* arg1:执行的方法对象
* arg2:方法执行中传递过来的参数
* arg3:执行方法的对象
*/
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("后置通知:----》方法:"+arg1.getName()+"返回值:"+arg0+"执行方对象:"+arg3);
}
}
3.环绕通知, 实现 MethodInterceptor 接口,重写
public Object invoke(MethodInvocation invocation) throws Throwable 方法
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class TestMethodinterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod() ; //方法
Object[] objs = invocation.getArguments() ; //参数
Object obj = invocation.getThis() ; //操作对象
System.out.println("环绕通知:-----》 开始: 方法:"+method.getName()+"传入的参数:"+objs+" 操作对象:"+obj);
Object result = invocation.proceed() ; //放行
System.out.println("环绕通知:-----》 结束: 返回值:"+result);
return result ;
}
}
4.异常通知,实现 ThrowsAdvice 接口,重写
public void afterThrowing(Method m, Object args, Object target,Throwable e) 方法
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class TestThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Method m, Object args, Object target, Throwable e) {
System.out.println("异常通知:方法"+m.getName()+"发生异常,"+e.getMessage());
System.exit(0);
}
}
注意:查看ThrowsAdvice源码会发现这个接口里面没有定义方法,但是这个方法必须这么写,
Spring 3.X 以后版本
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class TestAdvice {
public void before(JoinPoint joinPoint){ //前置通知
System.out.println("操作者"+joinPoint.getTarget()+"参数 "+joinPoint.getArgs()[0]);
System.out.println("*********************前置通知*********************");
}
//后置通知:当方法执行完会触发,出错则不执行
public void afterReturning(JoinPoint joinPoint,Object obj){
System.out.println("后置通知");
System.out.println("返回结果:"+obj);
}
//最终通知
public void after(JoinPoint joinPoint){
System.out.println("最终通知");
System.out.println("调用的方法"+joinPoint.getSignature());
}
//异常通知
public void throwAdvice(Exception exception){
System.out.println("--------异常通知--------");
System.out.println("异常消息"+exception.getMessage());
}
//环绕通知
public Object around(ProceedingJoinPoint proceedingJoinPoint){
System.out.println("环绕通知开始");
try {
Object obj = proceedingJoinPoint.proceed() ;
System.out.println("环绕通知结束");
return obj ;
} catch (Throwable e) {
e.printStackTrace();
}
return null ;
}
}
配置信息
<!--Spring3.X以前-->
<!--后置通知-->
<bean id="afterAdvice" class="com.spring.advice.TestAfterReturningAdvice"/>
<!--前置通知-->
<bean id="beforeAdvice" class="com.spring.advice.TestMethodBeforeAdvice"/>
<!--环绕通知-->
<bean id="interceptorAdvice" class="com.spring.advice.TestMethodinterceptor"/>
<!--异常通知-->
<bean id="throwsAdvice" class="com.spring.advice.TestThrowsAdvice"/>
<!--Spring3.X以后整合-->
<bean id="advice" class="com.spring.advice.TestAdvice"/>
<!-- AOP设置 -->
<aop:config>
<aop:pointcut expression="execution(* com.spring.service.*.*(..))" id="mycut"/>
<aop:advisor advice-ref="afterAdvice" pointcut-ref="mycut"/>
<aop:advisor advice-ref="beforeAdvice" pointcut-ref="mycut"/>
<aop:advisor advice-ref="interceptorAdvice" pointcut-ref="mycut"/>
<aop:advisor advice-ref="throwsAdvice" pointcut-ref="mycut"/>
<!-- 新版本 -->
<aop:aspect ref="advice">
<aop:before method="before" pointcut-ref="mycut"/>
<aop:after-returning method="afterReturning" returning="obj" pointcut-ref="mycut"/>
<aop:after method="after" pointcut-ref="mycut"/>
<aop:after-throwing method="throwAdvice" throwing="exception" pointcut-ref="mycut"/>
<aop:around method="around" pointcut-ref="mycut"/>
</aop:aspect>
</aop:config>
两种方法:
Spring3.X版本以前写法思路更清晰,新版本,虽然把4个通知整合在了一起,但是,如果业务复杂的话,通知较多建议分开写,
两种方法区别不是很大,具体还得开需求
expression的value值
任意公共方法的执行:
execution(public * *(..))
任何一个以“set”开始的方法的执行:
execution(* set*(..))
AccountService 接口的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
定义在service包里的任意方法的执行:
execution(* com.xyz.service.*.*(..))
定义在service包或者子包里的任意方法的执行:
execution(* com.xyz.service..*.*(..))
若要转载,请标明此处
Spring AOP 四大通知的更多相关文章
- Spring笔记07(Spring AOP的通知advice和顾问advisor)
1.Spring AOP的通知advice 01.接口代码: package cn.pb.dao; public interface UserDao { //主业务 String add(); //主 ...
- spring aop 环绕通知around和其他通知的区别
前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知 是不能决定的,他们只 ...
- spring aop环绕通知
[Spring实战]—— 9 AOP环绕通知 假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...
- Spring AOP前置通知和后置通知
Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...
- Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)
本文例子完整源码地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/Spring%20AOP%E9%A ...
- [转载] spring aop 环绕通知around和其他通知的区别
前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知 是不能决定的,他们只 ...
- Spring AOP前置通知实例说明AOP相关概念
今天又看了下韩顺平的SpringAOP的讲解,讲解的很透彻.仿照视频自己使用下前置通知. 一.引出问题 有个接口TestServiceInter,有两个实现方法TestService和Test2Ser ...
- Spring AOP前置通知实例讲解与AOP详细解析
一.引出问题 有个接口TestServiceInter,有两个实现方法TestService和Test2Service.他们都有sayHello():我们的需求是在调用这两个方法之前,要先完成写日志的 ...
- 【Spring AOP】通知(五)
一.通知介绍 1. 前置通知(Before) 在目标方法执行之前执行的通知. 前置通知方法,可以没有参数,也可以额外接收一个JoinPoint,Spring会自动将该对象传入,代表当前的连接点,通过该 ...
随机推荐
- String.split()用法以及特殊分隔符注意,ps:|
转载:http://www.cnblogs.com/mingforyou/archive/2013/09/03/3299569.html 在java.lang包中有String.split()方法,返 ...
- 【模式匹配】KMP算法的来龙去脉
1. 引言 字符串匹配是极为常见的一种模式匹配.简单地说,就是判断主串\(T\)中是否出现该模式串\(P\),即\(P\)为\(T\)的子串.特别地,定义主串为\(T[0 \dots n-1]\),模 ...
- .NET Core爬坑记 1.0 项目文件
前言: 之所以要写这个系列是因为在移植项目到ASP.NET Core平台的过程中,遇到了一些“新变化”,这些变化有编译方面的.有API方面的,今天要讲的是编译方面的一些问题.我把它们整理后分享出来,以 ...
- CentOS7上让Jexus开机自启动
昨天刚用了一下CentOS7,很自然的就安装了mono和Jexus,用的都是目前最新版mono4.2.2.10和jexus5.8.0 mono和jexus的具体安装方法,园子里已经有了很多教程,这里就 ...
- JS打印页面指定区域
错误的写法: //打印 function printPage(areaId) { if (parent.$("#PrinFrame").length == 0) { parent. ...
- 使用VS Code开发ASP.NET Core 应用程序
最新教程:http://www.cnblogs.com/linezero/p/VSCodeASPNETCore.html 使用VS Code开发ASP.NET Core 应用程序 准备 1.安装VS ...
- Oracle忘记密码的处理办法
可能有的人在很长一段时间不用Oracle数据库,然后在反过来用的时候就会发现自己依然忘记了密码... 这里提供一个忘记密码后的解决办法 首先呢,你打开数据库用 sys或者是sysdba超级管理员用户名 ...
- 组件Newtonsoft.Json实现object2json转换
很多情况下,我们需要把数据类型做一些转换,供其它外部的子系统调用. 最为典型的是生成json格式供javascript作调用. 现成的组件Newtonsoft.Json可以实现object2json之 ...
- jsp错误路径跳转设置
<error-page><error-code>400</error-code><location>/king.html</location> ...
- 【转】Android世界的Swift - Kotlin语言
最近Oracle又在和Google撕的厉害,才知道还有这么Kotlin在Android像Swift在Mac的地位一说. Kotlin是一门与Swift类似的静态类型JVM语言,由JetBrains设计 ...