AOP--注解
# AOP
## 应用场景


## 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 配置开关

>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 说明

[**说明**](https://www.cnblogs.com/aspirant/p/10288903.html)
# 注解
## 3个内置注解

## 元注解

## 自定义注解

