HuntExams Academy logo
JavaAdvanced#spring

What is Spring AOP and what is it used for?

Answer

Aspect-Oriented Programming lets you modularize cross-cutting concerns (logging, security, transactions) into reusable aspects applied via pointcuts, without cluttering business logic with repetitive boilerplate.

Example

@Aspect
@Component
class LoggingAspect {
  @Before("execution(* com.app.service.*.*(..))")
  void logBefore(JoinPoint jp) { System.out.println("Calling: " + jp.getSignature()); }
}

Detailed explanation

Spring AOP is Spring’s support for Aspect-Oriented Programming: separating cross-cutting concerns from core business logic. A cross-cutting concern is behaviour needed across many operations, such as logging, authorisation, transaction management, auditing, or timing. Instead of repeating this code inside every service method, you define it separately and apply it to selected methods. This keeps business classes focused and makes shared policies easier to maintain. Spring’s declarative transaction management is a common practical application of AOP.

The main terms are aspect, advice, join point, and pointcut. An aspect groups related cross-cutting behaviour; advice is the action performed. A join point is a place where that action can run; Spring AOP supports method-execution join points on Spring-managed beans. A pointcut selects those executions. In the example, @Aspect identifies the aspect and @Component registers it as a bean through component scanning. The @Before advice runs before methods matched by execution(* com.app.service.*.*(..)): any return type, any method in a class directly within com.app.service, and any arguments. It does not include subpackages. JoinPoint exposes details such as the method signature and arguments.

Under the hood, Spring AOP normally wraps eligible beans in proxies. When another object invokes a method through the proxy, an interceptor chain executes matching advice and delegates to the target. JDK dynamic proxies expose interfaces, while class-based proxies subclass the target class. Class-based proxies cannot advise final methods, and private methods cannot be advised through either proxy mechanism. A call from one method to another on the same instance normally bypasses the proxy, so self-invocation does not trigger advice.

Spring provides @Before, @AfterReturning, @AfterThrowing, @After, and @Around advice. @After runs whether execution succeeds or fails; @Around can control execution using ProceedingJoinPoint.proceed(), inspect results, or handle exceptions. AOP support must be enabled, commonly through Spring Boot auto-configuration with the relevant dependency or @EnableAspectJAutoProxy. Interviewers expect you to explain both the reduction in boilerplate and the proxy limitations, and to distinguish Spring’s proxy-based approach from AspectJ weaving, which supports broader join points.

Key points to remember

  • Spring AOP modularises cross-cutting behaviour without embedding it in business methods.
  • An aspect contains advice; pointcuts select the method executions where advice applies.
  • Typical uses include logging, auditing, authorisation, transaction management, and performance timing.
  • Advice runs through Spring-managed proxies, using interface-based or class-based proxying.
  • Self-invocation bypasses advice; Spring AOP is not equivalent to full AspectJ weaving.

Common mistakes candidates make

  • Assuming @Aspect alone registers a bean and enables AOP infrastructure.
  • Expecting advice to run on self-invocation or on objects created directly with new.
  • Using @Around unnecessarily or forgetting proceed() when the target method should execute.

Likely follow-up questions

How is Spring AOP different from AspectJ?

Spring AOP uses runtime proxies and supports method-execution join points on Spring-managed beans. AspectJ weaves bytecode at compile time or load time and supports additional join points, including constructor execution and field access.

Why might @Transactional not work when one method calls another?

In the usual proxy-based configuration, a same-instance call does not pass through the Spring proxy, so the called method’s transactional advice is not applied. Moving the transactional operation into another Spring bean allows the call to cross a proxy boundary.

When should you use @Around instead of @Before?

Use @Before when you only need an action before execution, such as logging an entry. Use @Around when you need to surround or control execution, such as measuring elapsed time with proceed() inside a try/finally block.

Interview tip: Start with the cross-cutting concern definition, explain the logging example, and finish with proxy-based execution and the self-invocation limitation.

Related Interview Questions

1
JavaAdvanced#spring

What is @Transactional and how does it work in Spring?

Open
2
JavaBeginner#spring

What is the difference between application.properties and application.yml in Spring Boot?

Open
3
JavaBeginner#spring

What is the purpose of Spring Boot starters?

Open