一、背景

  最近在自己搞一个项目时,遇到可需要开发自定义注解的需求,对于没有怎么关注这些java新特性的来说,比较尴尬,索性就拿出一些时间,来进行研究下自定义注解开发的步骤以及使用方式。今天在这里记下,方便以后学习复习以及分享给有需要的小伙伴们~

二、注解基本概念

  什么是注解?

    注解就是某种注解类型的一个实例,我们可以用它在某个类上进行标注,这样编译器在编译我们的文件时,会根据我们自己设定的方法来编译类。

  注解的分类有哪些?

  

由上图可知:注解共分为:标记注解、标准元注解、一般注解三类。

注:Deprecated注解,除了多个删除线,并没有什么拦截功能。

  标准元注解详解

    标准元注解是自定义注解的注解,主要包含4个,都位于java.lang.annotation包中,我们创建自定义注解时会用到4个标准元注解。它们的名称以及含义如下:

      1. @Documented:用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。是一个标记注解,没有成员。

      2. @Inherited:是一个标记注解阐述了某个被标注的类型是被继承的。使用了@Inherited修饰的注解类型被用于一个class时该class的子类也有了该注解。

      3. @Retention:定义了该注解的生命周期:某些注解仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的注解可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为注解与class在使用上是被分离的)。使用这个元注解可以对自定义注解的“生命周期”进行限制。

      生命周期策略枚举

      RetentionPolicy.RUNTIME 注解会在class字节码文件中存在,在运行时可以通过反射获取到。

      RetentionPolicy.CLASS 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得。

      RetentionPolicy.SOURCE 注解仅存在于源码中,在class字节码文件中不包含。

      4. @Target:说明了注解所修饰的对象范围:注解可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。

       修饰范围枚举

        ElementType.CONSTRUCTOR  作用于构造器
        ElementType.FIELD  作用于域/属性
        ElementType.LOCAL_VARIABLE  用于描述局部变量
        ElementType.METHOD  作用于方法
        ElementType.PACKAGE   用于描述包
        ElementType.PARAMETER   用于描述参数
        ElementType.TYPE   用于描述类、接口(包括注解类型) 或enum声明,最常用

三、开发自定义注解demo

1.开发自定义类注解

 package com.hafiz.zhang.annotation;

 import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* @author hafiz.Zhang
* @Date 2016年5月18日 下午1:58:11
* @Description 自定义类注解
*/
@Documented //定义可以被文档工具文档化
@Retention(RetentionPolicy.RUNTIME)//声明周期为runtime,运行时可以通过反射拿到
@Target(ElementType.TYPE)//注解修饰范围为类、接口、枚举
public @interface ClassAnnotation {
public String name() default "defaultService";
public String version() default "1.1.0";
}

2.自定义方法注解

 package com.hafiz.zhang.annotation;

 import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import com.hafiz.zhang.annotation.en.MethodTypeEnum; /**
* @author hafiz.Zhang
* @Date 2016年5月18日 下午1:58:26
* @Description 自定义方法注解
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodAnnotation {
public String name() default "defaultName";
public MethodTypeEnum type() default MethodTypeEnum.TYPE1;
}

3.自定义域注解

 package com.hafiz.zhang.annotation;

 import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* @author hafiz.Zhang
* @Date 2016年5月18日 下午1:58:37
* @Description 自定义域注解
*/
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldAnnotation {
public String name() default "defaultName";
public String value() default "defaultValue"; }

4.方法类型枚举类

 package com.hafiz.zhang.annotation.en;

 /**
* @author hafiz.Zhang
* @Date 2016年5月18日 下午1:59:02
* @Description 方法类型枚举类
*/
public enum MethodTypeEnum {
TYPE1,TYPE2
}

5.测试注解Bean

 package com.hafiz.zhang.annotation.bean;

 import com.hafiz.zhang.annotation.ClassAnnotation;
import com.hafiz.zhang.annotation.FieldAnnotation;
import com.hafiz.zhang.annotation.MethodAnnotation;
import com.hafiz.zhang.annotation.en.MethodTypeEnum; /**
* @author hafiz.Zhang
* @Date 2016年5月18日 上午11:59:37
* @Description 测试使用的bean
*/
@ClassAnnotation(name="personBean", version="1.2.1")
public class Person {
@FieldAnnotation(name="description", value="This is my personal annotation")
private String description; public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
}
@MethodAnnotation(name="sayHello", type = MethodTypeEnum.TYPE2)
public void sayHello() {
System.out.println("Hello Annotation!");
}
}

