一、 引入依赖
<dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-aop</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> </dependencies>
二、切入点表达式详解
切入点表达式用于定义在哪些连接点应用增强,可以组合并使用execution、within 、 @annotation 、@within 、@target、args、bean这几种表达式。
本文只介绍最常用的execution表达式。
execution表达式语法:execution([修饰符] 返回类型 [声明类型].方法名(参数) [异常])
* :匹配任意数量的字符(除了包分隔符.) .. :匹配任意数量的字符(包括包分隔符.)或任意数量的参数 + :匹配指定类型的子类型
@aspect
@component
public class testaop {
// 1.匹配特定类的特定方法
@pointcut("execution(public void com.example.demo.controller.aoptestcontroller.testaop(..))")
public void testaopmethod1() {
}
// 2.匹配特定类的特定方法,访问修饰符可以省略
@pointcut("execution(void com.example.demo.controller.aoptestcontroller.testaop(..))")
public void testaopmethod2() {
}
// 3.匹配特定类的特定方法,返回值可以使用通配符 * 表示任意返回值
@pointcut("execution(* com.example.demo.controller.aoptestcontroller.testaop(..))")
public void testaopmethod3() {
}
// 4.包名可以使用通配符表示任意包,但是有几级包就需要写几个 *
@pointcut("execution(* *.*.*.*.aoptestcontroller.testaop(..))")
public void testaopmethod4() {
}
// 5.包名可以使用 .. 表示当前包及其子包
@pointcut("execution(* *..aoptestcontroller.testaop(..))")
public void testaopmethod5() {
}
// 6.类名和方法名都可以使用 * 来实现通配
@pointcut("execution(* *..*.*(..))")
public void testaopmethod6() {
}
// 7. 匹配特定接口的所有实现方法
@pointcut("execution(* com.example.demo.controller.aoptestcontroller+.*(..))")
public void testaopmethod7() {
}
}三、案例实战
1. aop基本使用
① 待增强的业务代码:
@restcontroller
public class aoptestcontroller {
@getmapping("/testaop")
public object testaop() {
return "testaop";
}
@getmapping("/testaopexception")
public object testaopexception() {
return 1 / 0;
}
}② aop切面:
@aspect
@component
public class aoptestaspect {
@pointcut("execution(public * com.example.demo.controller.aoptestcontroller.testaop(..)) " +
"|| execution(public * com.example.demo.controller.aoptestcontroller.testaopexception(..))")
public void log() {
}
@before("log()")
public void debefore(joinpoint joinpoint) {
servletrequestattributes attributes = (servletrequestattributes) requestcontextholder.getrequestattributes();
httpservletrequest request = attributes.getrequest();
system.out.println("url : " + request.getrequesturl().tostring());
system.out.println("http_method : " + request.getmethod());
system.out.println("class_method : " + joinpoint);
system.out.println("args : " + arrays.tostring(joinpoint.getargs()));
system.out.println("前置通知:方法执行前执行...");
}
//返回通知
@afterreturning(returning = "result", pointcut = "log()")
public void doafterreturning(object result) {
system.out.println("返回通知:方法成功执行并返回后执行...");
system.out.println("方法的返回值: " + result);
}
//异常通知
@afterthrowing(throwing = "exception", pointcut = "log()")
public void dothrowing(joinpoint joinpoint, exception exception) {
system.out.println("异常通知:方法异常时执行...");
system.out.println("产生异常的方法:" + joinpoint);
system.out.println("异常种类:" + exception);
}
//后置通知
@after("log()")
public void doafter(joinpoint joinpoint) {
system.out.println("后置通知:最后且一定执行...");
}
}③ 控制台打印:


