注解Annotation
元注解的作用就是负责注解其他注解。
Java定义了4个标准的 meta-annotation类型,它们被用来提供对其它 annotation 类型作说明。 • 这些类型和它们所支持的类在java.lang.annotation包中可以 找到
–@Target
– @Retention
– @Documented
– @Inherited
@Target
用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
@Target(ElementType.TYPE)——接口、类、枚举、注解
@Target(ElementType.FIELD)——字段、枚举的常量
@Target(ElementType.METHOD)——方法
@Target(ElementType.PARAMETER)——方法参数
@Target(ElementType.CONSTRUCTOR) ——构造函数
@Target(ElementType.LOCAL_VARIABLE)——局部变量
@Target(ElementType.ANNOTATION_TYPE)——注解
@Target(ElementType.PACKAGE)——包
@Retention
注解的保留位置
RetentionPolicy.SOURCE:
这种类型的Annotations只在源代码级别保留,编译时就会被忽略,在class字节码文>件中不包含。
RetentionPolicy.CLASS:
这种类型的Annotations编译时被保留,默认的保留策略,在class文件中存在,但JVM将会忽略,运行时无法获得。
RetentionPolicy.RUNTIME:
这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用。
@Document
说明该注解将被包含在javadoc中
@Inherited
说明子类可以继承父类中的该注解
自定义注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(value={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ownAnnotation {String studentName() default "";int age() default 0;int id() default -1; //String indexOf("abc") -1String[] schools() default {"清华大学","北京大学"};}
测试自定义注解的使用
/*** 测试自定义注解的使用**/
@ownAnnotation
public class Test {@ownAnnotation(age=19,studentName="老高",id=1001,schools={"北京大学","北京航空航天大学"})public void test(){}@ownAnnotation("aaaa")public void test2(){}}
@Target(value={ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface OwnTable {String value();
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(value={ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OwnField {String columnName();String type();int length();
}
@OwnTable ("tb_student")
public class OwnStudent{@OwnField(columnName="id",type="int",length=10)private int id;@OwnField(columnName="sname",type="varchar",length=10)private String studentName;@OwnField(columnName="age",type="int",length=3)private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
解析注解信息
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;/*** 使用反射读取注解的信息,模拟处理注解信息的流程* @author **/
public class Demo03 {public static void main(String[] args) {try {Class clazz = Class.forName("com.jmdf.test.annotation.OwnStudent");//获得类的所有有效注解Annotation[] annotations=clazz.getAnnotations();for (Annotation a : annotations) {System.out.println(a);}//获得类的指定的注解OwnTable own = (OwnTable) clazz.getAnnotation(OwnTable.class);System.out.println(own .value());//获得类的属性的注解Field f = clazz.getDeclaredField("studentName");OwnField ownField = f.getAnnotation(OwnField.class);System.out.println(ownField .columnName()+"--"+ownField .type()+"--"+ownField .length());//根据获得的表名、字段的信息,拼出DDL语句,然后,使用JDBC执行这个SQL,在数据库中生成相关的表} catch (Exception e) {e.printStackTrace();}}
}
注解Annotation相关推荐
- Java注解(Annotation)详解
转: Java注解(Annotation)详解 幻海流心 2018.05.23 15:20 字数 1775 阅读 380评论 0喜欢 1 Java注解(Annotation)详解 1.Annotati ...
- 一小时搞明白自定义注解(Annotation)
原文链接:http://blog.csdn.net/u013045971/article/details/53433874 什么是注解 Annotation(注解)就是Java提供了一种元程序中的元素 ...
- Android 自定义注解(Annotation)
现在市面上很多框架都有使用到注解,比如butterknife库.EventBus库.Retrofit库等等.也是一直好奇他们都是怎么做到的,注解的工作原理是啥.咱们能不能自己去实现一个简单的注解呢.注 ...
- java基础-注解Annotation原理和用法
转载自 http://www.wolfbe.com/detail/201608/265.html 在很多java代码中都可以看到诸如@Override.@Deprecated.@SuppressWar ...
- 让别人和自己看懂自己的程序代码?一文掌握Java单行多行、文档注释以及注解(Annotation)超详细的理解使用,IDEA注释注解快捷键和模板,提高程序代码更有可读性
文章目录 单行和多行注释 文档注释(Java特有) Annotation(注解)的理解 常见的Annotation示例 IDEA注释注解快捷键及模板 自定义 Annotation JDK 中的元注解 ...
- Java注解Annotation 完成验证
Java注解Annotation用起来很方便,也越来越流行,由于其简单.简练且易于使用等特点,很多开发工具都提供了注解功能,不好的地方就是代码入侵比较严重,所以使用的时候要有一定的选择性. 这篇文章将 ...
- java注释和注解_深入理解JAVA注解(Annotation)以及自定义注解
Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制.Java 语言中的类.方法.变量.参数和包等都可以被标注.注解可以看作是一种特殊的标记,在程序在编译或 ...
- 注解Annotation的IoC:从@Autowired到@Component
注解Annotation的IoC:从@Autowired到@Component 2017-01-23 目录 1 什么是注解 2 不使用注解示例 2.1 com.springioc.animal.M ...
- Enum枚举类|注解Annotation
Enum枚举类 ①枚举类和普通类的差别: 使用 enum 定义的枚举类默认继承了 java.lang.Enum 类 枚举类的构造器仅仅能使用 private 訪问控制符 枚举类的全部实例必须在枚举类中 ...
- java 注解: Annotation
java 注解: Annotation 普通注解 自定义注解 元注解 注解: 位于源码中,用来修饰程序的元素,但不会对被修饰的对象有直接的影响. 可增加程序的动态性. 普通注解 普通注解:为java代 ...
最新文章
- 从明天起,做一个幸福的人。喂马,砍柴,周游世界
- GridView 里的删除不起作用
- Nginx服务测试时的一些配置:wireshark、常用搜索URL格式、关闭防火墙、siege
- jstat和jmap使用
- ajax 成功回调函数,jQuery的阿贾克斯成功回调函数定义jQuery的阿贾克斯成功回调函数定义(jQuery ajax...
- boost::fibers模块实现multiple streams的测试程序
- mysql皮秒转成年月日时分秒_MySQL performance_schema 中 TIMER_*类字段 的易读转换
- 防止多次提交的几个比较
- 二分法查找的JAVA程序
- opencv 模式识别学习
- c语言从入门到秃头表情包,C语言从入门到入土表情包 - C语言从入门到入土微信表情包 - C语言从入门到入土QQ表情包 - 发表情 fabiaoqing.com...
- flutter 使用阿里图标
- oracle导出报错00091,exp报错:EXP-00091 Exporting questionable statistics
- 计算机三级网络技术知识点大全(九)
- 这个Python自动扫雷算法写完了,估计看懂的人十不存一了吧
- 可行解、最优解、基解、基可行解、基最优解
- Thinkbook14G2ITL笔记本重装系统遇到的问题?
- 《图形图像处理》— 使用matlab对图像进行二值化和灰度化处理
- 时序数据库之---InflxDB(实用篇)
- 关于apk软件的加固加壳和签名打包