自定义注解格式
示例
@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());
}
}