javaEEday12-springAOP-通知类型--2

This commit is contained in:
xx
2026-11-07 13:32:25 +08:00
parent 0e6163b9e0
commit 661f3646f8
3 changed files with 15 additions and 7 deletions
@@ -10,13 +10,19 @@ import org.springframework.stereotype.Component;
@Aspect @Aspect
public class MyAspect1 { public class MyAspect1 {
//相同切入点表达式的抽取(定义切入点)
@Pointcut("execution(* com.inmind.service.impl.DeptServiceImpl.*(..))")
public void pt(){};//此次可以定义为private私有和public公有,作用是是否开放切入点表达式
//前置通知 //前置通知
@Before("execution(* com.inmind.service.impl.DeptServiceImpl.*(..))") // @Before("execution(* com.inmind.service.impl.DeptServiceImpl.*(..))")
@Before("pt()")
public void before(){ public void before(){
log.info("before ..."); log.info("before ...");
} }
@Around("execution(* com.inmind.service.impl.DeptServiceImpl.*(..))") @Around("pt()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable { public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("around before ..."); log.info("around before ...");
@@ -27,17 +33,17 @@ public class MyAspect1 {
return result; return result;
} }
@After("execution(* com.inmind.service.impl.DeptServiceImpl.*(..))") @After("pt()")
public void after(){ public void after(){
log.info("after ..."); log.info("after ...");
} }
@AfterReturning("execution(* com.inmind.service.impl.DeptServiceImpl.*(..))") @AfterReturning("pt()")
public void afterReturning(){ public void afterReturning(){
log.info("afterReturning ..."); log.info("afterReturning ...");
} }
@AfterThrowing("execution(* com.inmind.service.impl.DeptServiceImpl.*(..))") @AfterThrowing("pt()")
public void afterThrowing(){ public void afterThrowing(){
log.info("afterThrowing ..."); log.info("afterThrowing ...");
} }
@@ -7,12 +7,13 @@ import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
//@Aspect//表示当前是AOP类,面向切面生效的 @Aspect//表示当前是AOP类,面向切面生效的
@Slf4j @Slf4j
public class TimeAspect { public class TimeAspect {
//模版方法 //模版方法
@Around("execution(* com.inmind.service.*.*(..))")//切入点表达式(表示com.inmind.service包下的所有类下,所有方法都生效) // @Around("execution(* com.inmind.service.*.*(..))")//切入点表达式(表示com.inmind.service包下的所有类下,所有方法都生效)
@Around("com.inmind.aop.MyAspect1.pt()")
public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable { public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
//1.记录开始时间 //1.记录开始时间
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
@@ -20,6 +20,7 @@ public class DeptServiceImpl implements DeptService {
//查询部门列表 //查询部门列表
@Override @Override
public List<Dept> deptList() { public List<Dept> deptList() {
//int i = 1/0;
//调用deptMapper获取部门列表数据 //调用deptMapper获取部门列表数据
List<Dept> deptList = deptMapper.deptList(); List<Dept> deptList = deptMapper.deptList();
return deptList; return deptList;