2. 注解
2.1 什么是注解
注解用来给类声明附加额外信息,可以标注在类、字段、方法等上面,编译器、JVM以及开发人员等都可以通过反射拿到注解信息,进而做一些相关处理
SpringBoot 全部都是采用注解化
2.1.1 常用注解
@Override 只能标注在子类覆盖父类的方法上面,有提示的作用
@Deprecated 标注在过时的方法或类上面,有提示的作用
@SuppressWarnings(“unchecked”) 标注在编译器认为有问题的类、方法等上面,用来取消编译器的警告提示,警告类型有serial、unchecked、unused、all
2.1.2 元注解
元注解用来在声明新注解时指定新注解的一些特性
@Target 指定新注解标注的位置,比如类、字段、方法等,取值有ElementType.Method等
@Retention 指定新注解的信息保留到什么时候,取值有RetentionPolicy.RUNTIME等
@Inherited 指定新注解标注在父类上时可被子类继承
2.1.3 注解的Target
TYPE:类、接口(包括注解类型)和枚举的声明
FIELD:字段声明(包括枚举常量)
METHOD:方法声明
PARAMETER:参数声明
CONSTRUCTOR:构造函数声明
LOCAL_VARIABLE:本地变量声明
ANNOTATION_TYPE:注解类型声明
PACKAGE:包声明
TYPE_PARAMETER:类型参数声明,JavaSE8引进,可以应用于类的泛型声明之处
TYPE_USE:JavaSE8引进,此类型包括类型声明和类型参数声明
2.2 获取注解信息
// 1.获取当前类上的注解
Class<?> aClass = Class.forName("com.example.demo.pojo.User");
MyAnnotation declaredAnnotation1 = aClass.getDeclaredAnnotation(MyAnnotation.class);
// 2.获取当前方法上的注解
Method usernameMethod = aClass.getDeclaredMethod("getUsername");
MyAnnotation declaredAnnotation2 = usernameMethod.getDeclaredAnnotation(MyAnnotation.class);
// 3.获取字段上的注解
Field username = aClass.getDeclaredField("username");
MyAnnotation declaredAnnotation3 = username.getDeclaredAnnotation(MyAnnotation.class);
// 4.获得构造方法注解
Constructor<?> constructor = aClass.getConstructor();
MyAnnotation declaredAnnotation = constructor.getDeclaredAnnotation(MyAnnotation.class);
2.3 注解的使用
反射+aop
2.3.1 自定义注解
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyAnnotation {
String name() default "";
int limit() default 20;
}
@MyAnnotation(name = "get",limit = 5)
public User get() {
return null;
}
2.3.2 定义aop
@Component
@Aspect //切面 定义了通知和切点的关系
public class MyAspect {
//前置通知在目标方法调用前执行
@Before(value = "@annotation(com.example.demo.annotation.MyAnnotation)")
public void before(){
System.out.println("------------------前置通知------------------");
}
//后置通知在调用目标方法拿到返回值后执行
@After(value = "@annotation(com.example.demo.annotation.MyAnnotation)")
public void after(){
System.out.println("------------------后置通知------------------");
}
@Around(value = "@annotation(com.example.demo.annotation.MyAnnotation)")
public Object around(ProceedingJoinPoint joinPoint){
try {
//获取拦截的方法
Signature sig = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) sig;
Method method = methodSignature.getMethod();
//获取方法上的注解
MyAnnotation declaredAnnotation = method.getDeclaredAnnotation(MyAnnotation.class);
// 获取注解上的信息
String name = declaredAnnotation.name();
int limit = declaredAnnotation.limit();
//具体业务处理
if (limit > 100){
return "超出限制";
}
//目标方法调用
Object result = joinPoint.proceed();
//返回值返回到调用目标方法的地方
return result;
} catch (Throwable throwable) {
return "系统出现了错误!";
}
}
}