6.自定义类注解测试类

 package com.hafiz.zhang.annotation.test;

 import com.hafiz.zhang.annotation.ClassAnnotation;
import com.hafiz.zhang.annotation.bean.Person; /**
* @author hafiz.Zhang
* @Date 2016年5月18日 上午11:56:34
* @Description 测试类注解
*/
public class TestClassAnnotation { private static Person person = new Person(); public static void main(String[] args) {
Class<?> clazz = person.getClass();
//因为注解是作用于类上面的,所以可以通过isAnnotationPresent来判断是否是一个具有指定注解的类
if(clazz.isAnnotationPresent(ClassAnnotation.class)) {
System.out.println("This is a class with annotation ClassAnnotation!");
//通过getAnnotation可以获取注解对象
ClassAnnotation annotation = clazz.getAnnotation(ClassAnnotation.class);
if(null != annotation) {
System.out.println("BeanName = " + annotation.name());
System.out.println("BeanVersion = " + annotation.version());
}else{
System.out.println("the annotation that we get is null");
}
}else{
System.out.println("This is not the class that with ClassAnnotation");
}
}
}

运行结果:

7.自定义方法注解测试类

 package com.hafiz.zhang.annotation.test;

 import java.lang.reflect.Method;

 import com.hafiz.zhang.annotation.MethodAnnotation;
import com.hafiz.zhang.annotation.bean.Person; /**
* @author hafiz.Zhang
* @Date 2016年5月18日 下午12:11:11
* @Description 测试方法注解
*/
public class TestMethodAnnotation { private static Person person = new Person(); public static void main(String[] args) throws Exception {
Class<?> clazz = person.getClass();
//因为是注解到method上的,所以首先要获取这个方法
Method method = clazz.getDeclaredMethod("sayHello");
if(method.isAnnotationPresent(MethodAnnotation.class)) {
System.out.println("===This is a method with a annotation:MethodAnnotation===");
//通过getAnnotation可以获取注解对象
MethodAnnotation annotation = method.getAnnotation(MethodAnnotation.class);
if(null != annotation) {
System.out.println("MethodName = " + annotation.name());
System.out.println("MethodType = " + annotation.type());
}else{
System.out.println("the annotation that we get is null");
}
}else{
System.out.println("This is not the class that with MethodAnnotation");
}
}
}

运行结果:

8.自定义域注解测试类

 package com.hafiz.zhang.annotation.test;

 import java.lang.reflect.Field;

 import com.hafiz.zhang.annotation.FieldAnnotation;
import com.hafiz.zhang.annotation.bean.Person; /**
* @author hafiz.Zhang
* @Date 2016年5月18日 下午12:17:49
* @Description 测试域注解
*/
public class TestFieldAnnotation { private static Person person = new Person(); public static void main(String[] args) throws Exception {
Class<?> clazz = person.getClass();
//因为是注解到Field上的,所以首先要获取这个字段
Field field = clazz.getDeclaredField("description");
//判断这个Field上是否有这个注解
if(field.isAnnotationPresent(FieldAnnotation.class)) {
System.out.println("===This is a field with annotation:FieldAnnotation!===");
//如果有这个注解,则获取注解类
FieldAnnotation annotation = field.getAnnotation(FieldAnnotation.class);
if(null != annotation){
System.out.println("before set the value is:" + person.getDescription());
//通过反射给私有变量赋值
field.setAccessible(true);
field.set(person, annotation.value());
System.out.println("after set the value is:" + person.getDescription());
}else{
System.out.println("the annotation that we get is null");
}
}else{
System.out.println("This is not the class that with FieldAnnotation");
}
}
}

运行结果:

附:demo项目结构图

以上就是本人对自定义注解开发的理解以及开发测试了,如有错误希望大家能够批评指正!

