-
Notifications
You must be signed in to change notification settings - Fork 49
Spring AOP AspectJ After Advice Example using @After Annotation
Ramesh Fadatare edited this page May 17, 2019
·
1 revision
In this Spring AOP example, we will learn to use AspectJ @After annotation in Spring-based applications. After advice methods annotated with @After that will execute after a join point execution, no matter how the execution ended.
In this example, We will create a simple spring boot application, add logging aspect and then invoke aspect methods based on pointcuts information passed in a @After annotation.
Below snippet shows usage of @After annotation:
package net.guides.springboot2.springaop.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Aspect for logging execution.
*
* @author Ramesh Fadatare
*
*/
@Aspect
@Component
public class LoggingAspect {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.*(..))")
public void logAfterAllMethods(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logAfterAllMethods() : " + joinPoint.getSignature().getName());
}
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.getEmployeeById(..))")
public void logAfterGetEmployee(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logAfterGetEmployee() : " + joinPoint.getSignature().getName());
}
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.addEmployee(..))")
public void logAfterAddEmployee(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logAfterCreateEmployee() : " + joinPoint.getSignature().getName());
}
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.deleteEmployee(..))")
public void logAfterDeleteEmployee(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logAfterCreateEmployee() : " + joinPoint.getSignature().getName());
}
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.getAllEmployees(..))")
public void logAfterGetAllEmployees(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logAfterCreateEmployee() : " + joinPoint.getSignature().getName());
}
}Let's demonstrate the usage of @After annotation with a complete step by step example.
Let's create a LoggingAspect using Java-based configuration. Here is an AspectJ annotated class and methods with pointcut information:
package net.guides.springboot2.springaop.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Aspect for logging execution.
*
* @author Ramesh Fadatare
*
*/
@Aspect
@Component
public class LoggingAspect {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.*(..))")
public void logAfterAllMethods(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logAfterAllMethods() : " + joinPoint.getSignature().getName());
}
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.getEmployeeById(..))")
public void logAfterGetEmployee(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logAfterGetEmployee() : " + joinPoint.getSignature().getName());
}
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.addEmployee(..))")
public void logAfterAddEmployee(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logAfterCreateEmployee() : " + joinPoint.getSignature().getName());
}
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.deleteEmployee(..))")
public void logAfterDeleteEmployee(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logAfterCreateEmployee() : " + joinPoint.getSignature().getName());
}
@After("execution(* net.guides.springboot2.springaop.service.EmployeeService.getAllEmployees(..))")
public void logAfterGetAllEmployees(JoinPoint joinPoint) {
LOGGER.debug("****LoggingAspect.logAfterCreateEmployee() : " + joinPoint.getSignature().getName());
}
}