-
- 创建注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Key {
public String name() default "";
}
-
- 使用注解
@Key(name = "Description")
private String description;
-
- 业务逻辑实现
// 利用反射获取注解实现个性化业务
Field[] fields = getEntityClass().getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
Field _field = fields[j];
Key attr = _field.getAnnotation(Key.class);
if (attr != null) {
keys.add(attr.name());
}
}