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.html

https://blog.zenfery.cc/archives/71.html

https://blog.zenfery.cc/archives/78.html

最新文章

  1. redis 操作string 的测试
  2. Centos允许root远程登录设置
  3. Discuz论坛安全加固浅析
  4. ABAP 权限程序
  5. Mac OS X上用CoreCLR运行一个真正的.NET控制台程序
  6. 【HTTP】GET和POST的区别
  7. java关键字synchronized
  8. UIView设置少于四个的圆角
  9. java泛型 之 入门(interface)
  10. COJ 1102 - You Can Say 11 题解
  11. ubuntu中文论坛
  12. javascript 中arguments.callee 调用自身
  13. Python学习记录----IDE安装
  14. 两句话动态修改table数据并提交到后台
  15. PS+HTML测试
  16. Rythm.js 使用教程详解
  17. nginx URL重写
  18. vue插件移动框
  19. 一个CLR20r3 错误解决。
  20. Installing Jenkins as a Windows service

热门文章

  1. JAVA文档注释标签
  2. OnClick和OnClientClick的区别
  3. 做为一个前端工程师,是往node方面转,还是往HTML5方面转
  4. Excel&mdash;如何解决数组求和运算时行列不匹配产生的错误
  5. rpc-1-OSI模型
  6. IIS部署遇到的一些问题
  7. node.js下使用RSA加密事例(windows)
  8. JS判断网页是否在微信中打开/
  9. Thinkphp 第二篇:如何将一个外部项目导入到Thimkphp环境中
  10. EXTJS中grid的数据特殊显示,不同窗口的数据传递