Spring framework learning (3.2) AOP return notification, exception notification, surround notification

Preface

Record the learning process, Previous section
In the previous section, pre notification is implemented based on xml configuration file

AOP implementation based on XML configuration file

Return notification

The return notification is executed after the execution of the connection point. Whether the normal execution is completed or an exception is thrown, the contents of the return notification will be executed
Example:
(1) In the log notification class LogAdvice in the previous section, add the method:

  //This method will return notifications for
    public void MyAfterReturnAdvice(JoinPoint afterjoinPoint){
        List<Object> list= Arrays.asList(afterjoinPoint.getArgs());
        //Log format string
        String logInfo="Return notification:"+
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+
                " "+list.get(0).toString()+" Browse products: "+list.get(1).toString();
        System.out.println(logInfo);
    }

(2) Modify xml configuration file
Add < AOP: after returning > to the < AOP: aspect > element
The difference between < AOP: after returning > and < AOP: after >:
After is executed after after returning, but whether the method ends normally or not, the after notification will be executed


This is the difference between < AOP: after returning > and < AOP: after > in the later exception notification test

 <aop:after-returning method="MyAfterReturnAdvice" pointcut-ref="logpointcut"/>


It can be seen that only after the business method browse is executed can the log records generated by the return notification method in the log notification class be output

Exception notification

Exception notification is executed after the connection point throws an exception
Example:
(1) Add method in LogAdvice

//This method is exception notification
    public void MyThrowingAdvice(JoinPoint throwingPoint){
        //Get the name of the called class
        String targetClassName=throwingPoint.getTarget().getClass().getName();
        //Get the called method name
        String targetMethodName=throwingPoint.getSignature().getName();
        //Log format string
        String logInfo="Exception notification: Execution"+targetClassName+"Class"+
                targetMethodName+"Exception in method";
        System.out.println(logInfo);
    }

(2) Modify the MealServiceImpl class and throw an exception manually

package com.BeforeAdvice.Impl;

import com.BeforeAdvice.Service.MealService;
import org.springframework.stereotype.Service;

@Service("mealService")
public class MealServiceImpl implements MealService {
    @Override
    public void browse(String LoginName, String mealName) {
        System.out.println("Execute business method browse");
        //Throw an exception artificially
        throw new RuntimeException("This is an exception thrown by someone");
    }

}

(3) xml configuration

<aop:after-throwing method="MyThrowingAdvice" pointcut-ref="logpointcut" />

Around Advice

The circular notification is around the connection point, such as before and after a method call. This is the most powerful notification type. It can customize some operations before and after a method call. The circular notification also needs to decide whether to continue processing the joinpoint (calling the procedure method of the proceed ing joinpoint) or interrupt the execution
Example:
(1) Add method in LogAdvice

 //This method is a circular notification
    public void MyAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        long beginTime=System.currentTimeMillis();
        proceedingJoinPoint.proceed();
        long endTime=System.currentTimeMillis();
        //Get the called method name
        String targetMethodName=proceedingJoinPoint.getSignature().getName();
        //Log format string
        String logInfo="Surround notification:"+targetMethodName+"Time before method call"+
                beginTime+"Millisecond,"+"Time after call"+endTime+"Millisecond";
        System.out.println(logInfo);
    }

(2) Modify browse method in MealServiceImpl class
Extending the execution time of a method through a while loop

 int i=10000000;
        while (i>0){
            i--;
        }

(3) Modify xml configuration file
Specify the MyAroundAdvice method in LogAdvice as a surround notification

<aop:around method="MyAroundAdvice" pointcut-ref="logpointcut"/>

(4) Testing

It can be seen that the circular notification can record the time before and after the execution of the business method browse

Published 7 original articles, won praise 0, visited 46
Private letter follow

Tags: xml

Posted on Sat, 11 Jan 2020 10:14:36 -0500 by Tjk