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会自动将该对象传入,代表当前的连接点,通过该 ...
随机推荐
- Android Studio快捷键每日一练(5)
原文地址:http://www.developerphil.com/android-studio-tips-of-the-day-roundup-5/ 42.Enter键和Tab键补全 快捷键:Ent ...
- RichTextBoxEx2
using System;using System.Collections.Specialized;using System.Drawing;using System.Drawing.Imaging; ...
- MYSQL 函数 字符串到整数
SELECT CAST('002' AS signed) 结果为2 SELECT CAST('125e342.83' as signed) 结果为 125
- [DBW]格式化时间
Date.prototype.format = function(format) { //author: meizz let o = { , //月份 "d+": this.get ...
- C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield
IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...
- win10系统iis下部署搭建https (ssl/tls)本地测试环境
有时想要把公司的某些XX项目部署成https站点,是为了在传输层加密传输,防止他人嗅探站点重要数据信息,平常我们使用的http方式都是明文方式传输的很不安全,容易被他人窃取.而有些时候要在本地搭建ht ...
- 孙鑫MFC学习笔记17:进程间通信
17 1.进程间通信4种方式 2.OpenClipboard打开剪贴板 3.EmptyClipboard清空剪贴板,并把所有权分配给打开剪贴板的窗口 4.SetClipboardData设置剪贴板数据 ...
- SSH实例(1)
首先,配置struts.xml文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE st ...
- 【Java每日一题】20161109
package Nov2016; import java.lang.reflect.Method; public class Ques1109 { public static void main(St ...
- Scalaz(42)- Free :FreeAp-Applicative Style Programming Language
我们在前面花了几期时间讨论Free Monad,那是因为FP既是Monadic programming,Free Monad是FP模式编程的主要方式.对我们来说,Free Monad代表着fp从学术探 ...