自定义注解
1、最简单的自定义注解
package annotation;
public @interface HpuAnnotation {
// 注解相当于一个特殊的类型,与Enum 、 Interface类似
}
2、测试自定义的注解
package annotation;
@HpuAnnotation
public class AnnotationTest {
public static void main(String[] args) {
// 判断AnnotationTest 是否添加了HpuAnnotation注解
if(AnnotationTest.class.isAnnotationPresent(HpuAnnotation.class)) {
// 获取AnnotationTest上面相应的注解
HpuAnnotation annotation =
AnnotationTest.class.getAnnotation(HpuAnnotation.class);
// 打印注解信息
System.out.println(annotation);
}
}
}
运行结果 : 不输出任何信息
原因:自定义注解有有三种保留策略:
- --RetentionPolicy.CLASS
- --RetentionPolicy.RUNTIME
- --RetentionPolicy.SOURCE
java语句执行的过程中包括三个阶段,第一个阶段是源文件阶段,即.java文件阶段;第二个阶段是Class阶段,即由源文件编译形成.class文件;第三个阶段是运行阶段,即由类加载器将.class文件加载进入内存,形成字节码的阶段。而自定义注解的默认保留策略为:class阶段,即.class文件里面有注解的信息,但是将.class文件加载进入内存的时候,会忽略掉注解信息。
If no Retention annotation is present on an annotation type declaration, the retention policy defaults to RetentionPolicy.CLASS.
故在自定义的注解上加上注解@Retention(RetentionPolicy.RUNTIME)设定保留策略后,就可以得到想要的结果了。
运行结果为:@annotation.HpuAnnotation()
3、在自定义注解上添加注解 @Target(value=ElementType.METHOD) 可以设定自定义注解的使用位置
- --METHOD 只能加在方法上
- --TYPE 只能加在类上
- --CONSTRUCTOR 只能加在构造方法上
- --FIELD 只能加在字段上
- --LOCAL_VARIABLE 只能加在局部变量上
- --PACKAGE 加在包上面
- --PARAMETER 加在参数上面
- --ANNOTATION_TYPE 注释类型声明
4、给自定义注解添加属性,在注解里面属性的存在形式为方法,属性值与返回值的类型一样。注解里面所有方法的默认类型为public abstract 类型的。如下(给注解添加一个属性color,其值的类型要求是String):
// 注解相当于一个特殊的类型,与Enum 、 Interface类似
@Retention(RetentionPolicy.RUNTIME)
@Target(value={ElementType.METHOD,ElementType.TYPE,ElementType.FIELD})
public @interface HpuAnnotation {
// 属性名:color 属性值类型:java.lang.String
String color();
}
测试方法:
@HpuAnnotation(color="red")
public class AnnotationTest {
public static void main(String[] args) {
// 判断AnnotationTest 是否添加了HpuAnnotation注解
if(AnnotationTest.class.isAnnotationPresent(HpuAnnotation.class)) {
// 获取AnnotationTest上面相应的注解
HpuAnnotation annotation = AnnotationTest.class.getAnnotation(HpuAnnotation.class);
// 获取注解中属性的值,获取方式通过调用相应的方法
System.out.println(annotation.color());
}
}
}
设置默认属性值:
// 属性名:color 属性值类型:java.lang.String
String color() default "blue";
当属性名为value的时候,且只需要设定value属性的时候,可以不用写value=,而直接在注解后面写上值即可。
@MetaAnnotation("wcj")
5、属性的多种值类型
自定义枚举类型,用于作为注解属性值类型:
public enum Color {
RED,GREEN,YELLOW
}
自定义注解类型,用于作为注解属性值的类型:
public @interface MetaAnnotation {
String value();
}
自定义的多种属性值注解:
// 注解相当于一个特殊的类型,与Enum 、 Interface类似
@Retention(RetentionPolicy.RUNTIME)
@Target(value={ElementType.METHOD,ElementType.TYPE,ElementType.FIELD})
public @interface HpuAnnotation {
// 属性名:color 属性值类型:java.lang.String
String color() default "blue";
// 属性名:enumColor 属性值类型:Enum
Color enumColor();
// 属性名:array 属性值类型:int[]
int[] array();
// 属性名: annotation 属性值类型:@interface
MetaAnnotation annotation() default @MetaAnnotation("wcj");
}
测试各种属性值类型:
@HpuAnnotation(enumColor=Color.GREEN,array={1,2,3},annotation=@MetaAnnotation("xy"))
public class AnnotationTest {
public static void main(String[] args) {
// 判断AnnotationTest 是否添加了HpuAnnotation注解
if(AnnotationTest.class.isAnnotationPresent(HpuAnnotation.class)) {
// 获取AnnotationTest上面相应的注解
HpuAnnotation annotation = AnnotationTest.class.getAnnotation(HpuAnnotation.class);
// 获取注解中属性的值,获取方式通过调用相应的方法
System.out.println(annotation.color());
// 获取属性值类型为Enum
System.out.println(annotation.enumColor());
// 获取属性值类型为int[]
int[] arr = annotation.array();
System.out.print("[");
for(int i = 0; i < arr.length; i++) {
if(i != arr.length - 1) {
System.out.print(arr[i] + ",");
} else {
System.out.print(arr[i]);
}
}
System.out.println("]");
// 获取属性值类型为@interface
System.out.println(annotation.annotation());
}
}
}
运行结果:
blue
GREEN
[1,2,3]
@annotation.MetaAnnotation(value=xy)
分享到:
相关推荐
自定义注解结合Hutool对SpringBoot接口返回数据进行脱敏 自定义注解结合Hutool对SpringBoot接口返回数据进行脱敏 自定义注解结合Hutool对SpringBoot接口返回数据进行脱敏 自定义注解结合Hutool对SpringBoot接口返回...
3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑...
这个一个自定义注解实现给注解传递伪动态参数的小案例,可以在此自定义注解的基础上来记录接口的调用记录。
java 自定义注解验证 可自己添加所需要的注解,本案例中只写了三个自定义注解类 本案例只花了半天时间,如有不好之处请多提提意见。谢谢!
自定义注解实现拦截sql,并在sql中增加相应的条件 。 对应博客: https://blog.csdn.net/qq_37716298/article/details/120659139
自定义注解得使用,模拟spring通过注解方式创建bean实例!
SpringBoot AOP各种注解、自定义注解、鉴权使用案例SpringBoot AOP各种注解、自定义注解、鉴权使用案例SpringBoot AOP各种注解、自定义注解、鉴权使用案例
excel导入动态校验,自定义注解动态校验
包括Java自定义注解以及包括自定义注解的应用实例。以及注解相关知识的注解
看到别的博主写的几篇接绍注解相关的东西,觉得挺好的,mark一下 ... 自定义注解:...主要就是自定义了注解,使用了自定义注解,然后利用java的反射机制根据注解生成不同的sql语句
springboot 脱敏自定义注解 SpringAOP
通过一个超市的自动售卖机的例子,详细说明了java的自定义注解的使用。
自定义java注解开发,javabean中某方法添加该自定义注解,配置该方法相关信息指标,通过全局扫描该注解获取全部方法及相关指标,自动执行方法(本demo用于系统监控开发)
springboot自定义注解的demo;通过本案例实现了通过自定义注解的拦截的方法可以设置访问权限;通过拦截器获取指定方法上的自定义的注解,然后判断当前的接口需不要权限校验;然后通过对应的访问去处理;demo里面的代码...
java自定义注解实践,介绍详细!值得参考
自定义注解bindview 省去findviewbyid,直接在变量前边添加@BindView即可绑定控件
自定义注解实现由类自定生成数据库中的表,适合各种类型的数据库,本案例主要实现的时sql server和oracle
spring中自定义注解(annotation)与AOP中获取注解.通过实例演示自定义注解。
SpringBoot 、Shiro、 自定义注解权限控制
网上很多人想使用注解拦截spring mvc action中的一个方法,实现方法很多,一般是通过在拦截器中分析url路径来实现, 使用自定义注解的方式来标注要拦截的 action 中的某个方法, 没有很好的解决方法, 如果通过借助spring...