annotation 自定义注解
来源:互联网 发布:谷歌地图软件 编辑:程序博客网 时间:2024/06/05 01:51
----------------------annotation----------------------------------------------------------------------
内建annotation的RetentionPolicy 基本使用
------自定义注解---必须在自己的文件夹呢
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(value=RetentionPolicy.RUNTIME) //表示此annotation在运行的时候有效
public @interface Sty {
public String key() default "hahaha";
public String hello() default "xx";
public String[] hello2() ; //定义一个数组
}
------使用自定义注解---------------------
@Sty(key = "999",hello="000",hello2={"111","2","ppp"})
public class TTTT {
}
-------通过反射取得annotation----------------
Class<?> class1 = Class.forName("com.dt.annotation.Demo");
Method method = class1.getMethod("toString");
Annotation annotation[] = method.getAnnotations();
for(Annotation annotation2:annotation){
System.out.println(annotation2);
}
-----------------------------
@Sty(key = "999",hello="000")
public class TTTT {
public static void main(String[] args) throws Exception {
Class<?> class1 = Class.forName("com.dt.annotation.Demo");
Method method = class1.getMethod("toString");
Annotation annotation[] = method.getAnnotations();
for(Annotation annotation2:annotation){
System.out.println(annotation2);
}
if (method.isAnnotationPresent(Sty.class)) {
Sty aSty = method.getAnnotation(Sty.class); //Sty.class 是自定义annotation
String key = aSty.key();
String hello=aSty.hello();
System.out.println(key+" ,"+hello);
}
}
}
阅读全文
0 0
- Java 自定义注解(Annotation)
- 自定义注解annotation
- Java自定义注解 Annotation
- 自定义Annotation(注解)
- Annotation自定义注解
- Java自定义注解Annotation
- 自定义注解Annotation
- 自定义一个注解 annotation
- Java 自定义注解Annotation
- 自定义注解(Annotation)
- 自定义注解 annotation 总结
- 自定义注解Annotation
- annotation 自定义注解
- Java自定义注解Annotation
- JAVA,Annotation自定义注解
- 注解(Annotation)自定义注解
- 自定义注解Annotation的使用
- Java Annotation自定义注解详解
- Java Web架构知识整理——记一次阿里面试经历
- 并发多线程
- printf格式化输出符号详细说明[转]
- eclipse复制maven项目修改位置
- 河南视野中的日本研究所列表
- annotation 自定义注解
- spring mvc番外篇(五):spring mvc结合ajax,json技术的应用
- css3滤镜
- 《STL源码剖析》-序列式容器(一)vector容器
- 2017.8.10每天五个编程题(六)
- 机器学习基础-学习笔记 矩阵论
- 时间序列
- python学习笔记之013.py
- python学习笔记之014.py