Implementing AOP Logging in a Spring Boot Application

This tutorial demonstrates how to integrate an AOP logging module into a Spring Boot project by adding required dependencies, creating a custom aspect that extends LBaseWebLogAspect, defining a pointcut, and exposing a sample REST endpoint that returns user data while logging detailed request information.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Implementing AOP Logging in a Spring Boot Application

This article, originally authored by Erwin_Feng on the imooc platform, provides a step‑by‑step guide for adding AOP‑based request logging to a Spring Boot application.

Step 1

Add the required Maven/Gradle dependencies for the AOP logging module (highlighted with a red border in the original UI).

Step 2

Create a custom aspect class that extends LBaseWebLogAspect and implements the pointCut() method to specify the join point.

@Component
class WebLogAspect extends LBaseWebLogAspect {

    @Pointcut("execution(* com.fengwenyi.javalibexampleaoplog..*.*(..))")
    @Override
    protected void pointCut() { }
}

Step 3

Write a test controller that uses the previously defined pointcut. The controller defines a /getUsers endpoint returning a list of User objects.

package com.fengwenyi.javalibexampleaoplog;

import com.fengwenyi.javalib.aop.LBaseWebLogAspect;
import com.fengwenyi.javalib.result.DefaultReturnCode;
import com.fengwenyi.javalib.result.Result;
import com.fengwenyi.javalib.result.ResultResponseUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class JavalibExampleAoplogApplication {

    public static void main(String[] args) {
        SpringApplication.run(JavalibExampleAoplogApplication.class, args);
    }

    /**
     * Get all user data
     */
    @GetMapping("/getUsers")
    public Result getUsers() {
        User user1 = new User().setName("冯文议").setAge(26).setGender("男");
        User user2 = new User().setName("张三").setAge(25).setGender("女");
        User user3 = new User().setName("李四").setAge(23).setGender("保密");
        User user4 = new User().setName("王五").setAge(20).setGender("未知");
        User[] users = {user1, user2, user3, user4};
        return ResultResponseUtil.ok().status(DefaultReturnCode.SUCCESS).data(users);
    }
}

@Aspect
@Component
class WebLogAspect extends LBaseWebLogAspect {
    @Pointcut("execution(* com.fengwenyi.javalibexampleaoplog..*.*(..))")
    @Override
    protected void pointCut() { }
}

@Data
@Accessors(chain = true)
class User {
    // name
    private String name;
    // age
    private Integer age;
    // gender
    private String gender;
}

Step 4

The AOP aspect records detailed access logs, including:

Client IP address

Request URL

HTTP method (GET, POST, …)

Target class and method

Request parameters (as JSON string)

Returned data object

Execution time in nanoseconds

Reference code and libraries can be found at:

Test code: GitHub – JavaLibExampleAopLog

JavaLib library: GitHub – JavaLib

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaaoploggingSpring Bootaspectj
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

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.