SpringBoot AOP使用笔记
今天小编就为大家分享一篇关于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;
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/sanpic/article/details/82800017
http://www.zzvips.com/article/176055.html
页:
[1]