元注解:用于描述注解的注解
@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(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)