add AOP for logging

This commit is contained in:
0samsung0 2025-05-20 18:40:35 +03:00
parent 8393b074fc
commit 0a43a7a311

View file

@ -1,27 +1,34 @@
package org.springframework.samples.petclinic; package org.springframework.samples.petclinic;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Aspect @Aspect
@Component @Component
public class LoggingAspect { public class LoggingAspect {
private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
//Pointcut: all methods in package "model" @Around("execution(* org.springframework.samples.petclinic..*.*(..))")
@Before("execution(* model.BaseEntity.getId())") public Object logMethodExecution(ProceedingJoinPoint joinPoint) throws Throwable {
public void logBeforeMethod(){ String methodName = joinPoint.getSignature().getName();
System.out.println("model.BaseEntity.getId..."); String className = joinPoint.getTarget().getClass().getSimpleName();
}
logger.info("Начало выполнения метода: {}.{}", className, methodName);
@Before("execution(* model.NamedEntity.getName())")
public void logBeforeMethod2(){ long startTime = System.currentTimeMillis();
System.out.println("model.NamedEntity.getName..."); Object result = null;
} try {
result = joinPoint.proceed();
@Before("execution(* model.Person.*())") return result;
public void logBeforeMethod3(){ } finally {
System.out.println("model.Person*"); long endTime = System.currentTimeMillis();
} logger.info("Завершение выполнения метода: {}.{} (время выполнения: {} мс)",
className, methodName, (endTime - startTime));
}
}
} }