自定义注解方法及属性(一)


 自定义注解会需要元注解

元注解

java中有四种元注解:@Retention、@Inherited、@Documented、@Target

 

@Retention

注解的保留位置(枚举RetentionPolicy),RetentionPolicy可选值:

  • SOURCE:注解仅存在于源码中,在class字节码文件中不包含
  • CLASS:默认的保留策略,注解在class字节码文件中存在,但运行时无法获得
  • RUNTIME:注解在class字节码文件中存在,在运行时可以通过反射获取到

 

@Inherited

声明子类可以继承此注解,如果一个类A使用此注解,则类A的子类也继承此注解

 

 

@Documented

声明注解能够被javadoc等识别

 

@Target

用来声明注解范围(枚举ElementType),ElementType可选值:

  • TYPE:接口、类、枚举、注解
  • FIELD:字段、枚举的常量
  • METHOD:方法
  • PARAMETER:方法参数
  • CONSTRUCTOR:构造函数
  • LOCAL_VARIABLE:局部变量
  • ANNOTATION_TYPE:注解
  • PACKAGE:包

 

自定义注解介绍

自定义注解使用场景

  1. 类属性自动赋值。
  2. 验证对象属性完整性。
  3. 代替配置文件功能,像spring基于注解的配置。
  4. 可以生成文档,像java代码注释中的@see,@param等

 

定义一个自定义注解(使用元注解,每个元注解都是可选,非必选):

注解中可以声明成员方法,声明的成员方法为最终的注解里面的参数,成员方法可以使用default关键字设置默认值。

新建一个自定义注解:

自定义注解,在定义@Target(value=ElementType.FIELD)@Retention (value=RetentionPolicy.RUNTIME)类时可能会报错。RetentionPolicy/ ElementType cannot be resolved to a variable

解决办法(手动导入类路径):

import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.ElementType;
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Target({ElementType.FIELD,ElementType.METHOD})
@interface MyAnno{
    public String name() default "zhangsan";
    public String email() default "hello@example.com";
}

 

定义一个User类,来使用自定义注解:

class  User{

    @MyAnno(name = "zhang")
    private String name;

    @MyAnno(name = "zhang@example.com")
    private String email;


    @MyAnno(name = "sayHelloWorld")
    public String sayHello(){
        return "";
    }
}

 

通过反射获取注解信息 
下面我们通过反射来演示下获取属性的注解信息和方法的注解信息:
Method[] methods = User.class.getMethods();//获取User类的所有方法
//Field[] fields = User.class.getFields();
Field[] fields = User.class.getDeclaredFields();//获取User类的所有属性
/*
    getFields:只能访问public属性及继承到的public属性
    getDeclaredFields:只能访问到本类的属性,不能访问继承到的属性

    getMethods:只能访问public方法及继承到的public方法
    getDeclaredMethods:只能访问到本类的方法,不能访问继承到的方法

    getConstructors:只能访问public构造函数及继承到的public构造函数
    getDeclaredConstructors:只能访问到本类的构造函数,不能访问继承到的构造函数
*/

for (Field field : fields) {
    MyAnno annotation = field.getAnnotation(MyAnno.class);
    if(annotation!=null){
        System.out.println("property="+annotation.name());
    }
}
for (Method method : methods) {
    MyAnno annotation = method.getAnnotation(MyAnno.class);
    if(annotation!=null){
        System.out.println("sayHello="+annotation.name());
    }
}

 

输出如下:

property=zhang
property=zhang@example.com
sayHello=sayHelloWorld

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM