Java自定义注解

来源:互联网 发布:淘宝商品描述怎么写 编辑:程序博客网 时间:2024/06/08 20:06

要深入学习注解,我们就必须能定义自己的注解,并使用注解,在定义自己的注解之前,我们就必须要了解Java为我们提供的元注解和相关定义注解的语法。

package annotation;import static java.lang.annotation.ElementType.METHOD;import static java.lang.annotation.RetentionPolicy.RUNTIME;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 Minzhe Xu    2017年4月27日下午3:22:52 *  *@Target 表示该注解用于什么地方,可能的值在枚举类 ElemenetType 中,包括:   *ElemenetType.CONSTRUCTOR:构造器声明  *ElemenetType.FIELD :域声明(包括 enum 实例)  *ElemenetType.LOCAL_VARIABLE:局部变量声明  *ElemenetType.METHOD :方法声明  *ElemenetType.PACKAGE :包声明  *ElemenetType.PARAMETER :参数声明  *ElemenetType.TYPE:类,接口(包括注解类型)或enum声明  * *@Retention 表示在什么级别保存该注解信息。可选的参数值在枚举类型 RetentionPolicy 中,包括:  *RetentionPolicy.SOURCE :注解将被编译器丢弃  *RetentionPolicy.CLASS :注解在class文件中可用,但会被VM丢弃  *RetentionPolicy.RUNTIME VM:将在运行期也保留注释,因此可以通过反射机制读取注解的信息。  * *@Documented 将此注解包含在 javadoc 中 ,它代表着此注解会被javadoc工具提取成文档。在doc文档中的内容会因为此注解的信息内容不同而不同。相当与@see,@param 等。 * *@Inherited 允许子类继承父类中的注解。 */@Target({ElementType.TYPE,ElementType.CONSTRUCTOR,ElementType.METHOD,ElementType.PARAMETER,ElementType.FIELD})@Retention(RetentionPolicy.RUNTIME)public @interface TestA {    /**     * @interface用来声明一个注解,     * 其中的每一个方法实际上是声明了一个配置参数。     * 方法的名称就是参数的名称,返回值类型就是参数的类型(返回值类型只能是基本类型、Class、String、enum)。     * 可以通过default来声明参数的默认值。     */    String name();    int id() default 0;    Class gid();}

测试类

package annotation;import java.util.HashMap;import java.util.Map;/** *  * @author Minzhe Xu    2017年4月27日下午3:09:20 * */@TestA(gid = Long.class, name = "type")public class UserAnotation {    @TestA(gid = Long.class, name = "param",id=1)    private Integer age;    @TestA(gid = Long.class, name = "constract",id=2)    public UserAnotation(){    }    @TestA(gid = Long.class, name = "method",id=3)    public void test1(){        @SuppressWarnings("rawtypes")        Map m=new HashMap(0);    }    @TestA(gid = Long.class, name = "method3",id=5)    public void test2(@TestA(gid = Long.class, name = "inner_param",id=4) Integer a){    }}

通过反射的方式 使用注解

package annotation;import java.lang.annotation.Annotation;import java.lang.reflect.Constructor;import java.lang.reflect.Method;/** *  * @author Minzhe Xu    2017年4月27日下午3:39:17 * */public class ParseAnotation {    public static void parseTypeAnnotation() throws ClassNotFoundException{        Class clazz=Class.forName("annotation.UserAnotation");        Annotation[] annotations = clazz.getAnnotations();        for(Annotation annotation:annotations){            TestA testA=(TestA) annotation;            System.out.println("id="+testA.id()+";name="+testA.name()+";gid="+testA.gid());        }    }    public static void parseMethodAnnotation(){        Method[] methods = UserAnotation.class.getDeclaredMethods();        for(Method method:methods){            boolean hasAnnotation=method.isAnnotationPresent(TestA.class);            if(hasAnnotation){                TestA testA=method.getAnnotation(TestA.class);                System.out.println("id="+testA.id()+";name="+testA.name()+";gid="+testA.gid());            }        }    }    @SuppressWarnings("unchecked")    public static void parseConstractAnnotation(){        Constructor[] constructors = UserAnotation.class.getConstructors();        for(Constructor contructor:constructors){            boolean annotationPresent = contructor.isAnnotationPresent(TestA.class);//isAnnotationPresent方法来判断是否使用了某个注解            if(annotationPresent){                TestA testA = (TestA) contructor.getAnnotation(TestA.class);                System.out.println("id="+testA.id()+";name="+testA.name()+";gid="+testA.gid());            }        }    }    public static void main(String[] args) throws ClassNotFoundException {        parseTypeAnnotation();        parseMethodAnnotation();        parseConstractAnnotation();    }}

这里写图片描述

注解的使用是基于java反射的机理,同时一般在开发的时候 不建议使用注解,除非是在搭建项目,对框架进行开发时,采用注解,形成统一的风格

参考链接:
http://www.cnblogs.com/peida/archive/2013/04/26/3038503.html

http://blog.sina.com.cn/s/blog_93dc666c0101gzn5.html

0 0
原创粉丝点击