|
|
@@ -0,0 +1,49 @@
|
|
|
+package work.baiyun.chronicdiseaseapp.aspect;
|
|
|
+
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
+import org.aspectj.lang.annotation.Around;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 开发/测试用:为 Controller 层请求注入可配置的延迟(默认关闭)。
|
|
|
+ * 通过设置 application.properties 中的 `app.simulatedDelay.enabled=true` 来启用,
|
|
|
+ * 延迟时长可通过 `app.simulatedDelay.millis` 配置(毫秒)。
|
|
|
+ */
|
|
|
+@Aspect
|
|
|
+@Component
|
|
|
+public class RequestDelayAspect {
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(RequestDelayAspect.class);
|
|
|
+
|
|
|
+ @Value("${app.simulatedDelay.enabled:false}")
|
|
|
+ private boolean enabled;
|
|
|
+
|
|
|
+ @Value("${app.simulatedDelay.millis:1000}")
|
|
|
+ private long delayMillis;
|
|
|
+
|
|
|
+ // 切点:拦截所有 controller 包下的方法
|
|
|
+ @Pointcut("execution(* work.baiyun.chronicdiseaseapp.controller..*(..))")
|
|
|
+ public void controllerMethods() {}
|
|
|
+
|
|
|
+ @Around("controllerMethods()")
|
|
|
+ public Object aroundController(ProceedingJoinPoint pjp) throws Throwable {
|
|
|
+ if (!enabled) {
|
|
|
+ return pjp.proceed();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ Thread.sleep(delayMillis);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ logger.warn("Simulated delay interrupted", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.debug("Applied simulated delay {} ms to {}", delayMillis, pjp.getSignature().toShortString());
|
|
|
+ return pjp.proceed();
|
|
|
+ }
|
|
|
+}
|