Aspect-Oriented Programming
Spring's proxy-based AOP: aspects, pointcuts, the advice types, JDK versus CGLIB proxies, and when to reach for real AspectJ weaving. Interviewers care because @Transactional, @Cacheable and method security are all just aspects underneath.
part ofSpring Frameworkoverview, primer and where to startread it →on this pageshowhide
explore
- AOP Model & the @AspectJ Style29 questions
- AOP Terminology & Cross-Cutting Concerns5 questions
- @EnableAspectJAutoProxy & Auto-Proxy Creator5 questions
- @Aspect Beans5 questions
- @Pointcut: Reusable Named Pointcuts5 questions
- Introductions (@DeclareParents)4 questions
- Schema-Based (XML) AOP Configuration5 questions
- Pointcut Designators29 questions
- execution() Designator5 questions
- within(), this(), target()5 questions
- @annotation, @within, @target, @args5 questions
- args() & Parameter Binding5 questions
- bean() Spring-Specific Designator5 questions
- Supported Designators in Spring AOP4 questions
- Advice Types & Join Points29 questions
- @Before Advice5 questions
- @AfterReturning Advice5 questions
- @AfterThrowing Advice5 questions
- @After (Finally) Advice4 questions
- @Around & ProceedingJoinPoint.proceed()5 questions
- JoinPoint vs ProceedingJoinPoint API5 questions
- Proxy Mechanics: JDK vs CGLIB27 questions
- JDK Dynamic Proxies5 questions
- CGLIB Subclass Proxies4 questions
- Proxy Choice & proxyTargetClass5 questions
- ProxyFactory, Advised & TargetSource5 questions
- AopContext.currentProxy() & exposeProxy4 questions
- Classic AOP API & AOP Alliance Advisors4 questions
- Proxy Limitations & Aspect Ordering19 questions
- Self-Invocation Bypasses the Proxy5 questions
- final / private / static Methods4 questions
- Aspect Ordering: @Order / Ordered5 questions
- Method-Execution-Only Join Points5 questions
- AspectJ Weaving & Framework Integrations19 questions
- Spring AOP vs Full AspectJ5 questions
- Load-Time Weaving (LTW)5 questions
- Compile-Time & Post-Compile Weaving4 questions
questions
152 · 6 sectionsHow do you declare an aspect with @Aspect and make Spring actually apply it? What steps are required?
basics
~20 sPut @Aspect on the class to mark it as an aspect, and also register it as a Spring bean (e.g. add @Component so it is scanned). Then enable AOP with @EnableAspectJAutoProxy (Spring Boot does this automatically).
What is a cross-cutting concern, and why does AOP exist to handle things like logging, security, and transactions?
basics
~20 sA cross-cutting concern is a responsibility (like logging, security, transactions) needed by many parts of an app but not part of any one's core job. AOP pulls that repeated code into one place instead of copying it everywhere.
What does @EnableAspectJAutoProxy do in a Spring application?
basics
~10 sIt turns on Spring's support for @AspectJ-style aspects. Spring then scans for @Aspect beans and automatically wraps matching beans in proxies so the aspect's advice runs around their methods.
What is a named pointcut in Spring AOP, and how do you declare one with @Pointcut?
basics
~10 sA named pointcut gives a pointcut expression a name so you can reuse it. You declare it by annotating an empty, void-returning method with @Pointcut("...expression..."); the method name becomes the pointcut's name.
Define the core AOP vocabulary: aspect, join point, pointcut, and advice. How do they relate?
basics
~20 sAn aspect is the module holding a cross-cutting concern. A join point is a point where code could run (in Spring, a method call). A pointcut is an expression selecting which join points. Advice is the code that runs at those matched points.
What does the @annotation pointcut designator match in Spring AOP, and how is it different from designators like execution() or within()?
basics
~10 s@annotation(SomeAnnotation) matches any method-execution join point where the method being called carries that annotation. Unlike execution()/within(), which match by name or package pattern, @annotation matches by the presence of an annotation on the method.
What does the args() pointcut designator do in Spring AOP, and how is it different from execution()?
basics
~20 sargs() matches join points where the method's arguments are of the given types at runtime. execution() matches on the static method signature. args() checks the actual runtime argument types and can also bind an argument into an advice parameter.
What does the bean() pointcut designator do in Spring AOP, and how do you use it?
basics
~10 sbean(name) limits advice to join points inside Spring beans whose bean name matches the given pattern. For example, bean(tradeService) advises only the bean named tradeService, no matter its type.
What does the execution() pointcut designator match, and how would you read execution(public * com.app.service..*.*(..))?
basics
~10 sexecution() matches method executions. That expression means: any public method, returning any type, in any class inside com.app.service or its sub-packages, with any name and any number of arguments.
In Spring AOP, what kind of join points can your aspects actually advise?
basics
~10 sOnly method executions. Spring AOP wraps beans in proxies, so an aspect can run before/after/around a public method call on a Spring bean — but not field reads/writes, constructors, or static blocks.
What is @After advice in Spring AOP and when does it run?
basics
~20 s@After is advice that runs after a matched method finishes — whether it returned normally or threw an exception. It behaves like a finally block, so it is used for cleanup. It cannot see the return value or the exception.
What does Spring's @AfterReturning advice do, and when does it run?
basics
~10 s@AfterReturning is AOP advice that runs after an advised method finishes successfully (returns normally). It does NOT run if the method throws an exception. It can read the returned value but cannot replace it.
What is @AfterThrowing advice in Spring AOP and when does it run?
basics
~10 s@AfterThrowing is an aspect advice method that runs only when the matched method (join point) exits by throwing an exception. If the method returns normally, it does not run.
What is @Around advice in Spring AOP, and why must it call ProceedingJoinPoint.proceed()?
basics
~20 s@Around wraps a method call. It runs code before and after the target method. You must call proceed() to actually run the target method; if you skip it, the target never executes and you return your own value instead.
What is @Before advice in Spring AOP and when does it run?
basics
~10 s@Before advice is aspect code that runs before a matched method (the join point) executes. It's used for things like logging entry or validating arguments. It runs before, then the real method still runs.
Why do internal (self-invocation) method calls in a Spring bean skip @Transactional/@Cacheable advice, and what does setting exposeProxy=true do about it?
basics
~20 sSpring advice like @Transactional lives on a proxy that wraps the bean. When one method calls another via this, the call bypasses the proxy, so no advice runs. Setting exposeProxy=true lets a method fetch the proxy and re-route the call so advice still applies.
What is a CGLIB proxy in Spring, and how does it differ from a JDK dynamic proxy?
basics
~10 sA CGLIB proxy is a runtime-generated subclass of your target class that overrides its methods to add behavior like transactions. Unlike a JDK dynamic proxy, it doesn't need the class to implement an interface.
What is a JDK dynamic proxy, and what does Spring require of a bean before it can create one for it?
basics
~20 sA JDK dynamic proxy is a class the JVM builds at runtime that implements the same interface(s) as your bean and forwards calls to it. Spring can only create one if the target implements an interface.
What is org.aopalliance.intercept.MethodInterceptor in Spring AOP, and how does its invoke() method work?
basics
~10 sMethodInterceptor is Spring's low-level 'around' advice. Its invoke(MethodInvocation) method runs your code, then calls invocation.proceed() to invoke the real target method, and can run more code after — like a wrapper around each call.
How does Spring decide between a JDK dynamic proxy and a CGLIB proxy when creating an AOP proxy for a bean?
basics
~20 sIn plain Spring's default: if the bean implements at least one interface, Spring makes a JDK dynamic proxy; if it implements no interface, Spring uses CGLIB, which builds a subclass at runtime. Spring Boot changes the default to always use CGLIB.
Which methods can Spring AOP proxies actually intercept, and which are silently skipped?
basics
~10 sSpring AOP can only intercept public or protected, non-final instance methods. Final methods, private methods, and static methods are not advised, so annotations like @Transactional or @Async on them silently do nothing.
What kinds of join points can proxy-based Spring AOP advise, and what can't it?
basics
~10 sSpring AOP only intercepts method executions on Spring beans. It cannot advise field reads/writes, constructors, static methods, or private methods — because it works by wrapping the bean in a proxy object.
What is self-invocation in Spring AOP, and why can it make @Transactional silently do nothing?
basics
~20 sWhen a bean calls its own method via this.method(), the call goes straight to the real object, not through Spring's proxy. Because advice like @Transactional lives on the proxy, it is skipped, so no transaction starts.
Why does an internal self-invoked call to a @Transactional method sometimes run without a transaction?
basics
~10 sBecause @Transactional is applied by the proxy. When a bean calls its own method with this.method(), the call goes straight to the target and skips the proxy, so the transactional advice never runs.
Explain what 'the aspect with the lowest order value has the highest precedence' means for the execution of before advice versus after advice across two aspects.
basics
~20 sThe lowest-value aspect is outermost, like the outer layer of an onion. On the way in, its @Before runs first. On the way out, its @After runs last. So before advice runs highest-precedence-first, and after advice runs highest-precedence-last (reversed).
Why does calling one @Transactional method from another method in the same class sometimes leave the transaction annotation ignored?
basics
~10 sSpring's @Transactional works through a proxy that wraps the bean. An internal call (this.method()) bypasses the proxy, so the transaction advice never runs. Only calls coming through the injected bean reference are intercepted.
What is Spring AOP and how does it apply an aspect to a bean at runtime?
basics
~20 sSpring AOP adds cross-cutting behavior (logging, transactions, security) by wrapping a Spring bean in a proxy. The proxy runs your advice before/after the real method call. It works only on Spring-managed beans and needs no extra build step.
Which Spring proxy-AOP limitations does AspectJ compile-time weaving remove, and why is it able to?
basics
~20 sCTW can advise final methods, private methods, constructors, and self-invoked calls, and even objects created with new. It works because ajc edits the class bytecode directly, so there is no proxy boundary that calls must pass through to be intercepted.
Walk through everything required to enable Spring Load-Time Weaving: the annotation, aop.xml, and the Java agent. What does each piece do?
basics
~10 sAdd @EnableLoadTimeWeaving on a config class, provide META-INF/aop.xml naming the aspects and packages to weave, and start the JVM with a java agent (-javaagent:spring-instrument.jar or aspectjweaver.jar) so classes can be transformed as they load.
Why does an @Transactional (or any advised) method fail to take effect when called from another method in the same class, and how do you fix it?
basics
~20 sSpring AOP advice runs in the proxy that wraps the bean. An internal call like this.save() goes straight to the real object and never touches the proxy, so the advice (transaction, cache, retry) is skipped. Fix: call through the proxy or use AspectJ.