Java注解和反射
2015-09-25 00:00
519 查看
摘要: 注解 反射
结果:
user1
3
id12
name22
age32
package com.annotation.demo2; 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 Table { public String value(); }
package com.annotation.demo2; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Coloumn { String value(); }
package com.annotation.demo2; @Table("user") public class Fb { @Coloumn("id1") public int id; @Coloumn("name2") public String name; @Coloumn("age3") public String age; }
package com.annotation.demo2; import java.lang.reflect.Field; public class DemoTest { public static void main(String[] args) { Class c = Fb.class;//获得Class类的Fb对象 Table t=(Table)c.getAnnotation(Table.class);//获得类名级别注解类型 System.out.println(t.value()+"1"); Field[] ms = c.getFields();//获得字段类型级别注解类型 System.out.println(ms.length); for(Field m:ms){ Coloumn cl=m.getAnnotation(Coloumn.class); System.out.println(cl.value()+"2"); } } }
结果:
user1
3
id12
name22
age32
相关文章推荐
- Java替换各种特殊字符工具类
- Eclipse提示Unhandled event loop exception 卡死
- java中的内部类总结
- Java的两个高精度的包装类
- Spring 架构简要说明
- Java:switch语句例子
- spring + freemark 的配置
- JAVA API文档(非chm版)
- JAVA学习2_Eclipse引入jar包的三种方式
- JAVA异常
- 多线程JAVA
- java---工具类的设计
- JVM(java 虚拟机)内存设置
- MyEclipse加载Maven工程,pom.xml错误,maven-site-plugin
- Java 获取资源文件
- Java学习总结:飘逸的字符串
- java队列( 2015年9月24日)
- JAVA基础学习(十五)--集合二--TreeSet和泛型
- java线程对异常的处理
- Java获取文件后缀名