本文总结的注解annotation的创建、使用以及得到注解信息,本文总结的是类上的、方法上的和字段上的注解,创建的工程如下:
一、首先介绍创建注解所用到的元注解(先有个了解即可,可略过)
1、@Target表示该注解可以用到什么地方,可能得ElementType参数包括:
CONSTRUCTOR构造器的声明
FIELD域声明(我理解的就是字段,也就是变量)
LOCAL_VARIABLE局部变量声明
METHOD方法声明
PACKAGE包声明
PARAMETEY参数声明
TYPE类、接口或enum声明
@Retention表示需要在什么级别保存该注解信息,可选的RetentionPolicy参数包括:
SOURCE注解将被编译器丢弃
CLASS注解在class文件中可用,但会被VM丢弃
RUNTIME在运行期也可以保存注解,因此能够通过反射机制读取注解信息
@Document将此注解包含在文档中
@Inherited允许子类继承父类中的注解
二、类 级别注解的创建、使用以及得到信息
1、创建注解:
package com.wang.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 作用在类、接口、或enum声明上的注解 * @author HeJW * */ @Target(ElementType.TYPE)//作用在类、接口、或enum声明 @Retention(RetentionPolicy.RUNTIME) public @interface ClassAnnotation { public String name(); public String description() default "no description "; }
2、使用,也就是只能在类上面才能使用上面的注解:
@ClassAnnotation(name = "class name is annotationBean") public class AnnotationBean {.......}
3、得到注解信息
Class<?> cl = Class.forName("com.wang.annotation.bean.AnnotationBean"); //得到类上的annotation ClassAnnotation ca = cl.getAnnotation(ClassAnnotation.class); System.out.println(ca.name()); System.out.println(ca.description());
三、方法上的
1、定义注解:
package com.wang.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 作用在方法上的注解 * @author HeJW * */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MethodAnnotation { public String name(); public String description() default "no description"; }
2、使用,只能在方法上使用:
@MethodAnnotation(name = "method name is methodTest", description = "method's name") public void methodTest(){ System.out.println("test"); } @MethodAnnotation(name = "method name is getId", description = "get id") public int getId() { return id; }
3、得到信息:
//得到方法上的annotation //for( Method m : cl.getDeclaredMethods() ){ for( Method m : cl.getMethods() ){ MethodAnnotation ma = m.getAnnotation(MethodAnnotation.class); //有的方法上有annotation,有的方法上没有annotation if( ma != null ){ System.out.println(ma.name()); System.out.println(ma.description()); } }
四、变量上的
1、为了下一步的说明,定义两个地段上的注解:
package com.wang.annotation; 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 FieldAnnotation1 { public String name(); public String description() default "no description "; }
另一个与这个基本相同:
package com.wang.annotation; 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 FieldAnnotation2 { public int value(); public String type(); }
2、使用:
@FieldAnnotation1(name="attribute name is id") @FieldAnnotation2(value=99,type="int") private int id; @FieldAnnotation1(name="attribute name is desc", description="descript attribute") private String desc;
3、得到信息:
//得到属性上的annotation for ( Field field : cl.getDeclaredFields() ) { FieldAnnotation1 fa1 = field.getAnnotation(FieldAnnotation1.class); if(fa1 != null){ System.out.println(fa1.name()); System.out.println(fa1.description()); } FieldAnnotation2 fa2 = field.getAnnotation(FieldAnnotation2.class); if(fa2 != null){ System.out.println(fa2.value()); System.out.println(fa2.type()); } }
五、除了能够得到指定的注解信息外,还能够通过得到annotation数组的形式得到注解信息
//以获取属性上的annotation为例,获取注解的annotation数组 for ( Field field : cl.getDeclaredFields() ) { Annotation[] anns = field.getDeclaredAnnotations(); if( anns[0] instanceof FieldAnnotation1 ){ FieldAnnotation1 fa1 = (FieldAnnotation1)anns[0]; System.out.println(fa1.name()); System.out.println(fa1.description()); } }
六、使用注解和得到注解信息的 完整代码
1、使用注解的完整代码:
package com.wang.annotation.bean; import com.wang.annotation.ClassAnnotation; import com.wang.annotation.FieldAnnotation1; import com.wang.annotation.FieldAnnotation2; import com.wang.annotation.MethodAnnotation; /** * 使用自定义annotation * @author HeJW * */ @ClassAnnotation(name = "class name is annotationBean") public class AnnotationBean { @FieldAnnotation1(name="attribute name is id") @FieldAnnotation2(value=99,type="int") private int id; @FieldAnnotation1(name="attribute name is desc", description="descript attribute") private String desc; @MethodAnnotation(name = "method name is methodTest", description = "method's name") public void methodTest(){ System.out.println("test"); } @MethodAnnotation(name = "method name is getId", description = "get id") public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } }
2、得到注解信息的完整代码:
package com.wang.annotation.app; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import com.wang.annotation.ClassAnnotation; import com.wang.annotation.FieldAnnotation1; import com.wang.annotation.FieldAnnotation2; import com.wang.annotation.MethodAnnotation; /** * 测试类 * @author HeJW * */ public class AnnotationTestApp { public static void main(String[] args) throws Exception { Class<?> cl = Class.forName("com.wang.annotation.bean.AnnotationBean"); //得到类上的annotation ClassAnnotation ca = cl.getAnnotation(ClassAnnotation.class); System.out.println(ca.name()); System.out.println(ca.description()); //得到方法上的annotation //for( Method m : cl.getDeclaredMethods() ){ for( Method m : cl.getMethods() ){ MethodAnnotation ma = m.getAnnotation(MethodAnnotation.class); //有的方法上有annotation,有的方法上没有annotation if( ma != null ){ System.out.println(ma.name()); System.out.println(ma.description()); } } //得到属性上的annotation for ( Field field : cl.getDeclaredFields() ) { FieldAnnotation1 fa1 = field.getAnnotation(FieldAnnotation1.class); if(fa1 != null){ System.out.println(fa1.name()); System.out.println(fa1.description()); } FieldAnnotation2 fa2 = field.getAnnotation(FieldAnnotation2.class); if(fa2 != null){ System.out.println(fa2.value()); System.out.println(fa2.type()); } } //以获取属性上的annotation为例,获取注解的annotation数组 for ( Field field : cl.getDeclaredFields() ) { Annotation[] anns = field.getDeclaredAnnotations(); if( anns[0] instanceof FieldAnnotation1 ){ FieldAnnotation1 fa1 = (FieldAnnotation1)anns[0]; System.out.println(fa1.name()); System.out.println(fa1.description()); } } } }
相关推荐
SpringBoot核心配置与注解
swagger 常用注解学习总结
Spring 注解学习手札,个人学习Spring mvc的知识总结
个人学习交流博客:http://blog.csdn.net/sun_promise(我博客中的文章就有一部分是此笔记中总结的内容。) 本文档是基于毕向东老师的java基础视频和张孝祥老师的高薪技术视频和银行管理系统以及交通灯管理系统的...
JAVA 基础数据数据类型运算符(1.注释,2.标识符和关键字3.数据类型分类)
包含SpringBoot环境搭建、常用注解、与Mybatis整合创建多数据源的连接+Druid连接池配置、SpringBoot的热部署、SpringBoot整合Freemarker/Servlet组件/文件上传与下载、SpringBoot自定义拦截器以及将拦截器注册到容器...
花了10个小时总结的spring注解开发,与普通的xml对比总结,更加形象,包括IOC DI AOP 事务 持久层整合.... 为以后的springboot学习有很大帮助!!
关于UIViewPropertyAnimator的学习总结:1、自定义贝塞尔曲线(两种实现方法),2、系统的贝塞尔曲线,3、系统的阻尼振动,4、自定义阻尼振动,以及暂停、继续、反向、取消等动画效果的Demo,代码有详细的注释。
每个文件里都会有很多的注释信息,方面阅读和学习。 1.只需要下载解压点击即可运行 2.每个模块代码都有注释可用来学习 3.没有用到任何的组件,纯html+css+js编写。 4.适合于没有javascript基础或javascript基础不...
相关例子 博文链接:https://zwx622.iteye.com/blog/2064446
基于python实现的BP神经网络手写数字识别模型实验源码+详细注释+数据集+项目说明+实验结果及总结.7z 人工智能 课程作业 手写数字数据集 BP网络模型识别手写数字 反向传播(英语:Backpropagation,缩写为BP)是...
1、 数据集:155张脑部肿瘤切片MRI图像与98张正常MRI图像,以9:1的比例划分训练集验证集 2、 代码通过3种不同深度学习神经网络...本代码来源于本人学习过程当中测试总结,适合初学者入门;分享给大家,共同学习!
正确的姿势,学习的态度来刷 LeetCode:高效的代码、简洁的注释、精炼的总结。
这是自己总结的java注解 使用了思维导图,方便读取,快速阅读
该资料是关于安卓中binder机制的详细说明文档,自己学习了好久总结出来的,拿出来分享给大家,共同进步。文档中在一些比较难以理解的地方有自己的注释,有问题的地方还望大家指正。
「喜欢的自提」 可以直接运行的代码,里面是我在工作中用来学和和测试的代码,一般是得到想要的测试结果,就直接用于实战项目中...17、BIO、NIO、AIO、Reactor、netty相关学习总结和测试(客户端/服务端通信测试demo)
Python 涵盖python的基础语法 和 基本用法 适用于小白学习巩固和需要快速入手的小伙伴 效率高 一天即可看完,在花几天消化就变成自己的啦。
从最基础的linux语句开始,创建,删除,重命名,和对文档、文件权限的理解和解释等等等等更好的加快学习,最后还有有安装数据库和配置jdk的操纵。
单片机学习经典程序,都是本人在培训机构中弄出来的,外加一些本人的总结,有些是一般教材上没有的,有详细的注释语句。
请仅仅把标题标签用于标题文本。不要仅仅为了产生粗体文本而使用它们... 注释:浏览器会自动地在标题的前后添加空行。 注释:默认情况下,HTML 会自动地在块级元素前后添加一个额外的空行,比如段落、标题元素前后。