JAVA-Spring AOP five types of notifications

First, the pre-notification

    Advice to be executed before the target method execution

    In the pre-notification method can be used without parameters, a receiver may additionally JoinPoint, Spring will automatically pass the object representing the current point of attachment, and the target audiences can obtain information related to the method by the subject. Note that if the receiving JoinPoint, must ensure that its first argument is a method, otherwise an error!

    Configuration:

    

Second, around advice

    The method of execution in the target before and after the notification can execute additional code.

    You must explicitly invoke the target method, otherwise the target method does not perform in around advice.

    This explicit when invoked by ProceedingJoinPoint, may receive a parameter in this type around the notification, spring container automatically passed the object, this parameter must be in a first position around the notification parameter.

    ** Note that only around advice can receive ProceedingJoinPoint, while others can only receive notice JoinPoint.

    Configuration:

      

    Around advice you need to return the return value, otherwise it will not get the return call to the real value, only get a null.

    Around the target is notified of the control method are performed before the code additional target method or after execution, control returns to whether there is a value, and even the ability to change the return value

    Around advice spite of this ability, but it must be used with caution, be careful not to damage the "high cohesion and low coupling" target software layered.

 

Third, after advice

    Notice after the target method performed.

    After returning advice can also be selectively receive a JoinPoint to obtain additional information from the connection point, but this parameter must be in the first argument list.

    

 

 

 

     In the post-notification, return values ​​may also be obtained by arranging

    

    We must ensure that JoinPoint in the parameter list of the first, otherwise Throws

    

Fourth, the exception notification

    Advice to be executed when the target method throws an exception

    Configuration:

      

    可以配置传入JoinPoint获取目标对象和目标方法相关信息,但必须处在参数列表第一位,另外,还可以配置参数,让异常通知可以接收到目标方法抛出来的异常对象

    

 

五、最终通知

    是在目标方法执行之后执行的通知。和后置通知不同的是,后置通知是在方法正常返回后执行的通知,如果方法没有正常返回,比如说抛出异常,则后置通知不会执行。而最终通知无论如何都会在目标方法调用过后执行,即使目标方法没有正常的执行完成。另外,后置通知可以通过配置得到返回值,而最终通知无法得到。

    配置方式:

        

      最终通知也可以额外接受一个JoinPoint参数,来获取目标对象和目标方法相关信息,但一定要保证必须是第一个参数。

    

 

 -------------------------------------------------------------LK------------------------------------------------------------------------

五种通知执行的顺序

  1.在目标方法没有抛出异常的情况下

    前置通知

    环绕通知的调用目标方法之前的代码   //--取决于配置顺序

    目标方法

    环绕通知的调用目标方法之后的代码

    后置通知              //--取决于配置顺序

    最终通知

  2.在目标方法抛出异常的情况下:

    前置通知

    环绕通知的调用目标方法之前的代码  //--取决于配置顺序

    目标方法    //抛出异常

    异常通知

    最终通知

  3.如果存在多个切面:

    多切面执行时,采用了责任链设计模式

    切面的配置顺序决定了切面的执行过程,类似于方法调用的过程,在环绕通知的proceed()执行时,去执行下一个切面或如果没有下一个切面执行目标方法,从而达成了如下的执行过程:

    

 

    如果目标方法抛出异常:

    

 

 

五种通知的常见使用场景

    

 

Guess you like

Origin www.cnblogs.com/xiaoluohao/p/11286242.html