tlias管理系统-springAOP的操作日志功能实现!!!
This commit is contained in:
@@ -8,7 +8,7 @@ import org.springframework.stereotype.Component;
|
|||||||
|
|
||||||
//切面类
|
//切面类
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Aspect
|
//@Aspect
|
||||||
@Component
|
@Component
|
||||||
public class MyAspect6 {
|
public class MyAspect6 {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.inmind.aop;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Before;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
//切面类
|
||||||
|
@Slf4j
|
||||||
|
//@Aspect
|
||||||
|
@Component
|
||||||
|
public class MyAspect7 {
|
||||||
|
|
||||||
|
|
||||||
|
/*需要匹配list和delete这2个方法*/
|
||||||
|
|
||||||
|
@Pointcut("@annotation(com.inmind.aop.MyLog)")
|
||||||
|
private void pt(){}
|
||||||
|
|
||||||
|
@Before("pt()")
|
||||||
|
public void before(){
|
||||||
|
log.info("MyAspect6 ... before ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package com.inmind.aop;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Before;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
//切面类
|
||||||
|
@Slf4j
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public class MyAspect8 {
|
||||||
|
|
||||||
|
@Pointcut("execution(* com.inmind.service.DeptService.*(..))")
|
||||||
|
private void pt(){}
|
||||||
|
|
||||||
|
@Before("pt()")
|
||||||
|
public void before(JoinPoint joinPoint){
|
||||||
|
log.info("MyAspect8 ... before ...");
|
||||||
|
//1. 获取 目标对象的类名 .
|
||||||
|
String className = joinPoint.getTarget().getClass().getName();
|
||||||
|
log.info("目标对象的类名:{}", className);
|
||||||
|
//2. 获取 目标方法的方法名 .
|
||||||
|
String methodName = joinPoint.getSignature().getName();
|
||||||
|
log.info("目标方法的方法:{}", methodName);
|
||||||
|
//3. 获取 目标方法运行时传入的参数 .
|
||||||
|
Object[] args = joinPoint.getArgs();
|
||||||
|
log.info("目标方法运行时传入的参数:{}", Arrays.toString(args));
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Around("pt()")
|
||||||
|
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
|
log.info("MyAspect8 around before ...");
|
||||||
|
|
||||||
|
//1. 获取 目标对象的类名 .
|
||||||
|
String className = joinPoint.getTarget().getClass().getName();
|
||||||
|
log.info("目标对象的类名:{}", className);
|
||||||
|
|
||||||
|
//2. 获取 目标方法的方法名 .
|
||||||
|
String methodName = joinPoint.getSignature().getName();
|
||||||
|
log.info("目标方法的方法:{}", methodName);
|
||||||
|
|
||||||
|
//3. 获取 目标方法运行时传入的参数 .
|
||||||
|
Object[] args = joinPoint.getArgs();
|
||||||
|
log.info("目标方法运行时传入的参数:{}", Arrays.toString(args));
|
||||||
|
|
||||||
|
//4. 放行 目标方法执行并返回结果 .其他4个通知是获取不到返回值的,JoinPoint没有提供该方法,
|
||||||
|
Object result = joinPoint.proceed();
|
||||||
|
|
||||||
|
//5. 获取 目标方法运行的返回值 .
|
||||||
|
log.info("目标方法运行的返回值:{}", result);
|
||||||
|
|
||||||
|
log.info("MyAspect8 around after ...");
|
||||||
|
|
||||||
|
return result;//注意:null默认是不返回,导致有返回值的方法失效,必须将原始方法执行结果进行return
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.inmind.aop;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)//设置注解的生命周期为运行时有效
|
||||||
|
@Target(ElementType.METHOD)//设置作用域:该注解只能作用于方法上
|
||||||
|
public @interface MyLog {
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.inmind.service.impl;
|
package com.inmind.service.impl;
|
||||||
|
|
||||||
|
import com.inmind.aop.MyLog;
|
||||||
import com.inmind.mapper.DeptMapper;
|
import com.inmind.mapper.DeptMapper;
|
||||||
import com.inmind.pojo.Dept;
|
import com.inmind.pojo.Dept;
|
||||||
import com.inmind.service.DeptService;
|
import com.inmind.service.DeptService;
|
||||||
@@ -17,12 +18,14 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private DeptMapper deptMapper;
|
private DeptMapper deptMapper;
|
||||||
|
|
||||||
|
@MyLog
|
||||||
@Override
|
@Override
|
||||||
public List<Dept> list() {
|
public List<Dept> list() {
|
||||||
List<Dept> deptList = deptMapper.list();
|
List<Dept> deptList = deptMapper.list();
|
||||||
return deptList;
|
return deptList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MyLog
|
||||||
@Override
|
@Override
|
||||||
public void delete(Integer id) {
|
public void delete(Integer id) {
|
||||||
//1. 删除部门
|
//1. 删除部门
|
||||||
|
|||||||
@@ -82,6 +82,11 @@
|
|||||||
<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>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
|||||||
12
tlias-web-management/src/main/java/com/inmind/anno/Log.java
Normal file
12
tlias-web-management/src/main/java/com/inmind/anno/Log.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package com.inmind.anno;
|
||||||
|
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
public @interface Log {
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package com.inmind.aop;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.inmind.mapper.OperateLogMapper;
|
||||||
|
import com.inmind.pojo.OperateLog;
|
||||||
|
import com.inmind.utils.JwtUtils;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.weaver.ast.Var;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
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 {
|
||||||
|
//1.操作人ID,就在当前请求是携带的令牌JWT中
|
||||||
|
String jwt = request.getHeader("token");
|
||||||
|
Claims claims = JwtUtils.parseJWT(jwt);
|
||||||
|
Integer operateUser = (Integer) claims.get("id");
|
||||||
|
//2.操作时间
|
||||||
|
LocalDateTime operateTime = LocalDateTime.now();
|
||||||
|
|
||||||
|
//3.执行方法全类名
|
||||||
|
String className = joinPoint.getTarget().getClass().getName();
|
||||||
|
|
||||||
|
//4.执行方法方法名
|
||||||
|
String methodName = joinPoint.getSignature().getName();
|
||||||
|
|
||||||
|
//5.执行方法运行时参数
|
||||||
|
Object[] args = joinPoint.getArgs();
|
||||||
|
String methodParams = Arrays.toString(args);
|
||||||
|
|
||||||
|
long begin = System.currentTimeMillis();
|
||||||
|
//6.执行方法返回值
|
||||||
|
Object result = joinPoint.proceed();
|
||||||
|
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
|
||||||
|
String returnValue = JSONObject.toJSONString(result);
|
||||||
|
|
||||||
|
//7.执行时长
|
||||||
|
Long costTime = end - begin;
|
||||||
|
OperateLog operateLog = new OperateLog(null,operateUser, operateTime, className, methodName, methodParams, returnValue, costTime);
|
||||||
|
//保存到数据库中,Mapper
|
||||||
|
operateLogMapper.insert(operateLog);
|
||||||
|
|
||||||
|
log.info("AOP记录操作日志:{}",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.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;
|
||||||
@@ -35,6 +36,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 delete(Integer id) throws Exception {
|
public void delete(Integer id) throws Exception {
|
||||||
try {
|
try {
|
||||||
//不仅删除对应部门数据
|
//不仅删除对应部门数据
|
||||||
@@ -42,9 +44,9 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
//进行一些业务逻辑代码,假设业务出现了异常
|
//进行一些业务逻辑代码,假设业务出现了异常
|
||||||
// int a = 1/0;
|
// int a = 1/0;
|
||||||
//抛出一个编译时异常
|
//抛出一个编译时异常
|
||||||
if (true) {
|
/* if (true) {
|
||||||
throw new Exception("出错了...");
|
throw new Exception("出错了...");
|
||||||
}
|
}*/
|
||||||
//还要删除该部门下的员工数据(根据部门ID删除相关的员工)
|
//还要删除该部门下的员工数据(根据部门ID删除相关的员工)
|
||||||
empMapper.deleteByDeptId(id);
|
empMapper.deleteByDeptId(id);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -60,6 +62,7 @@ public class DeptServiceImpl implements DeptService {
|
|||||||
|
|
||||||
//新增部门
|
//新增部门
|
||||||
@Override
|
@Override
|
||||||
|
@Log
|
||||||
public void add(Dept dept) {
|
public void add(Dept dept) {
|
||||||
//补充基础属性,创建时间和更新时间
|
//补充基础属性,创建时间和更新时间
|
||||||
dept.setCreateTime(LocalDateTime.now());
|
dept.setCreateTime(LocalDateTime.now());
|
||||||
@@ -75,6 +78,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