示例为自定义类注解使用 type 值获取对应类下的方法执行后的结果返回。
@Inherited
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationClassAnnotation {
UserOperationType operationType() default UserOperationType.ONE_CODE;
}
使用枚举类
@Getter
@AllArgsConstructor
public enum UserOperationType {
ONE_CODE("1", "1"),
TWO_CODE("2", "2"),
THREE_CODE("3", "3"),
FOUR_CODE("4", "4"),
;
private String key;
private String value;
}
使用策略
@Autowired
private ApplicationContext applicationContext;
private Map userOperationProcessorMap = null;
private void initProcessor() {
//获取到使用了OperationClassAnnotation注解的类
Map controllers = applicationContext.getBeansWithAnnotation(OperationClassAnnotation.class);
for (Map.Entry entry : controllers.entrySet()) {
Object value = entry.getValue();
Class> aClass = value.getClass();
if (UserOperationProcessor.class.isAssignableFrom(aClass)) {
// 获取当前对象 声明的注解 获取到注解后 还可以获取注解中的属性
OperationClassAnnotation operationClassAnnotation = aClass.getAnnotation(OperationClassAnnotation.class);
userOperationProcessorMap.put(operationClassAnnotation.operationType().getKey(), (UserOperationProcessor) value);
}
}
}
/**
* 根据key值获取处理类
*
* @param type
* @return
*/
private UserOperationProcessor getProcessorByType(String type) {
//初始化
if (CollectionUtils.isEmpty(userOperationProcessorMap)) {
userOperationProcessorMap = new HashMap<>();
initProcessor();
}
if (userOperationProcessorMap.containsKey(type)) {
return userOperationProcessorMap.get(type);
}
return null;
}
public Page findUserStatistics(ManPowerCommonQuery param) {
UserOperationProcessor processorByType = getProcessorByType(param.getQueryType());
if (Objects.isNull(processorByType)) {
return new Page(param.getCurrentPage(), param.getPageSize(), 0L);
}
return processorByType.getUserStatistics(param);
}
后续类实现 processorByType 接口下的 getUserStatistics 方法即可解析到对应类型方法。
自定义方法适用于 aop 切面方法监听,不建议使用于解析方法或者反射方法执行。
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RecordAnnotation {
NursingRecordOperationType saveLogType() default NursingRecordOperationType.ADD;
}
使用 AOP 切面示例
@Aspect
@Component
@Slf4j
public class RecordAspect {
private final NursingRecordLogService nursingRecordLogService;
public RecordAspect(NursingRecordLogService nursingRecordLogService) {
this.nursingRecordLogService = nursingRecordLogService;
}
@Pointcut("@annotation(com.depth.RecordAnnotation)")
private void saveLog() {
}
@Around("saveLog() && @annotation(recordAnnotation)")
public Object doSaveLog(ProceedingJoinPoint joinPoint, RecordAnnotation recordAnnotation) throws Throwable {
// 获取方法返回值
Object proceed = joinPoint.proceed();
// 返回值验证
if (Objects.isNull(proceed)) {
return null;
}
try {
// 保存类型枚举
NursingRecordOperationType nursingRecordOperationType = recordAnnotation.saveLogType();
// 构建日志信息保存
NursingRecordVo nursingRecordVo = new NursingRecordVo();
nursingRecordVo.setOperationType(nursingRecordOperationType);
。。。。。。
} catch (Exception e) {
log.error("日志保存失败.2--type:" + recordAnnotation.saveLogType().getValue(), e);
}
return proceed;
}
自定义字段注解用于 Vo 对象字典值赋值使用,可正向解析或逆向解析,字典值 -> 字典名称 或 字典名称 -> 字典值。
@Documented
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DictionaryField {
DictionaryVariable dictionaryType() default DictionaryVariable.SYS_SEX;
}
使用字典枚举类
@Getter
@AllArgsConstructor
public enum DictionaryVariable {
SYS_SEX("sys_sex", "性别(1:男2:女)"),
SYS_EDUCATION_TYPE("sys_education_type", "学历")
;
private String key;
private String value;
}
/**
* 性别
*/
@DictionaryField(dictionaryType = DictionaryVariable.SYS_SEX)
private String sexId;
渲染方法
/**
* 渲染字典属性值
* (仅能渲染本级与父级)
* @param params
* @param resolving 当此值不为空则为反解析
* @return void
* @throws
* @method initDictionaryField
* @version 1.0
*/
public void initDictionaryField(List params, Object... resolving) {
// 1. 获取注解属性中的字典组信息
Class dictionaryVariableClass = DictionaryVariable.class;
DictionaryVariable[] enumConstants = dictionaryVariableClass.getEnumConstants();
List dictionaryArr = new ArrayList<>();
for (int i = 0; i < enumConstants.length; i++) {
DictionaryVariable enumConstant = enumConstants[i];
dictionaryArr.add(enumConstant.getKey());
}
GostopResponseVo