- 自定义注解实现
//注解
package com.example.test.service; import java.lang.annotation.*; @Target({ElementType.FIELD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Info { String value() default "tracy"; boolean isDelete(); public enum Color{ BULE,RED,GREEN}; Color fruitColor() default Color.GREEN; }//注解用于实体字段
package com.example.test.bean; import com.example.test.service.Info; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; @Data @Info(isDelete = true) public class UserBean { @JsonProperty(value = "编号",defaultValue = "1") private int id; @Info(fruitColor = Info.Color.RED ,isDelete = false) @JsonProperty(value = "姓名",defaultValue = "") private String name; @Info(fruitColor = Info.Color.GREEN ,isDelete = true) @JsonProperty(value = "密码",defaultValue = "") private String password; }2、获取注解的值进行过滤
//第一种 Class clazz = bean1.getClass(); Info infoAnno = (Info) clazz.getAnnotation(Info.class); System.out.println("person.name :" + infoAnno.value() + ",person.isDelete:" + infoAnno.isDelete()); //第二种 System.out.println(UserBean.class.getDeclaredAnnotation(Info.class).value());
-
运用场景1:
根据自定义注解的值,进行数据过滤(比如根据是否有该注解的字段进行数据导出)。
3、应用场景2:
登录拦截器是否配置注解的接口