SpringBoot AOP (三)
演示一个@annotation方式
@annotation:
带有相应注解的方法,比如对标有@Transactional注解的方法进行增强
@annotation(org.springframework.transaction.annotation.Transactional)
实战
第一步:编写一个自定义注解
//表示次注解可以标注在类和方法上
@Target({ElementType.METHOD, ElementType.TYPE})
//运行时生效
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLogAnnotation {
//定义一个变量,可以接受参数
String desc() default " ";
}
第二步:在HelloController类中添加一个方法
@RestController
public class HelloController {
@RequestMapping("helloAnnotation")
//标有这个注解的方法会被增强
@MyLogAnnotation(desc = "@Annotation")
public Object helloAnnotation() {
return "hello annotation";
}
}
第三步:切面类
@Aspect
@Component
public class MyAopAnnotation {
//切入点:增强标有MyLogAnnotation注解的方法
@Pointcut(value="@annotation(com.aismall.testaop.MyAnnotation.MyLogAnnotation)")
//切入点签名
public void logAnnotation(){
System.out.println("pointCut签名。。。");
}
//前置通知
@Before("logAnnotation()")
public void deBefore(JoinPoint jp) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
System.out.println("URL : " + request.getRequestURL().toString());
}
//返回通知
@AfterReturning(returning = "ret", pointcut = "logAnnotation()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
System.out.println("返回通知:方法的返回值 : " + ret);
}
//异常通知
@AfterThrowing(throwing = "ex", pointcut = "logAnnotation()")
public void throwss(JoinPoint jp,Exception ex){
System.out.println("异常通知:方法异常时执行.....");
System.out.println("产生异常的方法:"+jp);
System.out.println("异常种类:"+ex);
}
//后置通知
@After("logAnnotation()")
public void after(JoinPoint jp){
System.out.println("后置通知:最后且一定执行.....");
}
}
第四步:启动项目
- 请求链接:http://localhost:8080/helloAnnotation
- 控制台返回的结果:
URL : http://localhost:8080/helloAnnotation
返回通知:方法的返回值 : hello annotation
后置通知:最后且一定执行.....
环绕通知
spring的通知(Advice)中 一共有五种通知,之前已经介绍了四种,为什么不把环绕通知和它们放在一起说,因为环绕通知可以把前面的四种通知都表示出来,而且环绕通知一般单独使用
环绕通知的使用:
- Spring框架为我们提供了一个接口:ProceedingJoinPoint,该接口有一个方法proceed(),此方法就相当于明确调用切入点方法。
- 该接口作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用。
- 增强代码写在调用proceed()方法之前为前置通知,之后为返回通知,写在catch中为异常通知,写在finally中为后置通知
实战:
第一步:在HelloController类中添加一个方法
@RestController
public class HelloController {
@RequestMapping("/helloAround")
public Object helloAround(){
System.out.println("helloAround执行了。。。");
return "hello around";
}
}
第二步:切面类
@Aspect
@Component
public class MyAroundAop {
//切入点:待增强的方法
@Pointcut("execution(public * com.aismall.testaop.controller.*.*(..))")
//切入点签名
public void logAround() {
System.out.println("pointCut签名。。。");
}
//环绕通知,环绕增强,相当于MethodInterceptor
@Around("logAround()")
public Object aroundAdvice(ProceedingJoinPoint pjp) {
Object rtValue = null;
try {
Object[] args = pjp.getArgs();//得到方法执行所需的参数
System.out.println("通知类中的aroundAdvice方法执行了。。前置");
rtValue = pjp.proceed(args);//明确调用切入点方法(切入点方法)
System.out.println("通知类中的aroundAdvice方法执行了。。返回");
System.out.println("返回通知:"+rtValue);
return rtValue;
} catch (Throwable e) {
System.out.println("通知类中的aroundAdvice方法执行了。。异常");
throw new RuntimeException(e);
} finally {
System.out.println("通知类中的aroundAdvice方法执行了。。后置");
}
}
}
第三步:启动项目
- 请求链接:http://localhost:8080/helloAround
- 控制台返回的结果:
通知类中的aroundAdvice方法执行了。。前置
helloAround执行了。。。
通知类中的aroundAdvice方法执行了。。返回
返回通知:hello around
通知类中的aroundAdvice方法执行了。。后置