2. aop结合自定义注解
① 自定义注解:
@target({elementtype.method, elementtype.type})
@retention(retentionpolicy.runtime)
public @interface logannotation {
string desc() default " ";
}② 待增强的业务代码:
@restcontroller
public class aoptestcontroller {
@getmapping("testaopannotation")
@logannotation(desc = "测试注解类型:testaopannotation")
public object testaopannotation() {
return "testaopannotation";
}
}③ aop切面:
@aspect
@component
public class aopannotationtestaspect {
@pointcut(value = "@annotation(com.example.demo.annotation.logannotation)")
public void logaction() {
}
@before("logaction() && @annotation(logannotation)")
public void dobefore(joinpoint joinpoint, logannotation logannotation) {
system.out.println("前置通知:方法执行前执行...");
system.out.println("dobefore注解的值:" + logannotation.desc());
servletrequestattributes attributes = (servletrequestattributes) requestcontextholder.getrequestattributes();
system.out.println("url: " + attributes.getrequest().getrequesturl().tostring());
}
@afterreturning(returning = "result", pointcut = "logaction() && @annotation(logannotation)")
public void doafterreturning(object result, logannotation logannotation) {
system.out.println("doafterreturning注解的值:" + logannotation.desc());
system.out.println("返回通知:方法成功执行并返回后执行...");
system.out.println("方法的返回值: " + result);
}
@afterthrowing(throwing = "exception", pointcut = "logaction() && @annotation(logannotation)")
public void dothrows(joinpoint joinpoint, exception exception, logannotation logannotation) {
system.out.println("dothrows注解的值" + logannotation.desc());
system.out.println("异常通知:方法异常时执行...");
system.out.println("产生异常的方法:" + joinpoint);
system.out.println("异常种类:" + exception);
}
@after("logaction() && @annotation(logannotation)")
public void doafter(joinpoint joinpoint, logannotation logannotation) {
system.out.println("doafter注解的值:" + logannotation.desc());
system.out.println("后置通知:最后且一定执行...");
}
}④ 控制台打印:

3. 环绕通知
1. 非注解模式
① 待增强业务代码:
@restcontroller
public class aoptestcontroller {
@getmapping("/testaoparound")
public object testaoparound() {
return "testaoparound";
}
}② aop切面:
@aspect
@component
public class aoparoundtestaspect {
@pointcut("execution(public * com.example.demo.controller.aoptestcontroller.testaoparound(..))")
public void logaround() {
}
@around("logaround()")
public object aroundadvice(proceedingjoinpoint proceedingjoinpoint) {
try {
object[] args = proceedingjoinpoint.getargs();
system.out.println("通知类中的aroundadvice方法执行前...");
// 明确调用切入点方法
object resvalue = proceedingjoinpoint.proceed(args);
system.out.println("通知类中的aroundadvice方法执行后...返回值:" + resvalue);
return resvalue;
} catch (throwable e) {
system.out.println("通知类中的aroundadvice方法执行异常...");
throw new runtimeexception(e);
} finally {
system.out.println("通知类中的aroundadvice方法执行完成...");
}
}
}③ 控制台打印:

2. 注解模式
① 自定义注解:
@target({elementtype.method, elementtype.type})
@retention(retentionpolicy.runtime)
public @interface logannotation {
string desc() default " ";
}② 待增强的业务代码:
@restcontroller
public class aoptestcontroller {
@getmapping("/testaoparoundannotation")
@logannotation(desc = "测试注解类型:testaoparoundannotation")
public object testaoparoundannotation(){
return "testaoparoundannotation";
}
}③ aop切面:
@aspect
@component
public class aoparoundannotationtestaspect {
@pointcut(value = "@annotation(com.example.demo.annotation.logannotation)")
public void logaroundannotation() {
}
@around("logaroundannotation() && @annotation(logannotation)")
public object aroundannotationadvice(proceedingjoinpoint proceedingjoinpoint, logannotation logannotation) {
try {
system.out.println("logannotation注解的值:" + logannotation.desc());
object[] args = proceedingjoinpoint.getargs();
system.out.println("通知类中的aroundannotationadvice方法执行前...");
// 明确调用切入点方法
object resvalue = proceedingjoinpoint.proceed(args);
system.out.println("通知类中的aroundannotationadvice方法执行后...返回值:" + resvalue);
return resvalue;
} catch (throwable e) {
system.out.println("通知类中的aroundannotationadvice方法执行异常...");
throw new runtimeexception(e);
} finally {
system.out.println("通知类中的aroundannotationadvice方法执行完成...");
}
}
}④ 控制台打印:

到此这篇关于springboot整合aop及使用案例实战的文章就介绍到这了,更多相关springboot整合aop内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论