欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot集成aop(实现日志的记录)

程序员文章站 2022-05-15 17:16:27
...
  1. 添加AOP依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
  1. 创建一个自定义的注解
package com.cn.annotation;

import java.lang.annotation.*;

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SysAnnotation {

    String value() default "";
    String operation() default "";
    String exceptionMessage() default "系统内部异常";
}
  1. 添加日志切面
package com.cn.aspect;

import com.cn.annotation.SysAnnotation;
import com.cn.dao.SysLogMapper;
import com.cn.model.SysLog;
import com.cn.utils.IPUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

@Aspect
@Component
public class SysLogAspect {

    @Autowired
    private SysLogMapper sysLogMapper;

    @Pointcut("@annotation(com.cn.annotation.SysAnnotation)")
    public void logPointCut(){
    }

    /**
     * 环绕通知
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Object result = null;
        try{
            SysLog sysLog = new SysLog();
            MethodSignature signature = (MethodSignature) point.getSignature();
            Method method = signature.getMethod();
            //获取注解
            SysAnnotation controllerEndpoint = method.getAnnotation(SysAnnotation.class);
            if(controllerEndpoint!=null)
            {
                String operation = controllerEndpoint.operation();
                //注解上的操作描述
                sysLog.setOperation(operation);
            }
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            //操作者ip
            String ip = IPUtil.getIPAddress();
            sysLog.setIp(ip);
            //操作的url
            String url = request.getRequestURI();
            sysLog.setUrl(url);
            //操作时间
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            sysLog.setOperationTime(dateFormat.format(new Date()));
            sysLog.setUsername("马云");
            //执行目标方法
            result=point.proceed();
            //请求的方法名
            String className = point.getTarget().getClass().getName();
            String methodName = signature.getName();
            sysLog.setMethod(className + "." + methodName);
            sysLogMapper.insertSelective(sysLog);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}
  1. 启动项目,随便访问一个接口,然后查看数据库的操作日志是否添加,下面是我自己随便写的接口
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @SysAnnotation(operation = "查询用户",exceptionMessage = "查询用户失败")
    @GetMapping(value = "/getList")
    public List<UserInfo> getList(){
       return userService.getList();
    }

springboot集成aop(实现日志的记录)

  • 查看数据库的日志表,确实添加了一条,说明操作日志添加成功
    springboot集成aop(实现日志的记录)