xxxxxxxxxx
package dev.gayerie;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogAspect {
@Before("execution(public * dev.gayerie.*Service.*(..))")
public void log(JoinPoint joinPoint) {
System.out.printf("Appel de %s avec %d paramètres%n",
joinPoint.toShortString(),
joinPoint.getArgs().length);
}
}
xxxxxxxxxx
// pom.xml dependencies for AspectJ and Logging
<dependencies>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- Logging dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>
// ApplicationConfig.java for ApplicationContext configuration
@Configuration
@ComponentScan(basePackages = "com.example.myapp")
@EnableAspectJAutoProxy
public class ApplicationConfig {
}
// Custom annotation to use as PointCut trigger
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogThis {
}
// Service class as Joinpoint : Where the Aspected behavior is applied through annotation
@Service
public class MyService {
@LogThis
public void doSomething() {
System.out.println("Doing something...");
}
}
// The aspect class advises : The custom behavior that is applied to the JoinPoints through annotation calls
@Aspect
@Component
public class LoggingAspect {
@Pointcut("@annotation(com.example.myapp.annotation.LogThis)") // Setting up the annotation as pointcut
private void logThisMethods() {} // Root advise method
@Before("logThisMethods()") // this method will be called before root advise method
public void logBefore(JoinPoint joinPoint) {
System.out.println("Before executing " + joinPoint.getSignature().toShortString());
}
@After("logThisMethods()") // this method will be called after root advise method
public void logAfter(JoinPoint joinPoint) {
System.out.println("After executing " + joinPoint.getSignature().toShortString());
}
@Around("logThisMethods()") // FLEXIBLE: take care before, after and during exception while processing root advise method
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Before executing " + joinPoint.getSignature().toShortString());
Object result = joinPoint.proceed();
System.out.println("After executing " + joinPoint.getSignature().toShortString());
return result;
}
@AfterReturning(pointcut = "logThisMethods()", returning = "result") // this method will be called after root advise method
public void logAfterReturning(JoinPoint joinPoint, Object result) {
System.out.println("After returning from " + joinPoint.getSignature().toShortString() + ", result: " + result);
}
@AfterThrowing(pointcut = "logThisMethods()", throwing = "exception") // this method will take care any method exception
public void logAfterThrowing(JoinPoint joinPoint, Throwable exception) {
System.out.println("After throwing exception from " + joinPoint.getSignature().toShortString() + ", exception: " + exception.getMessage());
}
}
xxxxxxxxxx
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
xxxxxxxxxx
package dev.gayerie;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@EnableAspectJAutoProxy
@Configuration
@ComponentScan
public class Application {
public static void main(String[] args) throws InterruptedException {
try (AnnotationConfigApplicationContext appCtx =
new AnnotationConfigApplicationContext(Application.class)) {
// ...
}
}
}
xxxxxxxxxx
package dev.gayerie;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Supervision {
int dureeMillis();
}
xxxxxxxxxx
@Aspect
@Component
public class LoggingAspect
{
@Around("execution(* com.howtodoinjava.aop..*(..)))")
public Object profileAllMethods(
ProceedingJoinPoint proceedingJoinPoint) throws Throwable
{
MethodSignature methodSignature =
(MethodSignature) proceedingJoinPoint.getSignature();
//...
}
}
xxxxxxxxxx
package dev.gayerie.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class SupervisionAspect {
@Around("@annotation(supervision)")
public Object superviser(ProceedingJoinPoint joinPoint, Supervision supervision)
throws Throwable {
long maxDuree = supervision.dureeMillis();
long start = System.currentTimeMillis();
try {
return joinPoint.proceed(joinPoint.getArgs());
} finally {
long end = System.currentTimeMillis();
long duree = end - start;
if (duree > maxDuree) {
System.out.printf("Attention l'appel à %s à durée %dms soit %dms de plus qu'attendu%n",
joinPoint.toShortString(), duree, duree - maxDuree);
}
}
}
}
xxxxxxxxxx
package dev.gayerie;
import org.springframework.stereotype.Service;
@Service
public class BusinessService {
@Supervision(dureeMillis = 5)
public void doSomething() {
System.out.println("réalise un traitement important pour l'application");
// ...
}
}