SpringBoot AOP使用笔记

1. 启用AOP

a. 在类上添加@Aspect注解

b. 注入该类, 可以使用@Component进行注入到Spring容器中

2. 通过PointCut对象创建切入点

a. 在某个方法使用类似下面的方法进行注入

@Pointcut("execution(* com.sguess.service.IAOPService.*(..))")

private void pointcut() {

}

i. 其中,execution表达式为

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)  

ii. 注意, pointcut()方法名是后面切入的时候需要使用的

iii. 方法内可以什么也不写, 写了也调不到

iv. 也可以创建多个PointCut,例如再创建一个

@Pointcut("execution(* com.sguess.service.IAOPService.fun1(..))")

private void pointcut2() {

}

这个的方法名就位pointcut2, 方法名不一样.  

b. 创建After方法,Before方法

@After(value = "pointcut()")

public void doAfter() {

System.out.println("Do AOP After function 01");

}

i. After方法是指, 在配置了的切入点被执行后, 执行该方法. 

ii. value中的pointcut() 是我们前面在创建@Pointcut中的方法名. 也就是说,是通过方法名和切入点进行匹配的. 

iii. 这个的方法名可以随便起. 

iv. Before方法同理

c. 带Return的After方法,

@AfterReturning(returning = "str", pointcut = "pointcut()")

public void doAfterReturning(String str) throws Exception {

System.out.println("Return value is: " + str);

}

i. AfterReturn是指在被切入的方法执行后, 获取其返回值, 再执行该方法. 注意关键, 这个可以进行操作返回值. 

ii. returning = "str",是指, 假设切入方法的返回的值变量名为str

doAfterReturning(String str)方法的参数变量名必须和和returning保持一致, 这里也叫作str. 然后才能在方法体中使用.

iii. pointcut = "pointcut()"同样是指前面声明的pointcut方法名

3. 通过注解, 使用切入点

a. 监听方法参数

@Before("execution(public int com.sguess.service.*(int, int))")

public void beforMethod(JoinPoint point) {

String methodName = point.getSignature().getName();

List<Object> args = Arrays.asList(point.getArgs());

System.out.println("Before FunctionName:" + methodName + ",ParameterName:" + args);

}

@After("execution(public int com.sguess.service.*(int, int))")

public void afterMethod(JoinPoint point) {

String methodName = point.getSignature().getName();

List<Object> args = Arrays.asList(point.getArgs());

System.out.println("After FunctionName:" + methodName + ",ParameterName:" + args);

}

4. 执行顺序:

a.Around的方法优先于Before/After执行,After优先于AfterReturn. 

i. 代码

@Before("execution(public int com.sguess.service.*.*(int, int))")

public void beforMethod(JoinPoint point) {

System.out.println("Before function");

}

@After("execution(public int com.sguess.service.*.*(int, int))")

public void afterMethod(JoinPoint point) {

System.out.println("After function");

}

@AfterReturning("execution(public int com.sguess.service.*.*(int, int))")

public void afterReturnMethod(JoinPoint point) {

System.out.println("AfterReturn function");

}

@AfterThrowing(value = "execution(public int com.sguess.service.*.*(int, int))", throwing = "e")

public void afterReturningThrowing(JoinPoint point, Exception e) {

System.out.println("AfterReturnThrowing function");

}

@Around("execution(public int com.sguess.service.*.*(int, int))")

public Object aroundMethod(ProceedingJoinPoint pdj) {

System.out.println("Start AroundFunction");

Object result = null;

try {

System.out.println("Around process start");

result = pdj.proceed();

System.out.println("Around process end");

} catch (Throwable e) {

System.out.println("Around process exception");

}

System.out.println("After Around process");

return result;

}

}

执行结果:

Start AroundFunction

Around process start

Before function

Around process end

After Around process

After function

AfterReturn function

5.小结:

@AfterReturning(returning = "str", pointcut = "pointcut()")

public void doAfterReturning(String str) throws Exception {

System.out.println("Return value is: " + str);

}

@Before("execution(public int com.sguess.service.*.*(int, int))")

public void beforMethod(JoinPoint point) {

String methodName = point.getSignature().getName();

List<Object> args = Arrays.asList(point.getArgs());

System.out.println("Before FunctionName:" + methodName + ",ParameterName:" + args);

}

@After("execution(public int com.sguess.service.*.*(int, int))")

public void afterMethod(JoinPoint point) {

String methodName = point.getSignature().getName();

List<Object> args = Arrays.asList(point.getArgs());

System.out.println("After FunctionName:" + methodName + ",ParameterName:" + args);

}

@AfterThrowing(value = "execution(public int com.sguess.service.*.*(int, int))", throwing = "e")

public void afterReturningThrowing(JoinPoint point, Exception e) {

String methodName = point.getSignature().getName();

List<Object> args = Arrays.asList(point.getArgs());

System.out.println("AfterReturningThrowing FunctionName:" + methodName + ",ParameterName:" + args + ",Exception:" + e);

}

@Around("execution(public int com.sguess.service.*.*(int, int))")

public Object aroundMethod(ProceedingJoinPoint pdj) {

System.out.println("Start AroundFunction");

Object result = null;

try {

System.out.println("Around process start");

result = pdj.proceed();

System.out.println("Around process end");

} catch (Throwable e) {

System.out.println("Around process exception");

}

System.out.println("After Around process");

return result;

}

总结

以上是 SpringBoot AOP使用笔记 的全部内容, 来源链接: utcz.com/z/351979.html

回到顶部