自定义注解和使用反射解析注解的值

自定义注解 

@Target(ElementType.FIELD)确定注解的使用范围
@Retention(RetentionPolicy.RUNTIME)确定注解什么时候可以使用,runtime程序运行时生效可以通过反射,class字节码文件中生效,source源文件生效
 
package www.it.com.object.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 AttributeAnnotation {
    String value(); //定义的是注解的参数
    String type();
    int length();
}
package www.it.com.object.annotation;

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

/**
 * 自定义类注解
 * @Target()表示注解的使用范围
 * @Retention()注解再什么情况产生效果 runtime运行时产生效果,class字节码文件产生效果,source源文件产生效果
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ClassAnnotation {
    String table();
}

创建student类使用自己定义的注解

package www.it.com.object.annotation;

import jdk.nashorn.internal.objects.annotations.Setter;

/**
 * @author : wangjie
 * company : 石文软件有限公司
 * @date : 2020/5/19  14:29
 * 使用自己定义的注解
 */
@ClassAnnotation(table = "student")
public class Student {
    @AttributeAnnotation(value = "name", type = "string", length = 20)
    private String name;
    @AttributeAnnotation(value = "age", type = "string", length = 20)
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

使用反射解析自己定义注解的值

package www.it.com.object.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * @author : wangjie
 * company : 石文软件有限公司
 * @date : 2020/5/19  14:35
 * 使用反射解析注解
 */
public class ReflectAnnotation {
    public static void main(String[] args) {
        try {
            //获取student的字节码类
            Class clazz = java.lang.Class.forName("www.it.com.object.annotation.Student");
            Field[] declaredFields = clazz.getDeclaredFields();
            for (int i = 0; i < declaredFields.length; i++) {
                System.out.println(declaredFields[i].getName());
            }
            //获取字节码类的所有类注解的信息
            Annotation[] annotations = clazz.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println(annotation);
            }
            //通过指定类注解的字节码文件,获取特定类注解的值这里获取的就是注解@ClassAnnotation的值
            ClassAnnotation student = (ClassAnnotation) clazz.getAnnotation(ClassAnnotation.class);
            System.out.println(student.table());
            //获取指定属性注解的值
            Field age = clazz.getDeclaredField("age");
            AttributeAnnotation annotation = age.getAnnotation(AttributeAnnotation.class);
            System.out.println(annotation.length() + "===" + annotation.value() + "===" + annotation.type());
            //根据获取到的注解的值拼写sql
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

posted @ 2020-05-19 16:13  求知若渴的蜗牛  阅读(281)  评论(0)    收藏  举报