Spring AOP Interview Questions and Implementation Guide
This article explains the concept of AOP, its typical use cases, the underlying mechanisms of Spring AOP (JDK dynamic proxy and CGLIB), the relationship with AspectJ, and provides a complete XML‑free code example that demonstrates aspects, pointcuts, join points and advice.
Aspect‑Oriented Programming (AOP) separates cross‑cutting concerns from core business logic, improving modularity and maintainability compared with traditional Object‑Oriented Programming.
Common AOP scenarios include logging, permission verification, performance monitoring (often implemented with annotations and aspects), and transaction management (Spring transactions are built on AOP).
Spring AOP is implemented using two proxy mechanisms: JDK dynamic proxies for classes that implement interfaces and CGLIB proxies (based on ASM) for concrete classes. Proxy creation occurs at runtime after bytecode generation, and the proxies are instantiated during Spring container initialization.
Spring AOP adopts the programming style of AspectJ; both aim to achieve the same AOP goals, with Spring AOP borrowing AspectJ’s syntax and concepts.
Below is a Maven‑style dependency snippet for adding Spring Context (version 5.0.8.RELEASE) to a project without any XML configuration:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>Three Java classes illustrate a minimal AOP setup: AopConfig (configuration class), IndexDao (a component with a simple method), and Test (a main class that loads the Spring context, retrieves the bean, and invokes the method). The console output is: dao----query The article then clarifies the four core AOP concepts: aspect (the modularized cross‑cutting concern), pointcut (a collection of join points, defined by an expression), join point (a specific method execution), and advice (the action taken at a join point). Weaving is the process of injecting the aspect logic into the target object.
Finally, a complete aspect class example shows how to declare a pointcut and associate @Before and @After advice methods with it:
/**
* Aspect definition – must be managed by Spring
*/
@Component
@Aspect
public class VingAspectJ {
/**
* Pointcut definition – represents a set of join points
*/
@Pointcut("execution(* com.ving.dao.*.*(..))")
public void pointCut() {}
/**
* After advice linked to the pointcut
*/
@After("com.ving.config.VingAspectJ.pointCut()")
public void after() {
System.out.println("after");
}
/**
* Before advice linked to the pointcut
*/
@Before("com.ving.config.VingAspectJ.pointCut()")
public void before() {
System.out.println("before");
}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
