tlias管理系统-springAOP的操作日志功能实现!!!

This commit is contained in:
2025-10-28 16:21:28 +08:00
parent 13d23747f8
commit 438290ad52
11 changed files with 233 additions and 3 deletions

View File

@@ -8,7 +8,7 @@ import org.springframework.stereotype.Component;
//切面类
@Slf4j
@Aspect
//@Aspect
@Component
public class MyAspect6 {

View File

@@ -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 ...");
}
}

View File

@@ -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
}
}

View File

@@ -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 {
}

View File

@@ -1,5 +1,6 @@
package com.inmind.service.impl;
import com.inmind.aop.MyLog;
import com.inmind.mapper.DeptMapper;
import com.inmind.pojo.Dept;
import com.inmind.service.DeptService;
@@ -17,12 +18,14 @@ public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@MyLog
@Override
public List<Dept> list() {
List<Dept> deptList = deptMapper.list();
return deptList;
}
@MyLog
@Override
public void delete(Integer id) {
//1. 删除部门