Java自定义注解开发的更多相关文章

  1. Java实现自定义注解开发

    Java实现自定义注解开发 一直都对注解开发挺好奇的,最近终于有时间自己实践了一把,记录一下 万一后期会用到呢 哈哈哈 首先我们了解一下自定义注解的标准示例,注解类使用 @interface 关键字修 ...

  2. java自定义注解注解方法、类、属性等等【转】

    http://anole1982.iteye.com/blog/1450421 http://www.open-open.com/doc/view/51fe76de67214563b20b385320 ...

  3. java自定义注解学习(二)_注解详解

    上篇文章,我们简单的实现了一个自定义注解,相信大家对自定义注解有了个简单的认识,这篇,这样介绍下注解中的元注解和内置注解 整体图示 内置注解 @Override 重写覆盖 这个注解大家应该经常用到,主 ...

  4. JAVA自定义注解 ------ Annotation

    日常开发工作中,合理的使用注解,可以简化代码编写以及使代码结构更加简单,下面记录下,JAVA自定义注解的开发过程. 定义注解声明类. 编写注解处理器(主要起作用部分). 使用注解. 相关知识点介绍, ...

  5. java自定义注解类

    一.前言 今天阅读帆哥代码的时候,看到了之前没有见过的新东西, 比如java自定义注解类,如何获取注解,如何反射内部类,this$0是什么意思? 于是乎,学习并整理了一下. 二.代码示例 import ...

  6. java自定义注解实现前后台参数校验

    2016.07.26 qq:992591601,欢迎交流 首先介绍些基本概念: Annotations(also known as metadata)provide a formalized way ...

  7. java自定义注解知识实例及SSH框架下,拦截器中无法获得java注解属性值的问题

    一.java自定义注解相关知识 注解这东西是java语言本身就带有的功能特点,于struts,hibernate,spring这三个框架无关.使用得当特别方便.基于注解的xml文件配置方式也受到人们的 ...

  8. Java自定义注解的实现

    Java自定义注解的实现,总共三步(eg.@RandomlyThrowsException): 1.首先编写一个自定义注解@RandomlyThrowsException package com.gi ...

  9. Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性)

    Java自定义注解源码+原理解释(使用Java自定义注解校验bean传入参数合法性) 前言:由于前段时间忙于写接口,在接口中需要做很多的参数校验,本着简洁.高效的原则,便写了这个小工具供自己使用(内容 ...

随机推荐

  1. 在WordPress后台菜单系统中添加Home链接

    在wordpress后台如果想打开前台的话,要想先把鼠标移动到左上角菜单,然后在下拉菜单中点击“查看站点”,很是麻烦,能不能在 WordPress 后台菜单系统中添加 Home 链接呢? 将下面代码复 ...

  2. post提交表单

    <script type="text/javascript"> $(function () { $("#btnRefresh1").click(fu ...

  3. Android学习笔记(七)——常见控件

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 给我们提供了大量的 UI控件,下面我们简单试试几种常用的控件. 一.TextView 在布局文 ...

  4. JavaScript break跳出多重循环

    多重循环在编程中会经常遇到,那么在JavaScript中如何指定跳出那层的循环呢.其实这也是break的一个用法,下面是一个不错的例子,来自<JavaScript权威指南>,可以参考下: ...

  5. JavaScript获取onclick、onchange等事件值的代码

    这里主要是用到了getAttributeNode()这个方法,它获取的是属性节点,忽略属性和事件的差别,具体示例如下,感兴趣的朋友可以参考下哈希望对大家有所帮助 今天小菜处理下拉菜单级联问题时,想获取 ...

  6. (备忘)android模拟器摄像头模拟

    Camera分Front Camera和Back Camera 通常我们模拟后摄像头就可以了 三个选项 none:表示没有摄像头,打开摄像应用会崩溃 emulated:系统模拟一个动态的画面--在黑白 ...

  7. BZOJ 1461: 字符串的匹配

    Description 同上题. Sol KMP+树状数组. 写这题的时候我灰常naive...不管了...直接贴代码... Code /******************************* ...

  8. CodeVS 2845 排序的代价

    Description 给你一个数列使他递增,交换两个元素的代价为两个数的和,最小化代价. Sol 置换群+离散化. 使一个数列恢复递增顺序,那么,他和他要到达的位置的数需要交换,这样就形成了一个置换 ...

  9. 大数据热点问题TOP K

    1单节点上的topK (1)批量数据 数据结构:HashMap, PriorityQueue 步骤:(1)数据预处理:遍历整个数据集,hash表记录词频 (2)构建最小堆:最小堆只存k个数据. 时间复 ...

  10. 初识hibernate小案例

    使用hibernate前需要导入相关JAR包. 1.它可以接受词文法语言描述,并能产生识别这些语言的语句的程序 2.是一个Java的XML API,类似于jdom,用来读写XML文件的 3.支持注解配 ...