加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

自定义注解

发布时间:2020-12-15 01:11:11 所属栏目:大数据 来源:网络整理
导读:自定义注解: 1.成员类型是受限的,合法的类型包括原始类型及String,Calss,Anootation,Enumreation 2.如果注解只有一个成员,则成员名必须取名为Value(),在使用的时可以忽略成员名和赋值号(=) 3.没有成员的注解称为标识注解 public @interface Description

自定义注解:
1.成员类型是受限的,合法的类型包括原始类型及String,Calss,Anootation,Enumreation
2.如果注解只有一个成员,则成员名必须取名为Value(),在使用的时可以忽略成员名和赋值号(=)
3.没有成员的注解称为标识注解

public @interface Description{//使用@interface关键字注解
String name();//成员以无参无异常方式声明
String author();
int age() default 19;//可以用default为成员变量指定一个默认值
}

元注解
@Target({ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD})
// Target 注解的作用域 CONSTRUCTOR 构造方法声明,FIELD 字段声明,LOCAL_VARIABLE 局部变量声明 ,METHOD 方法声明,PACKAGE 包声明,PARAMETER 参数声明,TYPE 类接口。
@Retention(RetentionPolicy.RUNTIME)
//Retention 生命周期——SOURCE 只在源码显示,编译时会丢弃;CLASS 编译时会记录到class中,运行时忽略;RUNTIME 运行时存在,可以通过反射读取。
@Inherited
//Inherited 允许子类继承
@Documented
//Documented 生成javadoc的时候包含注解

下面以一个demo演示解析注解

package com.description.demo;

import java.lang.annotation.Documented;
 java.lang.annotation.ElementType;
 java.lang.annotation.Inherited;
 java.lang.annotation.Retention;
 java.lang.annotation.RetentionPolicy;
 java.lang.annotation.Target;

//注解的作用域
@Target({ElementType.METHOD,ElementType.TYPE})
注解的生命周期
@Retention(RetentionPolicy.RUNTIME)
注解是否允许被继承
@Inherited
是否生成注解Javadoc文档子注解
@Documented
public @interface Description {
    String desc();
    String auth()default"rongrong";
}

接口

public  Person {

    String name();
    int age();
    void say();
}

实现类

 com.description.demo;

@Description(desc = "class annotation")
class Child implements Person {

    @Override
    @Description(desc = "method annotation")
    public String name() {
         TODO Auto-generated method stub
        return null;
    }

    @Override
     age() {
        return 0 say() {
         TODO Auto-generated method stub

    }

}

测试类:

/**
 * 
 */
 java.lang.annotation.Annotation;
 java.lang.reflect.Method;

 org.junit.Test;


 * @author Administrator
 *    解析注解
 class MainTest {
    
    @SuppressWarnings("unchecked")
    @Test
     run(){
        try {
            通过反射加载实体类
            Class c = Class.forName("com.description.demo.Child");
            找到类上面的注解
            boolean isExist= c.isAnnotationPresent(Description.);
            if (isExist) {
                拿到实体类,一定强转为注解类型,反则找不到
                Description d = (Description) c.getAnnotation(Description.);
                System.out.println(d.desc());
            }
            找到方法上的注解
            Method[] method = c.getMethods();
            for (Method ms : method) {
                isExist = ms.isAnnotationPresent(Description.);
                 (isExist) {
                    Description d = ms.getAnnotation(Description.);
                    System.out.println(d.desc());
                }
                另一种方法
                Annotation[] annotation = ms.getAnnotations();
                 (Annotation as : annotation) {
                    if (as instanceof Description) {
                        String desc = ((Description) as).desc();
                        System.out.println(desc);
                    }
                }
            }
        } catch (ClassNotFoundException e) {
             TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读