注解的基础
1.注解的定义:Java文件叫做Annotation,用@interface表示。
2.元注解:@interface上面按需要注解上一些东西,包括@Retention、@Target、@Document、@Inherited四种。
3.注解的保留策略:
@Retention(RetentionPolicy.SOURCE) // 注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
4.注解的作用目标:
@Target(ElementType.TYPE) // 接口、类、枚举、注解
@Target(ElementType.FIELD) // 字段、枚举的常量
@Target(ElementType.METHOD) // 方法
@Target(ElementType.PARAMETER) // 方法参数
@Target(ElementType.CONSTRUCTOR) // 构造函数
@Target(ElementType.LOCAL_VARIABLE) // 局部变量
@Target(ElementType.ANNOTATION_TYPE) // 注解
@Target(ElementType.PACKAGE) // 包
5.注解包含在javadoc中:
@Documented
6.注解可以被继承:
@Inherited
7.注解解析器:用来解析自定义注解。
示例:
1.自定义注解
package annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Inherited
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Validate {
public int min() default 1;
public int max() default 10;
public boolean isNotNull() default true; // 不可为空
}
2.在数据模型使用注解
package annotation;
public class User {
@Validate(min = 2, max = 5)
private String name;
@Validate(min = 1, max = 100)
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3.注解解析器
package annotation;
import java.lang.reflect.Field;
public class UserCheck {
public static boolean check(User user) {
if (user == null) {
System.out.println("!!校验对象为空!!");
return false;
}
// 获取User类的所有属性(如果使用getFields,就无法获取到private的属性)
Field[] fields = User.class.getDeclaredFields();
for (Field field : fields) {
// 如果属性有注解,就进行校验
if (field.isAnnotationPresent(Validate.class)) {
Validate validate = field.getAnnotation(Validate.class);
if (field.getName().equals("age")) {
if (user.getAge() == 0) {
if (validate.isNotNull()) {
System.out.println("!!年龄可空校验不通过:不可为空!!");
return false;
} else {
System.out.println("年龄可空校验通过:可以为空");
continue;
}
} else {
System.out.println("年龄可空校验通过");
}
if (user.getAge() < validate.min()) {
System.out.println("!!最小年龄检验校验不通过!!");
return false;
} else {
System.out.println("最小年龄检验校验通过");
}
if (user.getAge() > validate.max()) {
System.out.println("!!最大年龄检验校验不通过!!");
return false;
} else {
System.out.println("最大年龄检验校验通过");
}
}
if (field.getName().equals("name")) {
if (user.getName() == null) {
if (validate.isNotNull()) {
System.out.println("!!名字可空校验不通过:不可为空!!");
return false;
} else {
System.out.println("名字可空校验通过:可以为空");
continue;
}
} else {
System.out.println("名字可空校验通过");
}
if (user.getName().length() < validate.min()) {
System.out.println("!!名字最小长度校验不通过!!");
return false;
} else {
System.out.println("名字最小长度校验通过");
}
if (user.getName().length() > validate.max()) {
System.out.println("!!名字最大长度校验不通过!!");
return false;
} else {
System.out.println("名字最大长度校验通过");
}
}
}
}
return true;
}
}
4.测试
package annotation;
public class Test {
public static void main(String[] args) {
User user = new User();
user.setName("chen");
user.setAge(18);
System.out.println(UserCheck.check(user));
}
}
5.测试结果
名字可空校验通过
名字最小长度校验通过
名字最大长度校验通过
年龄可空校验通过
最小年龄检验校验通过
最大年龄检验校验通过
true