Spring Boot 中使用Spring Aop实现日志记录功能

开发 项目管理
本篇带给大家Spring Boot 中使用Spring Aop实现日志记录功能,希望对你有所帮助。

一、创建日志注解

1、日志类型枚举类

import lombok.Getter;

/**
 * 日志类型枚举类
 */
@Getter
public enum LogType {
    /**
     * 登录
     */
    login("LoginLog");

    /**
     * 实现类名称
     */
    private final String implClassName;

    LogType(String implClassName) {
        this.implClassName = implClassName;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

2、自定义日志注解

import java.lang.annotation.*;
import com.baige.enums.LogType;

/**
 * 自定义日志注解
 *
 * 注解说明:
 * @Documented:文档注解
 * @Retention(RetentionPolicy.RUNTIME):生命周期注解,注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在
 * @Target({ ElementType.PARAMETER, ElementType.METHOD }):@Target说明了Annotation所修饰的对象范围,ElementType.TYPE:接口、类、枚举、注解,ElementType.METHOD:方法
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER, ElementType.METHOD })
public @interface Log {
    /**
     * 登录类型
     */
    LogType logType();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

二、利用AOP实现日志切面统一处理

1、日志实现类创建工厂

import com.baige.enums.LogType;
import com.baige.util.SpringContextHolder;
import org.springframework.context.ApplicationContext;

/**
 * 日志实现类创建工厂
 */
public class LogFactory {

    public static ApplicationContext context;

    public static IUserLog getInstance(LogType logType) {
        return (IUserLog) SpringContextHolder.getBean(logType.getImplClassName());
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

2、切面类

import lombok.extern.slf4j.Slf4j;
import com.baige.aop.log.IUserLog;
import com.baige.aop.annotation.Log;
import com.baige.aop.log.LogFactory;

import org.aspectj.lang.annotation.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class LogAspect {
    /**
     * 定义切入点,切入点为com.baige.controller.AopController中的所有函数
     * 通过@Pointcut注解声明频繁使用的切点表达式
     */
    @Pointcut("execution(public * com.baige.controller.UserController.*(..)))")
    public void pointcut() {
    }

    @Around("pointcut()")
    public void doAround(ProceedingJoinPoint pjp) {
        try {
            MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
            Log log = methodSignature.getMethod().getAnnotation(Log.class);

            IUserLog userLog = LogFactory.getInstance(log.logType());
            userLog.addLog(pjp, log);

        } catch (Throwable throwable) {
            log.info("日志记录异常,不影响正常业务", throwable);
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.

三、创建日志实现类

1、添加日志接口

import com.baige.aop.annotation.Log;
import org.aspectj.lang.ProceedingJoinPoint;

import java.net.UnknownHostException;

public interface IUserLog {
    /**
     * 添加日志
     */
    int addLog(ProceedingJoinPoint pjp, Log log) throws UnknownHostException;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

2、添加日志接口实现类

import java.util.Date;
import java.net.InetAddress;
import java.net.UnknownHostException;

import lombok.AllArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

import com.baige.domain.User;
import com.baige.domain.UserLog;
import com.baige.aop.log.IUserLog;
import com.baige.mapper.UserMapper;
import com.baige.aop.annotation.Log;
import com.baige.mapper.UserLogMapper;

@AllArgsConstructor
@Component("LoginLog")
public class LoginLog implements IUserLog {

    private final UserMapper userMapper;
    private final UserLogMapper userLogMapper;

    @Override
    public int addLog(ProceedingJoinPoint pjp, Log log) throws UnknownHostException {
        UserLog userLog = new UserLog();
        Object[] params = pjp.getArgs();

        if (params.length > 0) {
            User param = (User) params[0];
            User user = userMapper.getOne(param.getLoginName(), param.getLoginPassword());
            if (user != null) {
                userLog.setUserId(user.getId());
                userLog.setContent("获取用户信息成功");
            } else {
                userLog.setContent("未获取到用户信息");
            }
        }

        userLog.setIpAddress(InetAddress.getLocalHost().getHostAddress());
        userLog.setCreateTime(new Date());

        return userLogMapper.insert(userLog);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.

四、controller

package com.baige.controller;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import com.baige.domain.User;
import com.baige.enums.LogType;
import com.baige.aop.annotation.Log;
import com.baige.service.UserService;

@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    @PostMapping("/login")
    @Log(logType = LogType.login)
    public String login(@RequestBody User params) {
        User user = userService.login(params.getLoginName(), params.getLoginPassword());
        return user != null ? "登录成功" : "登录失败";
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
责任编辑:姜华 来源: 今日头条
相关推荐

2021-03-01 23:26:41

日志Spring BootAOP

2023-03-30 07:48:46

接口鉴权SpringBoot

2022-02-17 13:39:09

AOP接口方式

2009-06-22 15:10:00

java 编程AOP

2023-11-26 09:10:34

WebSocketgreeting​在线用户

2021-08-11 05:00:48

Spring 日志手段

2019-04-15 08:32:25

Spring Boot日志门面模式

2022-07-26 16:54:08

QuartzJava

2024-12-03 08:00:00

2023-07-17 18:42:47

gRPCDemo项目

2009-06-19 11:09:27

Spring AOP

2022-02-09 20:39:52

Actuator应用监控

2024-09-02 00:27:51

SpringAOP自定义

2017-12-27 15:16:35

Spring BootFlyway数据库

2025-02-28 09:47:36

2009-06-19 13:28:30

Spring AOPSpring 2.0

2022-06-07 07:58:45

SpringSpring AOP

2022-09-26 10:01:04

SpringAOP日志

2022-05-12 11:38:26

Java日志Slf4j

2023-09-13 08:56:51

点赞
收藏

51CTO技术栈公众号