19.Java 注解
2016-12-27 23:02
387 查看
19.Java注解
1.Java内置注解----注解代码
@Deprecated //不推荐使用的过时方法@Deprecated public void badMethod(){ System.out.println("I am a old function"); }
@Override //必须是覆盖父类(接口)的函数
@Override public String toString(){ return "override toString()"; }
@SuppressWarnings //关闭不恰当的编译期间警告
2.自定义注解
a.没有任何元素的注解---标记注解b.普通注解定义、普通注解的使用
/** * 是否是类 */ public @interface IsClass { String value(); boolean isClass() default true; }
解释:该注解有两个元素value和isClass,isClass元素的目的是标记对象是否为类,该元素默认为真。
当注解的元素都有默认值的时候,名为value的元素赋值时不用写名称
3.元注解
@Target---------------用于描述注解的使用范围eg:TYPE:用于描述类、接口(包括注解类型) 或enum声明
@Retention-----------用于描述注解的生命周期
SOURCE:在源文件中有效(即源文件保留)---编译时起作用
RUNTIME:在运行时有效(即运行时保留)----运行时起作用
CLASS:在class文件中有效(即class保留)[默认]
@Documented————被修饰的注解会被写到javadoc中(标记注解)
@Inherited------允许子类继承父类中的注解[注解自身时不可继承的]可以使用getAnnotations()反射获取父类被
@Target(ElementType.TYPE) @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) public @interface IsClass { String value(); boolean isClass() default true; }
说明:元注解是用来修饰注解的注解,限定了注解的注释范围,注解的生命周期【源码级,运行时级别,Class文件级】,是否可以继承,是否创建文档等
4.运行时注解
像Spring一类的注解都与运行时注解有很大的关系,下面是一个利用注解,获取二维表的表名称,表的行名称,表的列值三个属性4.1定义运行时注解
Table是二维表的表格名称Column是二维表的列
Key是二维表的行
import java.lang.annotation.*; /** * Created by yangyun on 2016/12/27. */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Column { String value(); } @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface Key{ String value(); } @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @interface Table { String value();//表格的名称 }
4.2使用注解并编写相关的注解信息获取类
Tables类中包含了我们所需要的信息,Table注解注解在Table类上,Key和Column注解在Field上GetInformation类是获取相关的注解信息的操作类,利用的是Java的反射机制
import java.util.List; /** * Created by yangyun on 2016/12/27. */ @Empty(field = "Not Empty") @IsClass("value属性的值") public class Annotation { public static void main(String[] args) { System.out.format("tableName:%s\n",GetInformation.getName(Tables.class)); try { System.out.format("key:%s\n",GetInformation.getKey(Tables.class)); } catch (NoSuchFieldException e) { e.printStackTrace(); } System.out.format("columns:%s\n",GetInformation.getCols(Tables.class)); } } @Table("students") class GetInformation{ public static String getName(Class<Tables> tablesClass){ String strTableName=null; if(tablesClass.isAnnotationPresent(Table.class)){ Table tableName=tablesClass.getAnnotation(Table.class);//表格名称 strTableName=tableName.value(); } return strTableName; } public static String getKey(Class<Tables> tablesClass) throws NoSuchFieldException { Field field=tablesClass.getDeclaredField("key");//获取private属性 String strKey=null; if(field!=null){ Key key=field.getAnnotation(Key.class); if(key!=null){ strKey=key.value(); } } return strKey; } public static List<String> getCols(Class<Tables> tablesClass){ List<String> columns=new ArrayList<String>(); Field[] fields=tablesClass.getDeclaredFields(); if(fields!=null) { for (Field field : fields) { if (field.isAnnotationPresent(Column.class)) { columns.add(field.getAnnotation(Column.class).value()); } } } return columns; } } @Table("Students") class Tables{ @Key("Id") private String key; @Column("name") private String col1; @Column("sex") private String col2; }
4.3 主要的API介绍(不细说属于反射的知识)
1.myClass.getAnnotations()---获取该类所有的注解
2.myClass.getAnnotation(Class annotationType)—获取特定注解类型
3.myClass.isAnnotationPresent(Class annotationType)—判断是否存在特定类型的注解
4.myClass.getDeclareAnnotations()— 获取该类的所有注解不包括从父类中继承的注解
参考文献
https://blog.zenfery.cc/archives/70.htmlhttps://blog.zenfery.cc/archives/71.html https://blog.zenfery.cc/archives/78.html
相关文章推荐
- Java 10个调试技巧
- JVM 优点与缺点的深入分析
- Java 类的生命周期详解
- java的mock测试框架
- springMVC请求映射全面分析
- 使用Mockito进行Java单元测试
- 说说初用 Mock 工具测试碰到的坑
- Eclipse快捷键
- Java Mocking入门—使用Mockito
- Mockito:一个强大的用于Java开发的模拟测试框架
- JAVASE02-Unit04: 集合框架 、 集合操作 —— 线性表
- struts2_hibernate5_spring4整合的一般步骤及其注意事项
- JAVA设计模式(策略模式)
- javax.mail.AuthenticationFailedException: 530
- Eclipse自动下载源码--Java Source Attacher插件
- 修改spring Boot启动时的默认图案Banner
- Spring4的知识应用总结(七)—— Spring整合Struts2框架
- java基础--32.集合框架 Collection
- Leetcode刷题记—— Search a 2D Matrix(在二维数组里查找)
- 性能优化一点总结