SpringBoot AOP实现日志记录

幽灵船长 2024-06-10 ⋅ 39 阅读

介绍

在开发过程中,日志记录是一个非常重要的环节。它可以帮助我们排查问题、监控系统运行情况以及提供数据分析等功能。本篇博客将介绍如何使用SpringBoot和AOP来实现日志记录功能。

准备工作

在开始之前,我们需要准备以下条件:

  • JDK 1.8+
  • Maven
  • SpringBoot

添加依赖

首先,在你的SpringBoot项目中的pom.xml文件中添加以下依赖:

<dependencies>
    <!-- SpringBoot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    
    <!-- AOP -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    
    <!-- 日志记录 -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
    </dependency>
</dependencies>

创建切面类

接下来,我们需要创建一个切面类,用于在方法执行前和执行后添加日志记录。在这个切面类中,我们可以定义各种不同类型的通知,例如@Before、@After等。

package com.example.demo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class LogAspect {

    @Before("execution(* com.example.demo.controller.*.*(..))")
    public void beforeControllerMethod(JoinPoint joinPoint) {
        log.info("Before {} method execution", joinPoint.getSignature().getName());
    }

    @After("execution(* com.example.demo.controller.*.*(..))")
    public void afterControllerMethod(JoinPoint joinPoint) {
        log.info("After {} method execution", joinPoint.getSignature().getName());
    }
}

在上述代码中,我们定义了两个通知方法:beforeControllerMethod()afterControllerMethod()。这两个方法分别在目标方法执行前和执行后添加日志记录。

注册切面类

要使切面类生效,我们需要手动将其注册到Spring容器中。在SpringBoot中,我们可以使用@EnableAspectJAutoProxy注解来自动开启AOP功能,同时也会自动扫描并注册切面类。

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@EnableAspectJAutoProxy
@SpringBootApplication
public class DemoApplication {

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

}

测试

为了验证日志记录是否正常工作,我们可以在Controller层中添加一些方法,并通过调用这些方法来触发切面。

package com.example.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
@Slf4j
public class ApiController {

    @GetMapping("/hello")
    public String hello() {
        log.info("Hello method executed");
        return "Hello World";
    }

    @GetMapping("/bye")
    public String bye() {
        log.info("Bye method executed");
        return "Goodbye";
    }
}

现在,我们可以启动应用程序,并在浏览器中访问http://localhost:8080/api/hellohttp://localhost:8080/api/bye,然后查看日志文件,以验证日志记录是否生效。

总结

通过使用SpringBoot和AOP,我们可以方便地实现日志记录功能。本篇博客介绍了如何创建切面类、注册切面类以及测试日志记录的方法。希望本文对你有所帮助,如果有任何问题,请随时留言交流。


全部评论: 0

    我有话说: