0%

aop

文章字数:131,阅读全文大约需要1分钟

面向切面的编程,底层是动态代理

分类

  • 前置通知@Before("execution(public int com.xx.yy.*(...))")
  • 后置通知@After
  • 返回后通知@AfterReturning
  • 出现异常后运行@AfterThrowing
  • 环绕通知(动态代理)@Around手动joinPoint.procced()手动执行业务方法

pointcut

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 原生Spring需要开启EnableAspectJAutoProxy, SpringBoot默认开了
// 需要注入到Spring
@Aspect
public class xxx

/** 指定切面,这样就不用每个方法都指定作用于那个地方 */
@Pointcut("execution(public int com.xx.yy.*(...))")
public void pointCut(){};

@Before("pointCut()")
public void logStart(){
sout("xxx");
}

@Around("pointCut()")
public Object Around(ProceedingJoinPoint joinPoint) throws Throwable{
sout("前置");// 比Before还早
// 执行业务方法,类似反射调用方法
Object res = joinPoint.proceed();
sout("后置");
return res;
}