@Aspect @Component public class AsyncPageAop { /** * 定义切入点,用于匹配带有 @PreProcess 注解的方法。 */ @Pointcut("@annotation(com.arpa.wms.common.asyncPage.AsyncPage)") public void AsyncPage() { } /** * 在方法调用前进行处理,修改请求参数。 * @param joinPoint 连接点对象 * @return 修改后的返回结果 * @throws Throwable 异常 */ @Around("AsyncPage() && @annotation(modifyReturnValueAnnotation)") public Object modifyParam(ProceedingJoinPoint joinPoint, AsyncPage modifyReturnValueAnnotation) throws Throwable { long startTime = System.currentTimeMillis(); // 1.获取请求参数 // Object[] args = joinPoint.getArgs(); // Object arg = args[0]; // // 2.查询selectType // Field selectTypeField = arg.getClass().getDeclaredField("selectType"); // selectTypeField.setAccessible(true); // Object selectTypeValue = selectTypeField.get(arg); System.out.println("Select Type: " + selectType); // JSONObject jsonData = JSONObject.parseObject(JSON.toJSONString(arg)); // System.out.println(jsonData.get("selectType")); // String selectType=String.valueOf(jsonData.get("selectType")); // 获取注解属性值 String annotationValue = modifyReturnValueAnnotation.value(); System.out.println("Annotation Value: " + annotationValue); // 根据入参修改返回值 if (AsyncPageEnum.RECORDS.getValue().equals(selectType)&&AsyncPageEnum.RECORDS.getValue().equals(annotationValue)) { System.out.println("判断完成进入方法执行:"+ (System.currentTimeMillis() - startTime) + "ms" + StringUtil.NEWLINE); Object result = joinPoint.proceed(); return result; }else if (AsyncPageEnum.RECORDS.getValue().equals(selectType)&&AsyncPageEnum.COUNT.getValue().equals(annotationValue)) { Object modelInstance =null; MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); // 被切的方法 Method method = methodSignature.getMethod(); // 返回类型 Class<?> methodReturnType = method.getReturnType(); System.out.println(methodReturnType); // 实例化 Map<String, Object> attributes = new HashMap<>(); attributes.put("total", 0L); modelInstance = methodReturnType.getDeclaredConstructor().newInstance(); setModelAttributes(modelInstance, attributes); System.out.println("判断完成返回count:"+(System.currentTimeMillis() - startTime) + "ms" + StringUtil.NEWLINE); System.out.println(modelInstance); return modelInstance; }else if (AsyncPageEnum.COUNT.getValue().equals(selectType)&&AsyncPageEnum.COUNT.getValue().equals(annotationValue)) { System.out.println("判断完成进入方法执行:"+(System.currentTimeMillis() - startTime) + "ms" + StringUtil.NEWLINE); Object result = joinPoint.proceed(); return result; } else if (AsyncPageEnum.COUNT.getValue().equals(selectType)&&AsyncPageEnum.RECORDS.getValue().equals(annotationValue)) { System.out.println("判断完成返回list:"+(System.currentTimeMillis() - startTime) + "ms" + StringUtil.NEWLINE); return new ArrayList<>(); } // 调用目标方法,并获取返回值 Object result = joinPoint.proceed(); return result; } public void setModelAttributes(Object model, Map<String, Object> attributeMap) throws Exception { Class<?> modelClass = model.getClass(); for (Map.Entry<String, Object> entry : attributeMap.entrySet()) { String propertyName = entry.getKey(); Object propertyValue = entry.getValue(); try { Field field = modelClass.getDeclaredField(propertyName); field.setAccessible(true); // 设置为可访问 field.set(model, propertyValue); // 给属性赋值 } catch (NoSuchFieldException e) { // 如果属性不存在,可以根据实际情况处理异常 e.printStackTrace(); } } } }
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AsyncPage { String value() default ""; }