美文网首页
深入学习java笔记-19.注解与反射

深入学习java笔记-19.注解与反射

作者: 笨鸡 | 来源:发表于2019-05-17 18:59 被阅读0次

从JDK5加入注解和反射后,java的编程方式具有了动态性,有了一点python语言那种编码的快感,来一个小Demo

MyAnnotationClass.java

package spring_annotation.annotation;

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

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotationClass {
//    String name();
    String name() default "";
//    String type();
    String type() default "";
}

MyAnnotationFiled.java

package spring_annotation.annotation;


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

@Target(value = ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotationFiled {

    String value() default "";

    String type() default "";

    int length() default 0;

}

TestObject.java

package spring_annotation.pojo;

import lombok.*;
import lombok.experimental.Accessors;
import spring_annotation.annotation.MyAnnotationClass;
import spring_annotation.annotation.MyAnnotationFiled;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@Builder
@MyAnnotationClass(name = "t_object", type = "innodb")
public class TestObject {

    @MyAnnotationFiled(value = "id", type = "int", length = 10)
    private Integer id;

    @MyAnnotationFiled(value = "name", type = "varchar", length = 20)
    private String name;

    @MyAnnotationFiled(value = "age", type = "int", length = 10)
    private Integer age;

}

Annotation.java

import org.junit.jupiter.api.Test;
import spring_annotation.annotation.MyAnnotationClass;
import spring_annotation.annotation.MyAnnotationFiled;
import spring_annotation.pojo.TestObject;

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

public class AnnotationTest {

    @Test
    public void test01() throws ClassNotFoundException, IllegalAccessException,
            InstantiationException, NoSuchFieldException {
        String path = "spring_annotation.pojo.TestObject";
        Class clazz = Class.forName(path);
        TestObject testObject = (TestObject) clazz.newInstance();
        System.out.println(testObject.setId(1).setName("zzz").setAge(20));

        Annotation[] declaredAnnotations = clazz.getDeclaredAnnotations();
        for (Annotation declaredAnnotation : declaredAnnotations) {
            System.out.println(declaredAnnotation);
        }

        MyAnnotationClass annotation = (MyAnnotationClass) clazz.getAnnotation(MyAnnotationClass.class);
        System.out.println(annotation.name()+"--->"+annotation.type());

        Field f = clazz.getDeclaredField("id");
        MyAnnotationFiled af = f.getAnnotation(MyAnnotationFiled.class);
        System.out.println(af.value()+"--->"+af.type()+"--->"+af.length());
    }
}

相关文章

  • 深入学习java笔记-19.注解与反射

    从JDK5加入注解和反射后,java的编程方式具有了动态性,有了一点python语言那种编码的快感,来一个小Dem...

  • 博客地址

    java注解-01、java注解-02、Git面试资源java反射-01、java反射-02、java反射-03为...

  • 2021校招 复习总结

    笔记导航: JAVA: 泛型 反射和动态代理 注解 JAVA多线程 ReentrantLock,Volatile,...

  • Java反射机制学习笔记

    上一篇《java注解学习笔记》中最后说到了注解的实现主要依赖java的反射机制,那么这一篇主要介绍一下java的反...

  • Java基础:反射

    反射注解动态代理相关阅读 Java基础:类加载器 Java基础:反射 Java基础:注解 Java基础:动态代理 ...

  • Spring注解原理探索(三)

    之 Java如何识别注解 关键词:Java 反射java.lang.reflect 包,实现反射功能的工具类。注解...

  • Java高级知识

    反射 sczyh30 深入解析Java反射(1) - 基础深入解析Java反射(2) - invoke方法深入探究...

  • Java(L3)-- Junit、反射、注解

    Junit、反射、注解 今天学习的是 Java 基础加强部分的内容,包括 Junit、反射、注解三部分内容。 Pa...

  • 解读注解使用

    一、注解:深入理解JAVA注解 要深入学习注解,我们就必须能定义自己的注解,并使用注解,在定义自己的注解之前,我们...

  • Java中的注解和反射

    个人博客http://www.milovetingting.cn Java中的注解和反射 注解 Java注解(An...

网友评论

      本文标题:深入学习java笔记-19.注解与反射

      本文链接:https://www.haomeiwen.com/subject/tifaaqtx.html