AOP

# AOP ## 应用场景 ![image.png](https://cos.easydoc.net/48081513/files/kglvn7k8.png) ![image.png](https://cos.easydoc.net/48081513/files/kglvnnnz.png) ## springboot 注解 使用 AOP >1 pom.xml ``` <!--AOP--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- 创建切面--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.5</version> </dependency> <!-- 常用util Hutool--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.4.4</version> </dependency> ``` >2 自定义注解 ``` @Target({ElementType.TYPE,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface wtlZJ { String name(); } ``` >3 创建切面 ``` import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.TimeInterval; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Date; //AOP 实现 @Aspect @Component public class TokenAspect { @Value("${wtl.onOff}") private boolean onOff; //开关 application 里面配置 //切点(Pointcut)对哪些方法进行拦截 注解类路径(com.example.demo.zhujian.wtlZJ) @Pointcut(value = "@annotation(com.example.demo.zhujian.wtlZJ)") public void route(){}; @Around("route()") //切面(Aspect译 环绕)通常是一个类,里面可以定义切入点和通知 public Object around(ProceedingJoinPoint joinPoint) throws Throwable { if(!onOff){ return joinPoint.proceed();} //判断是否开启计时功能 TimeInterval timer = DateUtil.timer(); //计时器 Object o=joinPoint.proceed(); //方法(加注解的原方法) System.out.println("结束"+timer.interval()); //花费毫秒数 /* MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); WtlMethodExecutionTime action = method.getAnnotation(WtlMethodExecutionTime.class); System.out.println("wtl"+action.name());*/ //获取参数 return o; //日志记录 //Object[] method_args = joinPoint.getArgs(); //获取方法的参数值数组。 //if(true){ return "参数错误";} //String class_name = joinPoint.getTarget().getClass().getName(); //类名 //String method_name = joinPoint.getSignature().getName(); //方法名 } @After("route()") public void after(JoinPoint joinPoint) { System.out.println("结束"); //花费毫秒数 } @Before("route()") public void Before(JoinPoint joinPoint) { System.out.println("开始"); //花费毫秒数 } } ``` >4 配置开关 ![image.png](https://cos.easydoc.net/48081513/files/kglw4sv4.png) >5 使用 ``` @Controller @RequestMapping(value = "/t1", produces = "application/json; charset=utf-8") //乱码问题 @ResponseBody public class WtlController { @GetMapping(value = "/get1") @wtlZJ public String getChinaArea() { return "200"; } } ``` > 6 说明 ![image.png](https://cos.easydoc.net/48081513/files/kh08vzab.png) [**说明**](https://www.cnblogs.com/aspirant/p/10288903.html) # 注解 ## 3个内置注解 ![image.png](https://cos.easydoc.net/48081513/files/kfdjjjab.png) ## 元注解 ![image.png](https://cos.easydoc.net/48081513/files/kfdjmjho.png) 元注解用于修饰其他的注解(纪委:管干部的干部) ① @Retention:定义注解的保留策略 @Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含 @Retention(RetentionPolicy.CLASS) //默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得, @Retention(RetentionPolicy.RUNTIME) //注解会在class字节码文件中存在,在运行时可以通过反射获取到 ② @Target:指定被修饰的Annotation可以放置的位置(被修饰的目标) @Target(ElementType.TYPE) //接口、类 @Target(ElementType.FIELD) //属性 @Target(ElementType.METHOD) //方法 @Target(ElementType.PARAMETER) //方法参数 @Target(ElementType.CONSTRUCTOR) //构造函数 @Target(ElementType.LOCAL_VARIABLE) //局部变量 @Target(ElementType.ANNOTATION_TYPE) //注解 @Target(ElementType.PACKAGE) //包 注:可以指定多个位置,例如: @Target({ElementType.METHOD, ElementType.TYPE}),也就是此注解可以在方法和类上面使用 ③ @Inherited:指定被修饰的Annotation将具有继承性 ④@Documented:指定被修饰的该Annotation可以被javadoc工具提取成文档. ———————————————— 版权声明:本文为CSDN博主「小依不秃头」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_54936371/article/details/122576023 元注解用于修饰其他的注解(纪委:管干部的干部) ① @Retention:定义注解的保留策略 @Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含 @Retention(RetentionPolicy.CLASS) //默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得, @Retention(RetentionPolicy.RUNTIME) //注解会在class字节码文件中存在,在运行时可以通过反射获取到 ② @Target:指定被修饰的Annotation可以放置的位置(被修饰的目标) @Target(ElementType.TYPE) //接口、类 @Target(ElementType.FIELD) //属性 @Target(ElementType.METHOD) //方法 @Target(ElementType.PARAMETER) //方法参数 @Target(ElementType.CONSTRUCTOR) //构造函数 @Target(ElementType.LOCAL_VARIABLE) //局部变量 @Target(ElementType.ANNOTATION_TYPE) //注解 @Target(ElementType.PACKAGE) //包 注:可以指定多个位置,例如: @Target({ElementType.METHOD, ElementType.TYPE}),也就是此注解可以在方法和类上面使用 ③ @Inherited:指定被修饰的Annotation将具有继承性 ④@Documented:指定被修饰的该Annotation可以被javadoc工具提取成文档. ———————————————— ## 自定义注解 ![image.png](https://cos.easydoc.net/48081513/files/kfdjtl5g.png) ![image.png](https://cos.easydoc.net/48081513/files/kfdk2whn.png)