创建项目要AOP全称Aspect Oriented Programming意为面向切面编程,也叫做面向方法编程,是通过预编译方式和运行期动态代理的方式实现不修改源代码的情况下给程序动态统一添加功能的技术。
注解的方式实现AOP导入AOP依赖
aopalliance aopalliance1.0 org.springframework spring-aspects5.0.2.RELEASE org.aspectj aspectjweaver1.8.3
@Before前置通知创建自定义注解
package com.example.noteback.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //方法上添加该注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AopAnno { public String value(); }创建Test类,test方法
package com.example.noteback.test; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class Test { @RequestMapping("test") @AopAnno(value = "test") public String test(String str) { System.out.println("test方法" + str); return "test"; } }创建AopTest类
package com.example.noteback.test; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Component @Aspect public class AopTest { //切入点 @Pointcut("@annotation(com.example.noteback.test.AopAnno)") public void test() { } }
@Around环绕通知前置通知在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。使用方法如下:
@Component @Aspect public class AopTest { //切入点 @Pointcut("@annotation(com.example.noteback.test.AopAnno)") public void test() { } //跟据方法名(上面的test方法)获取切入点 @Before("test()") //获取要增强的方法参数,可以添加 JoinPoint public void before(JoinPoint joinPoint) { //获取方法签名 MethodSignature methodSignature=(MethodSignature) joinPoint.getSignature(); //获取要增强的方法 Method method=methodSignature.getMethod(); System.out.println("方法名"+method.getName()); } }
@After后置通知包围一个连接点的通知,类似 Web 中 Servlet 规范中的 Filter 的 doFilter 方法。可 以在方法的调用前后完成自定义的行为, 也可以选择不执行。使用方法如下:
@Component @Aspect public class AopTest { @Pointcut("@annotation(com.example.noteback.test.AopAnno)") public void test() { } @Around("test()") public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { //要增强的方法执行前 System.out.println("aroundBefore"); //执行要增强的方法 Object proceed = proceedingJoinPoint.proceed(); //要增强的方法执行后 System.out.println("aroundAfter"); return proceed; } }通过@Around可以实现权限验证,操作日志等。实现操作日志:
@Component @Aspect public class AopTest { @Pointcut("@annotation(com.example.noteback.test.AopAnno)") public void test() { } @Around("test()") public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { MethodSignature methodSignature=(MethodSignature) proceedingJoinPoint.getSignature(); Method method=methodSignature.getMethod(); StringBuilder stringBuilder=new StringBuilder(); stringBuilder.append("方法:"+method.toString()); stringBuilder.append("t参数("); //参数名 String[] parameterNames=methodSignature.getParameterNames(); //参数值 Object[] parameterValues=proceedingJoinPoint.getArgs(); for (int i = 0; i < parameterNames.length; i++) { stringBuilder.append(parameterNames[i]+":"+parameterValues[i]+","); } stringBuilder.append(")"); //允许目标方法执行 Object proceed = proceedingJoinPoint.proceed(); stringBuilder.append("t返回("); stringBuilder.append(method.getReturnType()+":"+proceed+")"); System.out.println(stringBuilder); //返回执行结果 return proceed; } }
@AfterReturning当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。
对Test类里面的值稍加修改
@RestController public class Test { @RequestMapping("test") @AopAnno(value = "test") public String test(String str) { //正常情况下会报错,下面的方法不执行 return (1/0)+""; } }@After使用方法如下:
package com.example.noteback.test; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.Arrays; @Component @Aspect public class AopTest { @Pointcut("@annotation(com.example.noteback.test.AopAnno)") public void test() { } @After("test()") public void after() { System.out.println("after"); } }运行结果:
@AfterThrowing在某连接点正常完成后执行的通知,不包括抛出异常的情况。使用方法如下:
package com.example.noteback.test; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.Arrays; @Component @Aspect public class AopTest { @Pointcut("@annotation(com.example.noteback.test.AopAnno)") public void test() { } @AfterReturning("test()") public void afterReturning() { System.out.println("AfterReturning"); } }
执行顺序在方法抛出异常退出时执行的通知。使用方法如下:
package com.example.noteback.test; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.Arrays; @Component @Aspect public class AopTest { @Pointcut("@annotation(com.example.noteback.test.AopAnno)") public void test() { } @AfterThrowing("test()") public void afterThrowing(){ System.out.println("afterThrowing"); } }
源码AfterThrowing在发生异常时执行
gitee仓库:git@gitee.com:AGi_R/note.git
在命令行输入:git clone git@gitee.com:AGi_R/note.git 进行下载



