Backend Development 6 min read

Spring AOP Advice Types, Execution Order, and Sample Code

This article explains the five Spring AOP advice types, their execution order when combined in a single aspect, and provides a complete Java code example demonstrating before, after, after‑returning, after‑throwing, and around advices with normal and exceptional flows.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Spring AOP Advice Types, Execution Order, and Sample Code

Introduction

This article is relatively simple, but many interviewees were asked about these concepts and could not answer; the techniques are widely used in projects, so developers should stay curious and examine the code.

Advice Types

In Spring AOP programming based on the AspectJ framework, Spring defines five types of advice:

Before advice (@Before)

After‑returning advice (@AfterReturning)

After‑throwing advice (@AfterThrowing)

After advice (@After)

Around advice (@Around) – highest priority

Advice Execution Order

When all the above advice types are placed in the same aspect, their execution order is as follows:

Code Example

package com.cy.pj.common.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SysTimeAspect {

    /**
     * Pointcut definition
     */
    @Pointcut("bean(sysMenuServiceImpl)")
    public void doTime() {}

    @Before("doTime()")
    public void doBefore(JoinPoint jp) {
        System.out.println("time doBefore()");
    }
    @After("doTime()")
    public void doAfter() {
        // similar to a finally block
        System.out.println("time doAfter()");
    }
    /** Core business ends normally */
    @AfterReturning("doTime()")
    public void doAfterReturning() {
        System.out.println("time doAfterReturning");
    }
    /** Core business throws an exception */
    @AfterThrowing("doTime()")
    public void doAfterThrowing() {
        System.out.println("time doAfterThrowing");
    }
    @Around("doTime()")
    public Object doAround(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("doAround.before");
        try {
            Object obj = jp.proceed();
            return obj;
        } catch (Throwable e) {
            System.out.println("doAround.error->" + e.getMessage());
            throw e;
        } finally {
            System.out.println("doAround.after");
        }
    }
}

Code Normal Completion

Code Exception Occurrence

Author: 其乐m Source: my.oschina.net/u/4115134/blog/3216359

END

Recommended Issues

【231】Interview Question: What is the role of serialVersionUID in Java?

【232】Interview Question: What advantages does Spring's IoC container have over using new?

【233】Interview Question: What is coupling and how can it be reduced?

【234】30 Essential Java Collection Interview Questions and Answers

【235】Interview Question: Is Redis data stored in memory? Discuss usage scenarios for different Redis data types.

【236】Interview Question: How are excess threads in a thread pool reclaimed?

【237】Interview Question: How to detect Redis hot keys and what are the solutions?

【238】Interview Question: Redis introduced multithreading in newer versions – your thoughts?

【239】Interview Question: How to use Redis for inventory deduction in e‑commerce systems?

【240】Interview Question: Do you understand JVM memory overflow?

Instead of searching the web for questions, follow us now!

backendJavaAOPSpringAdviceAspectJ
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.