-
Notifications
You must be signed in to change notification settings - Fork 49
Spring AOP AspectJ @AfterReturning Annotation Example
Ramesh Fadatare edited this page May 17, 2019
·
1 revision
In this Spring AOP example, we will learn to use AspectJ @AfterReturning annotation in Spring-based applications. After returning advice methods annotated with @AfterReturning that will execute after a join point completes normally, meaning that the target method returns normally without throwing an exception.
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 @AfterReturning annotation.
Below snippet shows usage of @AfterReturning annotation:
@Aspect
@Component
public class LoggingAspect {
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
@AfterReturning("execution(* net.guides.springboot2.springaop.service.EmployeeService.*(..))")
public void logAfterReturningAllMethods() throws Throwable {
LOGGER.debug("****LoggingAspect.logAfterReturningAllMethods() ");
}
@AfterReturning(pointcut = "execution(* net.guides.springboot2.springaop.service.EmployeeService.getEmployeeById(..))", returning = "retVal")
public void logAfterReturningGetEmployee(Object retVal) throws Throwable {
LOGGER.debug("****LoggingAspect.logAfterReturningGetEmployee() ");
}
@AfterReturning("execution(* net.guides.springboot2.springaop.service.EmployeeService.addEmployee(..))")
public void logAfterReturningAddEmployee() throws Throwable {
LOGGER.debug("****LoggingAspect.logAfterReturningAddEmployee() ");
}
}