1、案例
我们根据工作中用到的接口限流的注解案例来学习
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RedisRateLimit {
String limitKey() default ""; // 限流的方法名
int time() default 1; // 默认设置为1秒
int value() default 10; // 发放的许可证数量
}
2、注解@target
@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
取值(ElementType)有:
- CONSTRUCTOR:用于描述构造器
- FIELD:用于描述域
- .LOCAL_VARIABLE:用于描述局部变量
- METHOD:用于描述方法
- PACKAGE:用于描述包
- PARAMETER:用于描述参数
- TYPE:用于描述类、接口(包括注解类型) 或enum声明
3、注解@Retention
@Retention 用来指定注解的生命周期(源码、class文件、运行时),@Retention注解有一个属性value,是RetentionPolicy类型的,Enum RetentionPolicy是一个枚举类型,这个枚举决定了Retention注解应该如何去保持,也可理解为Rentention 搭配 RententionPolicy使用。
RetentionPolicy有3个值:
- RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;
- RetentionPolicy.CLASS:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;
- RetentionPolicy.RUNTIME:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在;
4、注解@Documented
@Documented 注解表明这个注解应该被 javadoc工具记录. 默认情况下,javadoc是不包括注解的。 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工具处理,所以注解类型信息也会被包括在生成的文档中,是一个标记注解,没有成员。
5、自定义注解
使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节。在定义注解时,不能继承其他的注解或接口。@interface用来声明一个注解,其中的每一个方法实际上是声明了一个配置参数。方法的名称就是参数的名称,返回值类型就是参数的类型(返回值类型只能是基本类型、Class、String、enum)。可以通过default来声明参数的默认值。
5.1、定义注解格式:
public @interface 注解名 {定义体}
现在我们可以理解案例的自定义注解@RedisRateLimit的含义了,作用在方法上,生命周期为运行时,注解里定义了三个参数。
5.2、举例说明:分别作用在类、方法、属性上的注解
5.2.1、作用在属性上注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FiledAnnotation {
String value() default "GetFiledAnnotation";
}
5.2.2、作用在方法上注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodAnnotation {
String name() default "MethodAnnotation";
String url() default "https://www.cnblogs.com";
}
5.2.3、作用在类上注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TypeAnnotation {
String value() default "Is-TypeAnnotation";
}
5.2.4、测试
1、使用自定义注解
@TypeAnnotation(value = "doWork")
public class Worker {
@FiledAnnotation(value = "CSDN博客")
private String myfield = "";
@MethodAnnotation()
public String getDefaultInfo() {
return "do the getDefaultInfo method";
}
@MethodAnnotation(name = "百度", url = "www.baidu.com")
public String getDefineInfo() {
return "do the getDefineInfo method";
}
}
2、测试
public class TestMain {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("com.wts.annotation.pattern3.Worker");
Method[] method = cls.getMethods();
/**判断Worker类上是否有TypeAnnotation注解*/
boolean flag = cls.isAnnotationPresent(TypeAnnotation.class);
/**获取Worker类上是TypeAnnotation注解值*/
if (flag) {
TypeAnnotation typeAnno = (TypeAnnotation) cls.getAnnotation(TypeAnnotation.class);
System.out.println("@TypeAnnotation值:" + typeAnno.value());
}
/**方法上注解*/
List<Method> list = new ArrayList<Method>();
for (int i = 0; i < method.length; i++) {
list.add(method[i]);
}
for (Method m : list) {
MethodAnnotation methodAnno = m.getAnnotation(MethodAnnotation.class);
if (methodAnno == null)
continue;
System.out.println( "方法名称:" + m.getName());
System.out.println("方法上注解name = " + methodAnno.name());
System.out.println("方法上注解url = " + methodAnno.url());
}
/**属性上注解*/
List<Field> fieldList = new ArrayList<Field>();
for (Field f : cls.getDeclaredFields()) {// 访问所有字段
FiledAnnotation filedAno = f.getAnnotation(FiledAnnotation.class);
System.out.println( "属性名称:" + f.getName());
System.out.println("属性注解值FiledAnnotation = " + filedAno.value());
}
}
}
本文详细介绍了Java中使用@interface声明自定义注解的规则,包括不能继承其他注解、每个方法对应参数等。同时,给出了注解在类、方法、属性上的应用示例,并展示了如何在代码中测试和获取自定义注解信息。

145

被折叠的 条评论
为什么被折叠?



