tlias管理系统-springAOP的通知类型介绍&切入点表达式的共用抽取

This commit is contained in:
2025-10-28 11:33:54 +08:00
parent 07301a7b59
commit bdbdfd03ab
3 changed files with 53 additions and 5 deletions

View File

@@ -0,0 +1,50 @@
package com.inmind.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@Aspect //切面类
public class MyAspect1 {
//定义了一个可以通用的切入点表达式
@Pointcut("execution(* com.inmind.service.impl.DeptServiceImpl.*(..))")
public void pt(){};
// @Before("execution(* com.inmind.service.impl.DeptServiceImpl.*(..))")
@Before("pt()")
public void before(){
log.info("before ...");
}
// @Around("execution(* com.inmind.service.impl.DeptServiceImpl.*(..))")
@Around("pt()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("around before ...");
//调用目标对象的原始方法执行
Object result = joinPoint.proceed();
log.info("around after ...");
return result;//环绕通知一定要返回原始方法的执行结果!!!!
}
@After("pt()")
public void after(){
log.info("after ...");
}
@AfterReturning("pt()")
public void afterReturning(){
log.info("afterReturning ...");
}
@AfterThrowing("pt()")
public void afterThrowing(){
log.info("afterThrowing ...");
}
}

View File

@@ -11,7 +11,8 @@ import org.springframework.stereotype.Component;
@Slf4j
public class TimeAspect {
@Around("execution(* com.inmind.service.*.*(..))")//表示AOP作用于com.inmind.service下的所有的类中所有的方法参数是0或多个
// @Around("execution(* com.inmind.service.*.*(..))")//表示AOP作用于com.inmind.service下的所有的类中所有的方法参数是0或多个
@Around("com.inmind.aop.MyAspect1.pt()")
public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
//1.记录开始时间
long begin = System.currentTimeMillis();

View File

@@ -1,6 +1,5 @@
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;
@@ -19,14 +18,12 @@ public class DeptServiceImpl implements DeptService {
private DeptMapper deptMapper;
@Override
@MyLog
public List<Dept> list() {
List<Dept> deptList = deptMapper.list();
return deptList;
}
@Override
@MyLog
public void delete(Integer id) {
//1. 删除部门
deptMapper.delete(id);
@@ -41,7 +38,7 @@ public class DeptServiceImpl implements DeptService {
@Override
public Dept getById(Integer id) {
// int i = 1/0;
int i = 1/0;
return deptMapper.getById(id);
}