java自定义注解

注解详解

  • @Documented:使用该注解的元素将被javadoc工具提取成文档。

  • @Inherited: 被它修饰的元素将具有继承性.如果某个类使用了被 @Inherited 修饰的 Annotation, 则其子类将自动具有该注释。

  • @Retention,用于指定该元素的声明周期。

    • RetentionPolicy.CLASS: 编译器将把注释记录在 class 文件中. 当运行 Java 程序时, JVM 不会保留注释. 这是默认值
    • RetentionPolicy.RUNTIME:编译器将把注释记录在 class 文件中. 当运行 Java 程序时, JVM 会保留注释. 程序可以通过反射获取该注释
    • RetentionPolicy.SOURCE: 编译器直接丢弃这种策略的注释
  • @Target:指定注解元素的种类:
    来波源码:

public enum ElementType {
    <br>  /** Class, interface (including annotation type), or enum declaration */ -- 标注类,接口,注解,枚举<br>    TYPE,

    /** Field declaration (includes enum constants) */ --标注字段,枚举常量
    FIELD,

    /** Method declaration */ -- 标注方法
    METHOD,

    /** Parameter declaration */ -- 标注参数
    PARAMETER,

    /** Constructor declaration */--标注构造器
     CONSTRUCTOR,

    /** Local variable declaration */ -- 标注局部变量
    LOCAL_VARIABLE,

    /** Annotation type declaration */ -- 标注注解
    ANNOTATION_TYPE,

    /** Package declaration */ -- 标注包
    PACKAGE
}

实例


@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface DataSource {
    String value() default "hello";
}
@DataSource
public interface TargetInterface {
     public abstract void getAK();
}
@DataSource("targetClass")
public class TargerClass {

    @DataSource("f1")
    public void f1() {

    }
}

public class AnnotationTest {
    public static void main(String[] args) {
        //判断某个类是否是注解
        System.out.println("对象是否为注解类型:"+DataSource.class.isAnnotation());
        System.out.println("获取目标类上注解里的值(如果没有值,则会取到注解中的默认值):"+TargetInterface.class.getAnnotation(DataSource.class).value());
        //获取某个具体类的所有注解
        Annotation[] annotations = TargetInterface.class.getAnnotations();
        for(Annotation annotation : annotations){
            //判断当前对象是否为自定义注解
            if(annotation.annotationType() == DataSource.class){
                System.out.println("类注解名称:"+DataSource.class.getSimpleName());
                Method[] methods = DataSource.class.getDeclaredMethods();
                for(Method method : methods){
                    System.out.println("类注解方法:"+method.getName());
                }
                System.out.println();
            }
        }
        System.out.println("获取某个类中所有方法的所有方法注解");
        Method[] methods = TargerClass.class.getMethods();
        for(Method method : methods){
            System.out.println("类方法名:"+method.getName());
            System.out.println("调用方法注解的属性值:"+method.getAnnotation(DataSource.class).value());
            Annotation[] mAnnotations = method.getAnnotations();
            for(Annotation mAnnotation : mAnnotations){
                if(mAnnotation.annotationType() == DataSource.class){
                    System.out.println("注解名:"+DataSource.class.getSimpleName());
                }
            }
        }
    }
}

运行结果:

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值