JAVA-注解

Java实现自定义注解开发
首先我们了解一下自定义注解的标准示例,注解类使用 @interface 关键字修饰,且在注解类上方声明注解相关信息,包含以下四种信息
@Documented – 注解是否将包含在JavaDoc中

@Retention – 什么时候使用该注解

@Target – 注解用于什么地方

@Inherited – 是否允许子类继承该注解
1.)@Retention – 定义该注解的生命周期
● RetentionPolicy.SOURCE : 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。
● RetentionPolicy.CLASS : 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式
● RetentionPolicy.RUNTIME : 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。
2.)Target – 表示该注解用于什么地方。默认值为任何元素,表示该注解用于什么地方。可用的ElementType 参数包括
● ElementType.CONSTRUCTOR: 用于描述构造器
● ElementType.FIELD: 成员变量、对象、属性(包括enum实例)
● ElementType.LOCAL_VARIABLE: 用于描述局部变量
● ElementType.METHOD: 用于描述方法
● ElementType.PACKAGE: 用于描述包
● ElementType.PARAMETER: 用于描述参数
● ElementType.TYPE: 用于描述类、接口(包括注解类型) 或enum声明
3.)@Documented – 一个简单的Annotations 标记注解,表示是否将注解信息添加在java 文档中。

4.)@Inherited – 定义该注释和子类的关系
@Inherited 元注解是一个标记注解,@Inherited 阐述了某个被标注的类型是被继承的。
如果一个使用了@Inherited 修饰的annotation 类型被用于一个class,则这个annotation 将被用于该class 的子类。
说明:自定义注解时,需要使用元注解对自定义注解进行修饰,因此元注解就是修饰注解的注解。Java提供的4种元注解如下:
在这里插入图片描述

自定义注解类的声明
@Target(value= {ElementType.TYPE,ElementType.METHOD,ElementType.FIELD}) 用于声明当前注解类的作用范围分别为 类 方法 属性
@Retention(value = RetentionPolicy.RUNTIME) 运行时保留该注解,可以通过反射读取注解信息

package org.hswebframework.web.demo.annotation;

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

/**
 * Created by Zhang on 2022/3/29.
 * 自定义注解类声明
 */
@Target(value= {ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    /*定义注解里面的参数信息*/
    String name();

    String value() default "default value";

    String path() default "default use的path";

    int num() ;

    String content() default "空的内容";
}

自定义注解使用
分别在类、方法、属性上使用注解信息

package org.hswebframework.web.demo.annotation;

@MyAnnotation(name = "类上面的名称", value = "类上面的注解", num = 5)
public class MyAnnotationTestDo {

    @MyAnnotation(name = "属性名name", value = "name", path = "属性路径", num = 1)
    private String name;

    @MyAnnotation(name = "年龄age", value = "18", path = "/user2", num = 3)
    private String age;

    @MyAnnotation(name = "方法名testAnno", value = "testAnno的Value", path = "方法访问路径", num =2 )
    public String testAnno(){
        return "successs!!!";
    }

    @MyAnnotation(name = "方法名getName", value = "getName的Value", path = "方法访问路径", num =01 )
    public String getName() {
        return name;
    }

    @MyAnnotation(name = "方法名setName", value = "setName", path = "方法访问路径", num =10 )
    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

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

读取注解信息(测试注解类)
这里通过反射读取注解信息,注解内容与对应的类、方法、属性对应。

package org.hswebframework.web.demo.annotation;

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

public class MyAnnotationTest {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException {
        Class clazz = Class.forName("org.hswebframework.web.demo.annotation.MyAnnotationTestDo");

        // 获取类注解信息
        MyAnnotation classAnno = (MyAnnotation) clazz.getAnnotation(MyAnnotation.class);
        System.out.println(classAnno.name()+ "---" + classAnno.value() + "---" + classAnno.path());

        // 获取所有方法注解信息 ps: 这里需要使用 isAnnotationPresent 判断方法上是否使用了注解
        Method[] allMethods = clazz.getDeclaredMethods();
        for (Method methodModel :allMethods) {
            if (methodModel.isAnnotationPresent(MyAnnotation.class)){
                MyAnnotation myAnnotation = methodModel.getAnnotation(MyAnnotation.class);
                System.out.println("遍历:当前方法名为:"+methodModel.getName()+" 的注解信息: ———" + myAnnotation.name() +"———" + myAnnotation.value() +"———" + myAnnotation.path());
                System.out.println(myAnnotation.num()+"————" + myAnnotation.content());
            }
        }

        //获取指定方法注解信息
        Method methodTest = clazz.getDeclaredMethod("testAnno");
        MyAnnotation methodAnnotest =  methodTest.getAnnotation(MyAnnotation.class);
        System.out.println( methodAnnotest.name()+"---"+methodAnnotest.value()+"---"+methodAnnotest.path());
        System.out.println(methodAnnotest.num()+"————" + methodAnnotest.content());


        //获取属性注解信息
        Field nameField =  clazz.getDeclaredField("name");
        MyAnnotation attrAnno = nameField.getAnnotation(MyAnnotation.class);
        System.out.println( attrAnno.name()+"---"+attrAnno.value()+"---"+attrAnno.path());
        System.out.println(attrAnno.num()+"————" + attrAnno.content());
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值