Day39项目saas-export项目-系统日志AOP**

Aop记录日志

  • (1)什么是AOP
    AOP ,Aspect Oritentd Programing 面向切面编程
    本质就是在不改变代码的基础上生成动态代理类(新类)
  • (2)应用场景
    》日志记录:
    》事务管理
    》权限管理
    》性能数据记录
    在这里插入图片描述

Aop记录日志

  • (1)实现步骤
    编写springmvc.xml, 开启Aop自动代理
    编写日志切面类(@Aspect)
    测试Aop,自动记录日志。
  • (2)实现
    LogAspect

1. 编写日志切面类(@Aspect)

//第一步:编写切面类
@Aspect  //配置了aop逻辑
@Component //非Controller,Service Repository
public class LogAspect {
    
    


    public LogAspect() {
    
    
        System.out.println("LogAspect 无参构造方法执行");
    }

    //要对所有的Controller的方法进行配置
    //指定包名 controller 下以及它的所有子包
    //
    @Around(value = "execution(* com.dxy.web.controller..*.*Controller.*(..))")
    public Object writeLog(ProceedingJoinPoint jp) {
    
    //切点
        // jp表示Controller中的任意方法 toList toAdd toUpdate add update delete
        //逻辑
        Object result = null;//返回一个表示页面的字符串,也可通是json数据
        try {
    
    
            //前置
            result = jp.proceed();//执行
            //后置
            //保存日志
            saveSysLog(jp);
        } catch (Throwable e) {
    
    
            //异常
        } finally {
    
    
            //最终
        }
        return result;
    }

    @Autowired
    ISysLogService iSysLogService;

    //登录成功之后session中是保存一个user对象的
    @Autowired
    HttpSession session;

    //request对象可以直接获取对方浏览器的IP
    @Autowired
    HttpServletRequest request;
    private void saveSysLog(ProceedingJoinPoint jp) {
    
    
        //将一个表单数据保存在javaBean中,再将javaBean存到数据库
        SysLog sysLog = new SysLog();

        User user = (User) session.getAttribute("loginUser");
        if(user !=null){
    
    
            //设置登录用户信息
            sysLog.setUserName(user.getUserName());
            //设置企业信息
            sysLog.setCompanyId(user.getCompanyId());
            sysLog.setCompanyName(user.getCompanyName());
        }

        //IP地址  request.getRemoteAddr()获取请求中的ip地址
        sysLog.setIp(request.getRemoteAddr());
        //设置记录时间
        sysLog.setTime(new Date());

        //执行的方法名称  jp.getSignature() 当前执行的方法 toList
        sysLog.setMethod(jp.getSignature().getName());

        //执行的类名称 jp.getTarget()目标对象
        sysLog.setAction(jp.getTarget().getClass().getName());

        iSysLogService.saveSysLog(sysLog);
    }
}

2.编写springmvc.xml, 开启Aop自动代理

<!--开启AOP切面注解: 整个项目扫描有没有@Aspect注解-->
    <aop:aspectj-autoproxy/>

如果标签报错,需要添加aop的标签声明

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

3.在打印处调用保存

上面有

 l.info("切面:writeLog");
            saveSysLog(jp);
    @Autowired
    ISysLogService iSysLogService;

    //登录成功之后session中是保存一个user对象的
    @Autowired
    HttpSession session;

    //request对象可以直接获取对方浏览器的IP
    @Autowired
    HttpServletRequest request;
    private void saveSysLog(ProceedingJoinPoint jp) {
    
    
        //将一个表单数据保存在javaBean中,再将javaBean存到数据库
        SysLog sysLog = new SysLog();

        User user = (User) session.getAttribute("loginUser");
        if(user !=null){
    
    
            //设置登录用户信息
            sysLog.setUserName(user.getUserName());
            //设置企业信息
            sysLog.setCompanyId(user.getCompanyId());
            sysLog.setCompanyName(user.getCompanyName());
        }

        //IP地址  request.getLocalAddr()获取请求中的ip地址
        sysLog.setIp(request.getLocalAddr());
        //设置记录时间
        sysLog.setTime(new Date());

        //执行的方法名称  jp.getSignature() 当前执行的方法 toList
        sysLog.setMethod(jp.getSignature().getName());

        //执行的类名称 jp.getTarget()目标对象
        sysLog.setAction(jp.getTarget().getClass().getName());

        l.info("saveSysLog sysLog "+sysLog);
        iSysLogService.saveSysLog(sysLog);
    }

猜你喜欢

转载自blog.csdn.net/qq_40711092/article/details/109518867
今日推荐