【Java Annoration小解】注:本文均是在Spring项目下编写及测试(自带validation)
注解声明(简单,不定时更新)
// 一般注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnorationName {}
注解分类(一般关联Java反射)
- 一般注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnorationName {}
- 验证注解(依赖validation)
// 继承已有注解(@Pattern),注解可以被继承,但必须包含message,groups, payload
@Pattern(regexp = "1313897830", message = "继承的格式错误")
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
@Documented
public @interface Phone2 {
String message() default "";
Class>[] groups() default { };
Class extends Payload>[] payload() default { };
}// 无继承,通过Constraint指定对应的注解处理器(可以实现ConstraintValidator接口)
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PhoneValidate.class)
@Documented
public @interface Phone {
// 不可为空
boolean nullAble() default true;
// 长度必须为11位
int length() default 11;
String message() default "手机号码错误";
Class>[] groups() default { };
Class extends Payload>[] payload() default { };
}
重复注解(jdk1.8之后变化)
- 1.8 before
// 声明
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SimpleAnnoration {
String value() default "hi";
}// 声明重复注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SimpleAnnorations {
SimpleAnnoration[] value();
}// 使用
public class User {
@SimpleAnnorations(value = https://www.it610.com/article/{@SimpleAnnoration("w"), @SimpleAnnoration("l"), @SimpleAnnoration("z") })
private String name;
}
- 1.8 after
// 声明
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(SimpleAnnorations.class)
public @interface SimpleAnnoration {
String value() default "hi";
}// 声明重复注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SimpleAnnorations {
SimpleAnnoration[] value();
}// 使用
public class User {
@SimpleAnnoration("J")
@SimpleAnnoration("a")
@SimpleAnnoration("s")
private Integer age;
}
// 测试
@Test
public void duplicationTest() throws Exception {
Class> clazz = User.class;
Field[] fields = clazz.getDeclaredFields();
// Java 8 之前的重复注解做法
SimpleAnnorations simpleAnnorations = clazz.getDeclaredField("name").getAnnotation(SimpleAnnorations.class);
if (simpleAnnorations != null) {
for (SimpleAnnoration annoration : simpleAnnorations.value()) {
System.out.println("tag.value: " + annoration.value());
}
}Annotation[] annotations1 = clazz.getDeclaredField("age").getAnnotationsByType(SimpleAnnoration.class);
for (Annotation annotation : annotations1) {
SimpleAnnoration annoration2 = (SimpleAnnoration) annotation;
System.out.println("tag.value: " + annoration2.value());
}
}
// 结果
tag.value: w
tag.value: l
tag.value: ztag.value: J
tag.value: a
tag.value: s
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)