posts - 397,comments - 0,views - 25332

元注解:用于描述注解的注解

  @Target :描述注解能够作用的位置

  ElementType取值

  type :可以作用于类上

  METHOD :可以作用于方法上

  FIELD : 可以用于成员变量上

@Retention :描述注解被保留的阶段

@Retention(RetentionPolicy.RUNTIME)︰当前被描述的注解,会保留到class字节码文件中,并被Jvm读取到

@Documented :描述注解是否被抽取到api文档中

Inherited:描述注解是否被子类继承

 

复制代码
import java.lang.annotation.*;

@Target(value = {ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyAnno3 {


}
复制代码

 

 

 

 

 

 

注解_解析注解

在程序使用(解析)注解:获取注解中定义的属性值

1。获取注解定义的位的对象(class,Method,Field)

2.获取指定的注解

复制代码
@Pro(className = "Itc.annotation.Demo1",methodName = "show")
public class ReflectDemo5 {

    public static void main(String[] args){

        Class<ReflectTest> reflectTestClass = ReflectTest.class;
        Pro annotation = reflectTestClass.getAnnotation(Pro.class);

        String s = annotation.className();
        String s1 = annotation.methodName();
        System.out.println(s);
        System.out.println(s1);
    }

}
复制代码

 

public class Demo1 {

    public void show(){
        System.out.println("demo1.....show");
    }
}

 

public class Demo1 {

    public void show(){
        System.out.println("demo1.....show");
    }
}

 

复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Pro {

    String className();
    String  methodName();

}
复制代码

 

posted on   淤泥不染  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示