0% found this document useful (0 votes)
3 views5 pages

Spring Aop Interview Qna - Chatgpt

The document provides a comprehensive overview of Spring AOP, including definitions of key concepts such as Aspect, JoinPoint, and Advice. It covers various types of advice, differences between Spring AOP and AspectJ, and practical examples of implementing AOP features like logging and exception handling. Additionally, it addresses common pitfalls and debugging tips for AOP in Spring applications.

Uploaded by

Joseph Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Spring Aop Interview Qna - Chatgpt

The document provides a comprehensive overview of Spring AOP, including definitions of key concepts such as Aspect, JoinPoint, and Advice. It covers various types of advice, differences between Spring AOP and AspectJ, and practical examples of implementing AOP features like logging and exception handling. Additionally, it addresses common pitfalls and debugging tips for AOP in Spring applications.

Uploaded by

Joseph Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Spring AOP Interview Questions and Answers (With Code

Examples)

Q1. What is AOP? Why is it used?


Answer: AOP (Aspect-Oriented Programming) is a programming paradigm
that allows separating cross-cutting concerns like logging, security, and
transaction management from business logic. It promotes cleaner, modular
code.

Q2. What are cross-cutting concerns?


Answer: These are concerns that affect multiple parts of an application,
such as logging, security, auditing, and transactions.

Q3. What are key AOP terminologies?


 Aspect: A module that encapsulates cross-cutting concerns
 JoinPoint: A point in execution (method call in Spring)
 Advice: The action taken at a JoinPoint
 Pointcut: A filter expression to match JoinPoints
 Weaving: Linking aspects with application code

Q4. What is the difference between JoinPoint and Pointcut?


Answer:
 JoinPoint: A specific point in the application (like method execution)
 Pointcut: An expression that matches one or more JoinPoints
@Before("execution(* com.example.service.*.*(..))")
public void logMethod(JoinPoint joinPoint) {
System.out.println("Method: " +
joinPoint.getSignature().getName());
}

Q5. Types of Advice?


 @Before
 @After
 @AfterReturning
 @AfterThrowing
 @Around
@AfterReturning("execution(* com.example..*(..))")
public void logAfterReturning() {
System.out.println("Method returned successfully.");
}

Q6. How does Spring AOP work internally?


Answer: Spring AOP uses proxy objects to wrap the original beans. The
proxy intercepts method calls and applies the advice.

Q7. What is a Proxy in AOP?


Answer: A proxy is an object that wraps the target object and intercepts
calls to add additional behavior like advice execution.

Q8. What is the difference between JDK Dynamic Proxy and CGLIB?
 JDK Proxy: Requires interfaces
 CGLIB Proxy: Works on concrete classes via subclassing
// JDK Proxy requires interface
public interface MyService {
void perform();
}

// CGLIB works on classes


@Service
public class MyConcreteService {
public void perform() {}
}

Q9. Can Spring AOP intercept private or static methods?


Answer: No. Spring AOP only intercepts public methods of Spring-
managed beans.

Q10. Difference between Spring AOP and AspectJ AOP?


Feature Spring AOP AspectJ
Proxy-based Yes No
Weaving time Runtime Compile/load-time
Targets Method execution Multiple joinpoints
Q11. How do you apply AOP to only selected methods or beans?
@Pointcut("execution(* com.example.service.UserService.*(..))")
public void userServiceMethods() {}

@Before("userServiceMethods()")
public void beforeUserService() {
System.out.println("Intercepted user service method");
}

Q12. Can you access method parameters in AOP?


@Before("execution(* com.example.service.*.*(String,..)) &&
args(name,..)")
public void logParams(String name) {
System.out.println("Parameter: " + name);
}

Q13. What is ProceedingJoinPoint?


Answer: Used in @Around advice to control when the actual method gets
executed.
@Around("execution(* com.example.service.*.*(..))")
public Object logExecutionTime(ProceedingJoinPoint pjp) throws
Throwable {
long start = System.currentTimeMillis();
Object result = pjp.proceed();
long time = System.currentTimeMillis() - start;
System.out.println("Execution time: " + time + "ms");
return result;
}

Q14. How to implement custom annotation like @TrackTime?


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TrackTime {}

@Around("@annotation(com.example.TrackTime)")
public Object trackTime(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
Object result = pjp.proceed();
System.out.println("Time taken: " + (System.currentTimeMillis() -
start));
return result;
}

Q15. How to order aspects?


@Aspect
@Order(1)
@Component
public class FirstAspect {}

@Aspect
@Order(2)
@Component
public class SecondAspect {}

Q16. How to log arguments and return values?


@Around("execution(* com.example.service.*.*(..))")
public Object logArgsAndReturn(ProceedingJoinPoint pjp) throws
Throwable {
System.out.println("Args: " + Arrays.toString(pjp.getArgs()));
Object result = pjp.proceed();
System.out.println("Returned: " + result);
return result;
}

Q17. How to use @AfterThrowing?


@AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))",
throwing = "ex")
public void handleException(Exception ex) {
System.out.println("Exception caught: " + ex.getMessage());
}

Q18. What are common pitfalls in Spring AOP?


 Internal method calls are not intercepted
 Final methods or classes can’t be proxied
 Forgetting @Aspect
 Not adding spring-boot-starter-aop

Q19. How to debug when AOP advice is not triggered?


 Check if the target is a Spring bean
 Ensure method is public
 Validate the pointcut expression
 Ensure @Aspect and @Component are both present

Q20. How have you used AOP in your project?


Answer:
 Logging entry/exit of service methods
 Tracking performance of critical methods
 Security checks
 Auditing API calls
 Retry logic and circuit breaker behavior

You might also like