学习Java自定义注解这一篇就够了

自定义注解格式

示例

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthCheck { //访问修饰符必须为public,不写默认为pubic;
	
    /**
     * 有任何一个角色
     * @return
     */
    String[] anyRole() default "";

    /**
     * 必须有某个角色
     * @return
     */
    String mustRole() default "";

}

Java注解属性格式:

  • 属性类型 属性名称() [default 属性值];
  • default 默认值可写可不写

实现自定义注解的2个基本要求

  • ①注解的作用范围 @Target
  • ②注解的生命周期 @Retention

什么是元注解

所谓的元注解:是Java5.0定义的注解,作用在注解上,并且为这个注解赋予了含义,当编译器扫描到这个注解时,就知道该注解是什么作用!

Java提供的元注解:

注解作用
@Target指定注解作用范围(比如说:该注解是作用在类上,还是方法,或者是属性上等等)
@Retention指定注解的生命周期(也就是注解的保留时间,是在编译器有效,还是运行时有效等等)
@Documented是一个标记注解,里面没有任何属性,用 @Documented 注解修饰的注解类会被 JavaDoc 工具提取成文档。(不常用,可选项)
@Inherited也是一个标记注解,没有定义属性,作用是为了表示该注解可以被继承(比如说:你自定义了一个A注解,并且使用了@Inherited修饰,然后在paren类使用了A注解,那么paren的所有子类都默认继承了注解A)。【不常用】

源码解读

@arget注解

在这里插入图片描述

可以看到,Target注解只是定义了一个Value属性,属性类型为 ElementType,来看一下ElementType有哪些值

public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,    //作用于类、接口、枚举
 
    /** Field declaration (includes enum constants) */
    FIELD,    //作用在字段上
 
    /** Method declaration */
    METHOD,    //作用在方法
 
    /** Formal parameter declaration */
    PARAMETER,  //形参
 
    /** Constructor declaration */
    CONSTRUCTOR,  //构造器
 
    /** Local variable declaration */
    LOCAL_VARIABLE,  //局部变量
 
    /** Annotation type declaration */
    ANNOTATION_TYPE, //注解类型声明
 
    /** Package declaration */
    PACKAGE, //包声明
 
    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,  //类型参数化的声明
 
    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE,  //把该注解当做一个类型来使用
 
    /**
     * Module declaration.
     *
     * @since 9
     */
    MODULE
}

@Retention

在这里插入图片描述

Retention也是只定义了一个value属性,该属性类型为: RetentionPolicy,我们再一起看看Retention有哪些值可以选择:

public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,  //表示该注解的生命周期只在编译阶段
 
    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS, //该注解被保留在class文件上
 
    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME //该注解生命周期在运行时
}

@Documented

在这里插入图片描述

可以看到,Documented注解只是一个标记注解,里面什么属性都没有声明,作用是:使用了该注解的类,会被 doc工具提取成参考文档

@Inherited

在这里插入图片描述

这个注解也是一个标记注解,表示该注解是可以被继承的

利用反射获取对象上的注解

自定义注解:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthCheck {

    /**
     * 有任何一个角色
     *
     * @return
     */
    String[] anyRole() default "";

    /**
     * 必须有某个角色
     *
     * @return
     */
    String mustRole() default "";

}

UserDemo类

@AuthCheck(mustRole = "admin")
public class UserDemo {
    private String username;
}

test

void contextLoads() throws ClassNotFoundException {
        UserDemo userDemo = new UserDemo();
        //获取userDemo的Class对象
        Class<? extends UserDemo> aClass = userDemo.getClass();
        //判断userDemo的Class对象上是否有AuthCheck注解
        if (aClass.isAnnotationPresent(AuthCheck.class)) {
            System.out.println("UserDemo类上配置了AuthCheck注解!");
            //获取该对象上AuthCheck类型的注解
            AuthCheck anno = aClass.getAnnotation(AuthCheck.class);
            System.out.println("mustRole :" + anno.mustRole());
        }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值