Spring如何基于aop实现操作日志功能

时间:2021-05-20

1. 在pom中添加所需依赖

创建一个springboot工程,添加所需要的依赖,持久化用的是mybatis

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--springboot aop依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <!--mysql连接--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> <scope>runtime</scope> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>

2. 创建日志实体类

import lombok.Data;import java.io.Serializable;@Datapublic class AdminLog implements Serializable { private static final long serialVersionUID = -291495801959706565L; private Integer id; //日志记录id private Integer userId;//操作人id private String userName;//操作人name private String loginip;//登录ip private int type; private String url; private String operation; private String createtime; private String remark;}

3. 自定义log注解

import java.lang.annotation.*;/** * 自定义日志注解 */@Target(ElementType.METHOD) //注解防止位置@Retention(RetentionPolicy.RUNTIME)//运行时可见@Documented //生成文档public @interface MyLog { String operation() default ""; int type();}

4. 创建aop切面处理类

import cn.***.springaopdemo.anno.MyLog;import cn.***.springaopdemo.dao.MyLogMapper;import cn.***.springaopdemo.pojo.Admin;import cn.***.springaopdemo.pojo.AdminLog;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;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;/** * 切面处理类 */@Aspect@Componentpublic class SysLogAspect { /** * 使用log4j2把一些信息打印在控制台上面 */ private static final Logger log = LogManager.getLogger(SysLogAspect.class); @Autowired private MyLogMapper myLogMapper; //定义切点 @Pointcut //在注解的位置切入代码 @Pointcut("@annotation(cn.***.springaopdemo.anno.MyLog)") public void logPointCut() { } //切面 配置为前置通知 @Before("logPointCut()") public void saveOperation(JoinPoint joinPoint) { log.info("---------------接口日志记录---------------"); //创建一个日志对象 AdminLog adminLog = new AdminLog(); //获取切面织处入点的方法 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); //获取切入点所在的方法 Method method = signature.getMethod(); //获取操作日志的属性值 MyLog myLog = method.getAnnotation(MyLog.class); if (myLog != null) { //操作事件 String operation = myLog.operation(); adminLog.setOperation(operation); //日志类型 int type = myLog.type(); adminLog.setType(type); log.info("operation=" + operation + ",type=" + type); } //获取url HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String requestURL = request.getRequestURI().toString(); adminLog.setUrl(requestURL); //获取客户端ip String ip = request.getRemoteAddr(); adminLog.setLoginip(ip); //获取操作人账号、姓名(需要提前将用户信息保存到Session) Admin admin = (Admin) request.getSession().getAttribute("admin"); if (admin != null) { Integer id = admin.getId(); String name = admin.getName(); adminLog.setUserId(id); adminLog.setUserName(name); } log.info("url=" + requestURL + ",ip=" + ip); //调用service保存Operation实体类到数据库 //可以在这设置id,因为是测试,这里就使用的是数据库的自增id myLogMapper.insertLog(adminLog); }}

5. mapper层把日志数据存储到mysql数据库中

mapper接口

import cn.***.springaopdemo.pojo.AdminLog;import java.util.List;public interface MyLogMapper { void insertLog(AdminLog adminLog);}

mapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.***.springaopdemo.dao.MyLogMapper"> <insert id="insertLog" parameterType="cn.***.springaopdemo.pojo.AdminLog"> INSERT INTO admin_log (user_id,user_name,loginip,type,url,operation,createtime,remark) VALUES (#{userId},#{userName},#{loginip},#{type},#{url},#{operation},now(),#{remark}) </insert></mapper>

6. 测试

先直接登录用户,因为是测试,直接从数据库中获取后登录,把admin存储到session中

import cn.***.springaopdemo.pojo.Admin;import cn.***.springaopdemo.service.IAdminService;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;import java.util.List;@RestController@RequestMapping("/admin")public class AdminController { private static final Logger log = LogManager.getLogger(AdminController.class); //中间service层可以省略,直接通过mapper接口操作数据即可 @Autowired private IAdminService adminService; @RequestMapping("/login") public Admin login(HttpServletRequest request) { List<Admin> adminList = adminService.findAllAdmin(); Admin admin = adminList.get(0); request.getSession().setAttribute("admin",admin ); return admin; }}

在浏览器中输入localhost:8080/admin/login,可以看到登录的admin

进行插入和查询操作,插入数据直接通过后台提供

import cn.***.springaopdemo.anno.MyLog;import cn.***.springaopdemo.pojo.Type;import cn.***.springaopdemo.service.ITypeService;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;import java.util.List;@RestController@RequestMapping("/type")public class TypeController { private static final Logger log = LogManager.getLogger(TypeController.class); @Autowired private ITypeService typeService; @MyLog(operation = "增加书籍类型", type = 2) @RequestMapping("/add") public void insertType() { List<Type> typeList = new ArrayList<>(); Type type = new Type(); type.setName("自然科学"); typeList.add(type); typeService.addTypeList(typeList); log.info("添加书籍类型" + type.getName()); } @MyLog(operation = "查询所有书籍类型", type = 1) @RequestMapping("/findAll") public List<Type> findAllType() { List<Type> typeList = typeService.findAllType(); log.info("查询所有书籍类型"); return typeList; }}

在浏览器中输入localhost:8080/type/add,后台日志打印记录

再输入查询请求localhost:8080/type/findAll,获得查询出的分类

查看数据库是否添加成功

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章