如何新增切面,日志切面

增加切面,对操作进行记录

第一步 新建日志实体

import java.io.Serializable;
import java.sql.Timestamp;

public class SysLog {

  private static final long serialVersionUID = 1L;

  public static final String USERNAME_COLUMN = "username";
  public static final String OPERATION_COLUMN = "operation";

  /**
   * 自增id
   */
  private Long id;
  /**
   * 用户id
   */
  private Long userId;
  /**
   * 用户名
   */
  private String username;
  /**
   * 用户操作
   */
  private String operation;
  /**
   * 响应时间
   */
  private Long time;
  /**
   * 请求方法
   */
  private String method;
  /**
   * IP地址
   */
  private String ip;
  /**
   * 创建时间
   */
  private Timestamp cTime;



  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public Long getUserId() {
    return userId;
  }

  public void setUserId(Long userId) {
    this.userId = userId;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getOperation() {
    return operation;
  }

  public void setOperation(String operation) {
    this.operation = operation;
  }

  public Long getTime() {
    return time;
  }

  public void setTime(Long time) {
    this.time = time;
  }

  public String getMethod() {
    return method;
  }

  public void setMethod(String method) {
    this.method = method;
  }

  public String getIp() {
    return ip;
  }

  public void setIp(String ip) {
    this.ip = ip;
  }

  public Timestamp getcTime() {
    return cTime;
  }

  public void setcTime(Timestamp cTime) {
    this.cTime = cTime;
  }


  @Override
  public String toString() {
    return "SysLog{" +
        "id=" + id +
        ", userId=" + userId +
        ", username='" + username + '\'' +
        ", operation='" + operation + '\'' +
        ", time=" + time +
        ", method='" + method + '\'' +
        ", ip='" + ip + '\'' +
        ", cTime=" + cTime +
        '}';
  }
}

第二步 新建一个切点接口

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

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

    String value() default "";

}

第三步 新建一个切面

@Aspect
@Component
public class SysLogAspect {

    @Resource
    private SysLogMapper sysLogMapper;

    @Pointcut("@annotation(**此处为切点接口全路径类名**)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        //保存日志
        saveSysLog(point, time);
        return result;
    }
    /**
     * @Description:保存日志
     */
    private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SysLog sysLog = new SysLog();
        SysLogAnno sysLogAnno = method.getAnnotation(SysLogAnno.class);
        if (sysLogAnno != null) {
            //注解上的描述 即@SysLogAnno("XXX")括号里的内容
            sysLog.setOperation(sysLogAnno.value());
        }
        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");

        //获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        //设置IP地址
        sysLog.setIp(IPUtils.getIpAddr(request));
        //获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        //设置IP地址
        sysLog.setIp(IPUtils.getIpAddr(request));
        //以下为演示功能 数据写死
        int id = 3;
        Long l = (long)id;
        sysLog.setUserId(l);
        sysLog.setUsername("jlm");
        sysLog.setTime(time);
        sysLog.setcTime(new Timestamp(System.currentTimeMillis()));
        //保存系统日志
        //本教程使用MyBatis对数据进行存储
        sysLogMapper.insertLog(sysLog);

    }

第四步 在需要记录操作的Controller层的方法上添加注解@SysLogAnno

例如:

	@SysLogAnno("修改业务类别")
    @RequestMapping("/updateBusinessClasses.do")
    @ResponseBody
    public Map<String ,Object> updateBusinessClasses(
            @RequestParam("describes") String describes,
            @RequestParam("deptId") Integer deptId,
            @RequestParam("serviceKey") String serviceKey,
            @RequestParam("id") int id){
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值