javaEEday12-AOP案例-记录操作日志-实现
This commit is contained in:
@@ -106,6 +106,12 @@
|
|||||||
<artifactId>fastjson</artifactId>
|
<artifactId>fastjson</artifactId>
|
||||||
<version>2.0.53</version>
|
<version>2.0.53</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--AOP起步依赖-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.inmind.anno;
|
||||||
|
|
||||||
|
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)
|
||||||
|
public @interface Log {
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package com.inmind.aop;
|
||||||
|
|
||||||
|
import com.inmind.mapper.OperateLogMapper;
|
||||||
|
import com.inmind.pojo.OperateLog;
|
||||||
|
import com.inmind.utils.JwtUtils;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class LogAspect {
|
||||||
|
//之前在控制器中,直接定义HttpServletRequest参数,DispatcherServlet就会直接传递一个请求对象给我们,这里我们是从spring容器中获取请求对象
|
||||||
|
@Autowired
|
||||||
|
private HttpServletRequest request;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OperateLogMapper operateLogMapper;
|
||||||
|
|
||||||
|
//切面
|
||||||
|
@Around("@annotation(com.inmind.anno.Log)")
|
||||||
|
public Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
|
//0.从请求中获取令牌的登录员的ID
|
||||||
|
String jwt = request.getHeader("token");
|
||||||
|
Claims claims = JwtUtils.parseJWT(jwt);
|
||||||
|
Integer operateUser = (Integer) claims.get("id");
|
||||||
|
LocalDateTime operateTime = LocalDateTime.now();
|
||||||
|
|
||||||
|
//1. 获取 目标对象的类名 .
|
||||||
|
String className = joinPoint.getTarget().getClass().getName();
|
||||||
|
log.info("around目标对象的类名:{}", className);
|
||||||
|
//2. 获取 目标方法的方法名 .
|
||||||
|
String methodName = joinPoint.getSignature().getName();
|
||||||
|
log.info("around目标方法的方法名: {}",methodName);
|
||||||
|
|
||||||
|
//3. 获取 目标方法运行时传入的参数 .
|
||||||
|
Object[] args = joinPoint.getArgs();
|
||||||
|
log.info("around目标方法运行时传入的参数: {}", Arrays.toString(args));
|
||||||
|
//4. 放行 目标方法执行并返回结果 .其他4个通知是获取不到返回值的,JoinPoint没有提供该方法,
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
Object result = joinPoint.proceed();
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
long costTime = end - start;
|
||||||
|
//5. 获取 目标方法运行的返回值 .
|
||||||
|
log.info("around目标方法运行的返回值: {}",result);
|
||||||
|
|
||||||
|
//记录操作日志
|
||||||
|
OperateLog operateLog = new OperateLog(null, operateUser, operateTime, className, methodName
|
||||||
|
, Arrays.toString(args), result== null?"null":result.toString(), costTime);
|
||||||
|
operateLogMapper.insert(operateLog);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.inmind.mapper;
|
||||||
|
|
||||||
|
import com.inmind.pojo.OperateLog;
|
||||||
|
import org.apache.ibatis.annotations.Insert;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface OperateLogMapper {
|
||||||
|
|
||||||
|
//插入日志数据
|
||||||
|
@Insert("insert into operate_log (operate_user, operate_time, class_name, method_name, method_params, return_value, cost_time) " +
|
||||||
|
"values (#{operateUser}, #{operateTime}, #{className}, #{methodName}, #{methodParams}, #{returnValue}, #{costTime});")
|
||||||
|
public void insert(OperateLog log);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.inmind.pojo;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class OperateLog {
|
||||||
|
private Integer id; //ID
|
||||||
|
private Integer operateUser; //操作人ID
|
||||||
|
private LocalDateTime operateTime; //操作时间
|
||||||
|
private String className; //操作类名
|
||||||
|
private String methodName; //操作方法名
|
||||||
|
private String methodParams; //操作方法参数
|
||||||
|
private String returnValue; //操作方法返回值
|
||||||
|
private Long costTime; //操作耗时
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.inmind.service;
|
package com.inmind.service;
|
||||||
|
|
||||||
|
import com.inmind.anno.Log;
|
||||||
import com.inmind.pojo.Dept;
|
import com.inmind.pojo.Dept;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -16,12 +17,14 @@ public interface DeptService {
|
|||||||
* 删除部门
|
* 删除部门
|
||||||
* @param id 部门id
|
* @param id 部门id
|
||||||
*/
|
*/
|
||||||
|
@Log
|
||||||
void deleteDept(Integer id) throws Exception;
|
void deleteDept(Integer id) throws Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增部门
|
* 新增部门
|
||||||
* @param dept 部门信息
|
* @param dept 部门信息
|
||||||
*/
|
*/
|
||||||
|
@Log
|
||||||
void addDept(Dept dept);
|
void addDept(Dept dept);
|
||||||
|
|
||||||
|
|
||||||
@@ -29,5 +32,6 @@ public interface DeptService {
|
|||||||
Dept getDeptById(Integer id);
|
Dept getDeptById(Integer id);
|
||||||
|
|
||||||
//根据id更新部门
|
//根据id更新部门
|
||||||
|
@Log
|
||||||
void update(Dept dept);
|
void update(Dept dept);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.inmind.service.impl;
|
package com.inmind.service.impl;
|
||||||
|
|
||||||
|
import com.inmind.anno.Log;
|
||||||
import com.inmind.mapper.DeptMapper;
|
import com.inmind.mapper.DeptMapper;
|
||||||
import com.inmind.mapper.EmpMapper;
|
import com.inmind.mapper.EmpMapper;
|
||||||
import com.inmind.pojo.Dept;
|
import com.inmind.pojo.Dept;
|
||||||
@@ -36,6 +37,7 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
//根据id删除部门
|
//根据id删除部门
|
||||||
@Transactional(rollbackFor = Exception.class)//让当前方法,处于事务管理下(在方法调用前,执行start transaction)
|
@Transactional(rollbackFor = Exception.class)//让当前方法,处于事务管理下(在方法调用前,执行start transaction)
|
||||||
@Override
|
@Override
|
||||||
|
@Log
|
||||||
public void deleteDept(Integer id) throws Exception {
|
public void deleteDept(Integer id) throws Exception {
|
||||||
try {
|
try {
|
||||||
//不仅删除对应部门数据
|
//不仅删除对应部门数据
|
||||||
@@ -61,6 +63,7 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
|
|
||||||
//新增部门
|
//新增部门
|
||||||
@Override
|
@Override
|
||||||
|
@Log
|
||||||
public void addDept(Dept dept) {
|
public void addDept(Dept dept) {
|
||||||
//补充基础属性,创建时间和更新时间
|
//补充基础属性,创建时间和更新时间
|
||||||
dept.setCreateTime(LocalDateTime.now());
|
dept.setCreateTime(LocalDateTime.now());
|
||||||
@@ -76,6 +79,7 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Log
|
||||||
public void update(Dept dept) {
|
public void update(Dept dept) {
|
||||||
//补充更新时间数据
|
//补充更新时间数据
|
||||||
dept.setUpdateTime(LocalDateTime.now());
|
dept.setUpdateTime(LocalDateTime.now());
|
||||||
|
|||||||
Reference in New Issue
Block a user