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 四大通知的更多相关文章

  1. Spring笔记07(Spring AOP的通知advice和顾问advisor)

    1.Spring AOP的通知advice 01.接口代码: package cn.pb.dao; public interface UserDao { //主业务 String add(); //主 ...

  2. spring aop 环绕通知around和其他通知的区别

    前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知   是不能决定的,他们只 ...

  3. spring aop环绕通知

    [Spring实战]—— 9 AOP环绕通知   假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...

  4. Spring AOP前置通知和后置通知

    Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...

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

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

  6. [转载] spring aop 环绕通知around和其他通知的区别

    前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知   是不能决定的,他们只 ...

  7. Spring AOP前置通知实例说明AOP相关概念

    今天又看了下韩顺平的SpringAOP的讲解,讲解的很透彻.仿照视频自己使用下前置通知. 一.引出问题 有个接口TestServiceInter,有两个实现方法TestService和Test2Ser ...

  8. Spring AOP前置通知实例讲解与AOP详细解析

    一.引出问题 有个接口TestServiceInter,有两个实现方法TestService和Test2Service.他们都有sayHello():我们的需求是在调用这两个方法之前,要先完成写日志的 ...

  9. 【Spring AOP】通知(五)

    一.通知介绍 1. 前置通知(Before) 在目标方法执行之前执行的通知. 前置通知方法,可以没有参数,也可以额外接收一个JoinPoint,Spring会自动将该对象传入,代表当前的连接点,通过该 ...

随机推荐

  1. SQL Server如何在变长列上存储索引

    这篇文章我想谈下SQL Server如何在变长列上存储索引.首先我们创建一个包含变长列的表,在上面定义主键,即在上面定义了聚集索引,然后往里面插入80000条记录: -- Create a new t ...

  2. SQL Server安全(10/11):行级别安全(Row-Level Security)

    在保密你的服务器和数据,防备当前复杂的攻击,SQL Server有你需要的一切.但在你能有效使用这些安全功能前,你需要理解你面对的威胁和一些基本的安全概念.这篇文章提供了基础,因此你可以对SQL Se ...

  3. .Net语言 APP开发平台——Smobiler学习日志:用Gridview控件设计较复杂的表单

    最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的”Smobil ...

  4. 实时数据显示--SignalR实例演示

    近段时间,有实现一个看板的功能,就是用户更新信息时,即是对数据库的数据进行插入,更新,或是删除时,在墙上的屏幕的数据不需要人为去刷新,用户就能看到更新后的数据. 实现此功能,Insus.NET使用Si ...

  5. java for 的用法总结

    J2SE 1.5提供了另一种形式的for循环.借助这种形式的for循环,可以用更简单地方式来遍历数组和Collection等类型的对象.本文介绍使用这种循环的具体方式,说明如何自行定义能被这样遍历的类 ...

  6. 项目中的web api知识总结

    最近在做公司的项目,自己负责webapi的框架的搭建与开发,最近很忙,一直没时间对工作中遇到的知识点缺少个总结,现总结一下,对自己是个提升,如果个人的小点点小总结能对博友有帮助那也是善莫大焉. (1) ...

  7. 【Java每日一题】20161124

    package Nov2016; public class Ques1124 { public static void main(String[] args) { Parent p = new Sub ...

  8. MongoDB-query查询接口

    在上一篇中简要使用了C# 对MongoDB进行数据操作,这里补充一些MongoDB query查询条件文档: Query.All("name", "a",&qu ...

  9. python验证登录

    一个web2.0时代的网站,自然少不了用户注册,登录,验证的功能,那么python可以怎样实现登录验证呢 这里我们使用装饰器来做登录验证 网站构成 假设我们有这样一个网站,是一个类似与博客园这种多个用 ...

  10. PhantomJS快速入门

    本文简要介绍了PhantomJS的相关基础知识点,主要包括PhantomJS的介绍.下载与安装.HelloWorld程序.核心模块介绍等.由于鄙人才疏学浅,难免有疏漏之处,欢迎指正交流. 1.Phan ...