Advice Types

Before Advice

aop:before

<aop:before method="printLog" pointcut-ref="pointcut">
</aop:before>
  • method: Used to specify the before advice method name
  • pointcut: Used to specify the pointcut expression
  • pointcut-ref: Used to specify the pointcut expression reference

Function: Used to configure before advice Location: Inside aop:aspect Timing: Before advice always executes before the pointcut method (business core method) executes Details: Before advice can get the pointcut method’s parameters and enhance them

After-Throwing Advice

aop:after-throwing

<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pointcut'>
</aop:after-throwing>

Used to configure after-throwing advice Location: Can only be inside aop:aspect tag Timing: After-throwing advice executes after the pointcut method (business core method) throws an exception. If the pointcut method executes without exception, after-throwing advice will not execute Details: After-throwing advice can not only get the pointcut method’s execution parameters but also get the exception information thrown by the pointcut method

After Advice

Used to specify after advice. Location: Can only be inside aop:aspect tag

<aop:after method="afterPrintLog" pointcut-ref="pointcut">
</aop:after>

Timing: After advice executes after the pointcut method (business core method) completes and before the pointcut method returns. In other words, regardless of whether the pointcut method throws an exception, it executes before returning. Details: In the after advice phase, you can get the advice method’s parameters. It can do some cleanup operations.

Around Advice

Configuration method:

<aop:around method="aroundPrintLog" pointcut-ref="pt1"></aop:around>

Function: Used to configure around advice Location: Can only be inside aop:aspect tag Attributes:

  • method: Used to specify the around advice method name
  • pointcut: Used to specify the pointcut expression
  • pointcut-ref: Used to specify the pointcut expression reference

Around advice is a special advice type different from the previous four types (before, after, after-throwing, and after-returning). Those four types specify when to enhance. Around advice is a type that Spring framework provides that allows you to control when to execute the enhancement code through coding. It uses the ProceedingJoinPoint interface and implementation class to manually trigger the pointcut method call.


XML + Annotation Approach

Enable Annotation

<!-- Enable Spring's annotation AOP support -->
<aop:aspectj-autoproxy/>

Example Code

WzkLogUtils

/**
 * WzkLogUtils
 * @author wzk
 * @date 11:26 2025/1/6
**/
@Component
@Aspect
public class WzkLogUtils {

    /**
     * When referencing pointcut expression, must be method name+(), e.g., pointcut()
     * Can directly write method name when used in the same aspect, must use fully qualified method name when used in other aspects
     * @author wzk
     * @date 11:28 2025/1/6
    **/
    @Pointcut("execution(* wzk.service.impl.*.*(..))")
    public void pointcut() {

    }

    /**
     * Before advice
     * @author wzk
     * @date 13:50 2025/1/6
    **/
    @Before("pointcut()")
    public void beforePrintLog(JoinPoint jp) {
        Object[] args = jp.getArgs();
        System.out.println("Before advice: beforePrintLog: " + Arrays.toString(args));
    }

    /**
     * After-throwing advice
     * @author wzk
     * @date 13:50 2025/1/6
    **/
    @AfterThrowing(value = "pointcut()", throwing = "e")
    public void afterThrowingPrintLog(JoinPoint joinPoint, Throwable e) {
        System.out.println("After-throwing advice: afterThrowingPrintLog: " + joinPoint.getSignature().getName() + " Exception: " + e.getMessage());
    }

    /**
     * After advice
     * @author wzk
     * @date 13:50 2025/1/6
    **/
    @After("pointcut()")
    public void afterPrintLog(JoinPoint joinPoint) {
        System.out.println("After advice: afterPrintLog: " + joinPoint.getSignature().getName());
    }

    /**
     * Around advice
     * @author wzk
     * @date 13:50 2025/1/6
    **/
    @Around("pointcut()")
    public Object aroundPrintLog(ProceedingJoinPoint proceedingJoinPoint) {
        Object result = null;
        try {
            System.out.println("Around advice: beforePrintLog: " + proceedingJoinPoint.getSignature().getName());
            result = proceedingJoinPoint.proceed();
            System.out.println("Around advice: afterPrintLog: " + proceedingJoinPoint.getSignature().getName());
        } catch (Throwable e) {
            System.out.println("Exception advice");
            e.printStackTrace();
        } finally {
            System.out.println("Finally advice");
        }
        return result;
    }

    public void printLog() {
        System.out.println("WzkLogUtils: printLog");
    }

}

Annotation Mode

When developing AOP with annotation-driven approach, we need to be clear that annotations replace the following configuration in the configuration file:

<!--Enable Spring's annotation AOP support-->
<aop:aspectj-autoproxy/>

This configuration is no longer needed, so comment it out in applicationContext.xml.

In the configuration class, use the following annotation to replace the above configuration:

/**
 * WzkAopConfig
 * @author wzk
 * @date 13:59 2025/1/6
**/
@Configuration
@Component("wzk")
@EnableAspectJAutoProxy
public class WzkAopConfig {

}