Java的自定义注解及通过反射获取注解

一、注解基本知识

  1、元注解:@Retention @Target @Document @Inherited

  2、Annotation型定义为@interface, 所有的Annotation会自动继承java.lang.Annotation这一接口,并且不能再去继承别的类或是接口。

  3、参数成员只能用public或默认(default)这两个访问权修饰

  4、参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和StringEnumClassannotations等数据类型,以及这一些类型的数组。

  5、要获取类、方法和字段的注解信息,必须通过Java的反射技术来获取 Annotation对象,除此之外没有别的获取注解对象的方法

  6、注解也可以没有定义成员, 不过这样注解就没啥用了,只起到标识作用

自定义注解类时, 可以指定目标 (类、方法、字段, 构造函数等) , 注解的生命周期(运行时,class文件或者源码中有效), 是否将注解包含在javadoc中及是否允许子类继承父类中的注解, 具体如下:

  1、@Target 表示该注解目标,可能的 ElemenetType 参数包括:

    ElemenetType.CONSTRUCTOR 构造器声明
    ElemenetType.FIELD 域声明(包括 enum 实例)
    ElemenetType.LOCAL_VARIABLE 局部变量声明
    ElemenetType.METHOD 方法声明
    ElemenetType.PACKAGE 包声明
    ElemenetType.PARAMETER 参数声明
    ElemenetType.TYPE 类,接口(包括注解类型)或enum声明

  2、@Retention 表示该注解的生命周期,可选的 RetentionPolicy 参数包括

    RetentionPolicy.SOURCE 注解将被编译器丢弃
    RetentionPolicy.CLASS 注解在class文件中可用,但会被JVM丢弃
    RetentionPolicy.RUNTIME JVM将在运行期也保留注释,因此可以通过反射机制读取注解的信息

  3、@Documented 指示将此注解包含在 javadoc 中

  4、@Inherited 指示允许子类继承父类中的注解

二、在java中的使用

  2.1、定义注解

