自定义注解会需要元注解,此处先介绍元注解。
元注解
java中有四种元注解:@Retention、@Inherited、@Documented、@Target
@Retention
注解的保留位置(枚举RetentionPolicy),RetentionPolicy可选值:
- SOURCE:注解仅存在于源码中,在class字节码文件中不包含
- CLASS:默认的保留策略,注解在class字节码文件中存在,但运行时无法获得
- RUNTIME:注解在class字节码文件中存在,在运行时可以通过反射获取到
@Inherited
声明子类可以继承此注解,如果一个类A使用此注解,则类A的子类也继承此注解
@Documented
声明注解能够被javadoc等识别(下面自定义注解处会有例子做介绍,点击查看)
@Target
用来声明注解范围(枚举ElementType),ElementType可选值:
- TYPE:接口、类、枚举、注解
- FIELD:字段、枚举的常量
- METHOD:方法
- PARAMETER:方法参数
- CONSTRUCTOR:构造函数
- LOCAL_VARIABLE:局部变量
- ANNOTATION_TYPE:注解
- PACKAGE:包
自定义注解介绍
自定义注解使用场景
- 类属性自动赋值。
- 验证对象属性完整性。
- 代替配置文件功能,像spring基于注解的配置。
- 可以生成文档,像java代码注释中的@see,@param等
1和2这个没有做过类似实例,应该是像Hibernate中的使用注解映射Bean对象到数据库一样(此处为个人猜测,如有错误请留言指出),中间有检测和自动赋值。
自定义注解定义
定义一个自定义注解(不使用元注解):
public @interface Action{}
定义一个自定义注解(使用元注解,每个元注解都是可选,非必选):
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Target({ElementType.FIELD,ElementType.METHOD})
public @interface Action{
/**
* 操作类型
* @return
*/
//public String operateType() default "";
/**
* 方法描述
* @return
*/
public String description() default "no description";
/**
* 归属模块
* @return
*/
public SysAuditModelType ownermodel() default SysAuditModelType.NULL ;
/**
* 日志类型
* @return
*/
public String exectype() default "操作日志";
/**
* 详细信息
* @return
*/
public String detail() default "";
/**
* 执行顺序
*/
public ActionExecOrder execOrder() default ActionExecOrder.AFTER;
}
注解中可以声明成员方法,声明的成员方法为最终的注解里面的参数,成员方法可以使用default关键字设置默认值。上面的注解使用如:
@Action(description="图表导出word",execOrder=ActionExecOrder.AFTER,detail="图表导出word<#if fl>成功<#else>失败</#if>(W_QBD_FAL_PROBLEMRESET)")
自定义注解演示
新建一个自定义注解:
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Target({ElementType.FIELD,ElementType.METHOD})
@interface MyAnno{
public String name() default "zhangsan";
public String email() default "hello@example.com";
}
定义一个User类,来使用自定义注解:
class User{
@MyAnno(name = "zhang")
private String name;
@MyAnno(name = "zhang@example.com")
private String email;
@MyAnno(name = "sayHelloWorld")
public String sayHello(){
return "";
}
}
- 通过反射获取注解信息
下面我们通过反射来演示下获取属性的注解信息和方法的注解信息:
Method[] methods = User.class.getMethods();
//Field[] fields = User.class.getFields();
Field[] fields = User.class.getDeclaredFields();
/*
getFields:只能访问public属性及继承到的public属性
getDeclaredFields:只能访问到本类的属性,不能访问继承到的属性
getMethods:只能访问public方法及继承到的public方法
getDeclaredMethods:只能访问到本类的方法,不能访问继承到的方法
getConstructors:只能访问public构造函数及继承到的public构造函数
getDeclaredConstructors:只能访问到本类的构造函数,不能访问继承到的构造函数
*/
for (Field field : fields) {
MyAnno annotation = field.getAnnotation(MyAnno.class);
if(annotation!=null){
System.out.println("property="+annotation.name());
}
}
for (Method method : methods) {
MyAnno annotation = method.getAnnotation(MyAnno.class);
if(annotation!=null){
System.out.println("sayHello="+annotation.name());
}
}
输出如下:
property=zhang
property=zhang@example.com
sayHello=sayHelloWorld
原文:https://blog.csdn.net/bluuusea/article/details/79996572