@Aspect
public class LoggingAspect {
@Before("execution(* com.example.*.*(..))")
public void beforeMethodInvocation(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().toShortString();
System.out.println("Before invoking method: " + methodName);
}
@AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = "result")
public void afterMethodInvocation(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().toShortString();
System.out.println("After invoking method: " + methodName);
System.out.println("Method returned: " + result);
}
@AfterThrowing(pointcut = "execution(* com.example.*.*(..))", throwing = "exception")
public void afterMethodThrowException(JoinPoint joinPoint, Exception exception) {
String methodName = joinPoint.getSignature().toShortString();
System.out.println("Exception thrown in method: " + methodName);
System.out.println("Exception message: " + exception.getMessage());
}
}
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.12.6</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<forceAjcCompile>true</forceAjcCompile>
<aspectLibraries>
<aspectLibrary>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
@within(com.example.LoggingAspect)
public class MyClass {
//...
}
@annotation(com.example.LoggingAspect)
public void myMethod() {
//...
}