1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| @Aspect @Component @Slf4j public class AutoExecuteAspect { @Autowired private ApplicationEventPublisher eventPublisher; @Autowired private TaskExecutor taskExecutor;
@Before("@annotation(autoExecute)") public void beforeExecute(JoinPoint joinPoint, AutoExecute autoExecute) { String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); log.info("方法执行前:{}, 参数:{}", methodName, Arrays.toString(args)); executeAutoFunctions("before", methodName, args, autoExecute); }
@AfterReturning(value = "@annotation(autoExecute)", returning = "result") public void afterExecute(JoinPoint joinPoint, Object result, AutoExecute autoExecute) { String methodName = joinPoint.getSignature().getName(); log.info("方法执行后:{}, 结果:{}", methodName, result); executeAutoFunctions("after", methodName, result, autoExecute); }
@AfterThrowing(value = "@annotation(autoExecute)", throwing = "exception") public void afterThrowing(JoinPoint joinPoint, Exception exception, AutoExecute autoExecute) { String methodName = joinPoint.getSignature().getName(); log.error("方法执行异常:{}, 异常:{}", methodName, exception.getMessage()); executeAutoFunctions("error", methodName, exception, autoExecute); }
@Around("@annotation(autoExecute)") public Object aroundExecute(ProceedingJoinPoint joinPoint, AutoExecute autoExecute) throws Throwable { String methodName = joinPoint.getSignature().getName(); long startTime = System.currentTimeMillis(); try { executeAutoFunctions("before", methodName, joinPoint.getArgs(), autoExecute); Object result = joinPoint.proceed(); executeAutoFunctions("after", methodName, result, autoExecute); return result; } catch (Exception e) { executeAutoFunctions("error", methodName, e, autoExecute); throw e; } finally { long endTime = System.currentTimeMillis(); log.info("方法执行耗时:{}ms", endTime - startTime); executeAutoFunctions("finally", methodName, endTime - startTime, autoExecute); } }
private void executeAutoFunctions(String phase, String methodName, Object data, AutoExecute autoExecute) { for (String type : autoExecute.value()) { if (autoExecute.async()) { taskExecutor.execute(() -> { executeByType(phase, type, methodName, data); }); } else { executeByType(phase, type, methodName, data); } } }
private void executeByType(String phase, String type, String methodName, Object data) { switch (type) { case "log": logFunction(phase, methodName, data); break; case "cache": cacheFunction(phase, methodName, data); break; case "notification": notificationFunction(phase, methodName, data); break; case "audit": auditFunction(phase, methodName, data); break; default: log.warn("未知的自动执行类型:{}", type); } } private void logFunction(String phase, String methodName, Object data) { log.info("自动日志记录 - 阶段:{}, 方法:{}, 数据:{}", phase, methodName, data); } private void cacheFunction(String phase, String methodName, Object data) { log.info("自动缓存操作 - 阶段:{}, 方法:{}, 数据:{}", phase, methodName, data); } private void notificationFunction(String phase, String methodName, Object data) { log.info("自动通知发送 - 阶段:{}, 方法:{}, 数据:{}", phase, methodName, data); } private void auditFunction(String phase, String methodName, Object data) { log.info("自动审计记录 - 阶段:{}, 方法:{}, 数据:{}", phase, methodName, data); } }
|