WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Framework · Advanced · question 48 of 100

Explain the difference between a JoinPoint, a Pointcut, and an Advice in Spring AOP.?

📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

In Spring AOP, JoinPoint, Pointcut, and Advice are three key concepts that help developers to implement cross-cutting concerns in their applications.

A JoinPoint is a point during the execution of a program, such as method call, constructor call, or exception handler execution. In Spring AOP, JoinPoints are identified by the framework and become the targets of weaving.

A Pointcut is a set of one or more JoinPoints where an Advice should be applied. Pointcut helps developers to specify what types of JoinPoints should be intercepted and executed with the Advice. For example, a Pointcut can include all the methods in a specific class or all the methods annotated with a certain annotation.

An Advice is a piece of code that runs at a specific JoinPoint in a programs execution. In Spring AOP, there are several types of advice, including BeforeAdvice, AfterAdvice, and AroundAdvice. BeforeAdvice runs before the JoinPoint executed; AfterAdvice runs after the JoinPoint executed, and AroundAdvice runs before and after the JoinPoint executed, allowing developers to customize how the JoinPoint behavior accordingly.

To better understand these concepts, let’s take a look at the following example:

@Aspect
public class LoggingAspect {
 
    @Pointcut("execution(* com.example.service.*.*(..))")
    public void serviceMethods() {}
    
    @Before("serviceMethods()")
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("Before method execution: " + joinPoint.getSignature());
    }
 
    @After("serviceMethods()")
    public void logAfter(JoinPoint joinPoint) {
        System.out.println("After method execution: " + joinPoint.getSignature());
    }
 
    @Around("serviceMethods()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("Before method execution: " + joinPoint.getSignature());
        Object result = joinPoint.proceed();
        System.out.println("After method execution: " + joinPoint.getSignature());
        return result;
    }
}

In the above code snippet, we have defined an Aspect called LoggingAspect. Within the LoggingAspect, we have defined a Pointcut ‘serviceMethods()‘ that matches all the methods in the ‘com.example.service‘ package.

We have also defined three Advices, Before, After, and Around, which are applied to Pointcut ‘serviceMethods()‘.

The Before advice logs a message before the JoinPoint is called. The After advice logs a message after the JoinPoint is completed successfully. The Around advice logs a message both before and after the JoinPoint is called, and in this case, it also intercepts the JoinPoint execution.

Overall, these three concepts are essential to implementing Aspect-Oriented Programming in Spring AOP, giving developers more control over their applications’ behavior and maintainability.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview — then scores it.
📞 Practice Spring Framework — free 15 min
📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All 100 Spring Framework questions · All topics