package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class MyAnnotation {
    /**
     * 注解类
     *
     * @author suguoliang
     *
     */
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyClassAnnotation {
        String uri();

        String desc();
    }

    /**
     * 构造方法注解
     *
     * @author suguoliang
     *
     */
    @Target(ElementType.CONSTRUCTOR)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyConstructorAnnotation {
        String uri();

        String desc();
    }

    /**
     * 方法注解
     *
     * @author suguoliang
     *
     */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyMethodAnnotation {
        String uri();

        String desc();
    }

    /**
     * 字段注解
     *
     * @author suguoliang
     *
     */
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyFieldAnnotation {
        String uri();

        String desc();
    }

    /**
     * 可以同时应用到类和方法上
     *
     * @author 尐蘇
     *
     */
    @Target({ ElementType.TYPE, ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyClassAndMethodAnnotation {
        // 定义枚举
        public enum EnumType {
            util, entity, service, model
        }

        // 设置默认值
        public EnumType classType() default EnumType.util;

        // 数组
        int[] arr() default { 3, 7, 5 };

        String color() default "blue";
    }
}

  2.2基本测试

package annotation;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import annotation.MyAnnotation.MyClassAndMethodAnnotation;
import annotation.MyAnnotation.MyClassAndMethodAnnotation.EnumType;
import annotation.MyAnnotation.MyClassAnnotation;
import annotation.MyAnnotation.MyConstructorAnnotation;
import annotation.MyAnnotation.MyFieldAnnotation;
import annotation.MyAnnotation.MyMethodAnnotation;

@MyClassAnnotation(desc = "The Class", uri = "com.sgl.annotation")
@MyClassAndMethodAnnotation(classType = EnumType.util)
public class TestAnnotation {
    @MyFieldAnnotation(desc = "The Class Field", uri = "com.sgl.annotation#id")
    private String id;

    @MyConstructorAnnotation(desc = "The Class Constructor", uri = "com.sgl.annotation#constructor")
    public TestAnnotation() {
    }

    public String getId() {
        return id;
    }

    @MyMethodAnnotation(desc = "The Class Method", uri = "com.sgl.annotation#setId")
    public void setId(String id) {
        this.id = id;
    }

    @MyMethodAnnotation(desc = "The Class Method sayHello", uri = "com.sgl.annotation#sayHello")
    public void sayHello(String name) {
        if (name == null || name.equals("")) {
            System.out.println("hello world!");
        } else {
            System.out.println(name + "\t:say hello world");
        }
    }

    public static void main(String[] args) throws Exception {
        Class<TestAnnotation> clazz = TestAnnotation.class;
        // 获取类注解
        MyClassAnnotation myClassAnnotation = clazz.getAnnotation(MyClassAnnotation.class);
        System.out.println(myClassAnnotation.desc() + "+" + myClassAnnotation.uri());

        // 获得构造方法注解
        Constructor<TestAnnotation> constructors = clazz.getConstructor(new Class[] {});// 先获得构造方法对象
        MyConstructorAnnotation myConstructorAnnotation = constructors.getAnnotation(MyConstructorAnnotation.class);// 拿到构造方法上面的注解实例
        System.out.println(myConstructorAnnotation.desc() + "+" + myConstructorAnnotation.uri());

        // 获得方法注解
        Method method = clazz.getMethod("setId", new Class[] { String.class });// 获得方法对象
        MyMethodAnnotation myMethodAnnotation = method.getAnnotation(MyMethodAnnotation.class);
        System.out.println(myMethodAnnotation.desc() + "+" + myMethodAnnotation.uri());

        // 获得字段注解
        Field field = clazz.getDeclaredField("id");// 暴力获取private修饰的成员变量
        MyFieldAnnotation myFieldAnnotation = field.getAnnotation(MyFieldAnnotation.class);
        System.out.println(myFieldAnnotation.desc() + "+" + myFieldAnnotation.uri());
    }
}

  2.3通过反射解析

package annotation;

import java.lang.reflect.Method;
import java.util.Arrays;

import annotation.MyAnnotation.MyClassAndMethodAnnotation;
import annotation.MyAnnotation.MyClassAndMethodAnnotation.EnumType;
import annotation.MyAnnotation.MyClassAnnotation;
import annotation.MyAnnotation.MyMethodAnnotation;

public class ParseAnnotation {
    /**
     * 解析方法注解
     *
     * @param clazz
     */
    public static <T> void parseMethod(Class<T> clazz) {
        try {
            T obj = clazz.newInstance();
            for (Method method : clazz.getDeclaredMethods()) {
                MyMethodAnnotation methodAnnotation = method.getAnnotation(MyMethodAnnotation.class);
                if (methodAnnotation != null) {
                    // 通过反射调用带有此注解的方法
                    method.invoke(obj, methodAnnotation.uri());
                }
                MyClassAndMethodAnnotation myClassAndMethodAnnotation = method
                        .getAnnotation(MyClassAndMethodAnnotation.class);
                if (myClassAndMethodAnnotation != null) {
                    if (EnumType.util.equals(myClassAndMethodAnnotation.classType())) {
                        System.out.println("this is a util method");
                    } else {
                        System.out.println("this is a other method");
                    }
                    System.out.println(Arrays.toString(myClassAndMethodAnnotation.arr()));// 打印数组
                    System.out.println(myClassAndMethodAnnotation.color());// 输出颜色
                }
                System.out.println("\t\t-----------------------");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static <T> void parseType(Class<T> clazz) {
        try {
            MyClassAndMethodAnnotation myClassAndMethodAnnotation = clazz
                    .getAnnotation(MyClassAndMethodAnnotation.class);
            if (myClassAndMethodAnnotation != null) {
                if (EnumType.util.equals(myClassAndMethodAnnotation.classType())) {
                    System.out.println("this is a util class");
                } else {
                    System.out.println("this is a other class");
                }
            }
            MyClassAnnotation myClassAnnotation = clazz.getAnnotation(MyClassAnnotation.class);
            if (myClassAnnotation != null) {
                System.err.println(" class info: " + myClassAnnotation.uri());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        parseMethod(TestAnnotation.class);
        parseType(TestAnnotation.class);
    }
}

原文地址:https://www.cnblogs.com/a591378955/p/8350499.html

时间: 2024-08-28 22:29:06

Java的自定义注解及通过反射获取注解的相关文章

自定义注解以及通过反射获取注解

一.自定义的注解 @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface jdbcConfig { String ip(); int port() default 3306; String database(); String encoding(); String username(); String pa

java反射获取注解并拼接sql语句

先建两个注解 分别为 Table 和 Column package com.hk.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE}) @Retention(Ret

java注解使用、java获取注解的属性、获取注解的字段值

一.前言 1.前面一直讲注解的基础知识,注解的定义,对于注解的使用几乎是一笔略过,本篇将着重讲讲注解的使用. 获取注解的属性,通过反射获取注解的属性值. 二.示例代码 1.定义一个注解,用于给 全局变量 field 字段 赋值 package com.haha.study.annotation.value;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.

JAVA自定义注解 和 运行时靠 反射获取注解,解决 shiro 注解型权限因子获取问题

项目的权限分配,采用的是RBAC的设计模式.后台配置权限的时候,需要获取到所有的权限因子. 不经让我想起YII框架的SRBAC模块,还有以前的一个ecshop改造系统的权限配置方式,都采用的是PHP的反射机制. 于是把PHP项目的经验带到JAVA项目中,发现PHP中的经验在java中,实现起来不是那么的方便. 这主要的原因:一是语言上的特性导致权限控制方面的差异性. 二是项目使用的是SSH框架,action的名称已经使用注解替换了原有名称.使用反射获取到的类名和方法名组合,不是有效的权限因子(当

Java自定义注解和运行时靠反射获取注解

Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明.配置的功能.注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用.包含在 java.lang.annotation 包中. 1.元注解 元注解是指注解的注解.包括  @Retention @Target @Document @Inherited四种. 1.1.@Retention: 定义注解的保留策略 @Retention(RetentionPolicy.SOURCE)   //注解仅存在于源码中,在c

Java 自定义注解及利用反射读取注解

一.自定义注解 元注解: @interface注解: 定义注解接口 @Target注解: 用于约束被描述的注解的使用范围,当被描述的注解超出使用范围则编译失败.如:ElementType.METHOD,ElementType.TYPE: @Retention 注解:用于约束被定义注解的作用范围,作用范围有三个: 1,.RetentionPolicy.SOURCE:作用范围是源码,作用于Java文件中,当执行javac时去除该注解. 2.RetentionPolicy.CLASS:作用范围是二进制

自定义注解,通过反射获得注解中的值(详细自定义注解解释)

自定义注解(@Alias): package com.nf.lc.demo3; import java.lang.annotation.*; /* 定义注解的生命周期元注解:@Retention RetentionPolicy.SOURCE 在编译阶段丢弃,编译结束没有任何意义 RetentionPolicy.CLASS 在类加载时丢弃,字节码文件处理时有用,默认这种方式 ☆ RetentionPolicy.RUNTIME 始终不会丢弃,运行时期也保留该注解,可以通过反射机制读取该信息 */ @

java 通过反射获取注解

package com.mine.practice.reflectfield; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import

Java中自定义注解

前言 随着springboot的流行,以前基于XML的spring配置用的越来越少,JavaConfig形式使用的越来越多,类似于: @Configuration public class AppConfig { @Bean(name="helloBean") public HelloWorld helloWorld() { return new HelloWorldImpl(); } } 可以看出更多的是基于注解(Annotation)实现的,包括springboot的入口类**Ap