skip to content

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 pageshow

explore

questions

152 · 6 sections

How do you declare an aspect with @Aspect and make Spring actually apply it? What steps are required?

level: juniorimportance: must knowfreq 80%
basics
~20 s

Put @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).

open as a page

What is a cross-cutting concern, and why does AOP exist to handle things like logging, security, and transactions?

level: juniorimportance: must knowfreq 80%
basics
~20 s

A 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.

open as a page

What does @EnableAspectJAutoProxy do in a Spring application?

level: juniorimportance: must knowfreq 70%
basics
~10 s

It 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.

open as a page

What is a named pointcut in Spring AOP, and how do you declare one with @Pointcut?

level: juniorimportance: must knowfreq 60%
basics
~10 s

A 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.

open as a page

Define the core AOP vocabulary: aspect, join point, pointcut, and advice. How do they relate?

level: middleimportance: must knowfreq 85%
basics
~20 s

An 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.

open as a page

What does the @annotation pointcut designator match in Spring AOP, and how is it different from designators like execution() or within()?

level: juniorimportance: must knowfreq 55%
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.

open as a page

What does the args() pointcut designator do in Spring AOP, and how is it different from execution()?

level: juniorimportance: must knowfreq 55%
basics
~20 s

args() 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.

open as a page

What does the bean() pointcut designator do in Spring AOP, and how do you use it?

level: juniorimportance: must knowfreq 55%
basics
~10 s

bean(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.

open as a page

What does the execution() pointcut designator match, and how would you read execution(public * com.app.service..*.*(..))?

level: juniorimportance: must knowfreq 62%
basics
~10 s

execution() 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.

open as a page

In Spring AOP, what kind of join points can your aspects actually advise?

level: juniorimportance: must knowfreq 68%
basics
~10 s

Only 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.

open as a page

What is @After advice in Spring AOP and when does it run?

level: juniorimportance: must knowfreq 58%
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.

open as a page

What does Spring's @AfterReturning advice do, and when does it run?

level: juniorimportance: must knowfreq 60%
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.

open as a page

What is @AfterThrowing advice in Spring AOP and when does it run?

level: juniorimportance: must knowfreq 65%
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.

open as a page

What is @Around advice in Spring AOP, and why must it call ProceedingJoinPoint.proceed()?

level: juniorimportance: must knowfreq 78%
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.

open as a page

What is @Before advice in Spring AOP and when does it run?

level: juniorimportance: must knowfreq 70%
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.

open as a page

Why do internal (self-invocation) method calls in a Spring bean skip @Transactional/@Cacheable advice, and what does setting exposeProxy=true do about it?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Spring 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.

open as a page

What is a CGLIB proxy in Spring, and how does it differ from a JDK dynamic proxy?

level: juniorimportance: must knowfreq 70%
basics
~10 s

A 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.

open as a page

What is a JDK dynamic proxy, and what does Spring require of a bean before it can create one for it?

level: juniorimportance: must knowfreq 70%
basics
~20 s

A 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.

open as a page

What is org.aopalliance.intercept.MethodInterceptor in Spring AOP, and how does its invoke() method work?

level: juniorimportance: must knowfreq 55%
basics
~10 s

MethodInterceptor 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.

open as a page

How does Spring decide between a JDK dynamic proxy and a CGLIB proxy when creating an AOP proxy for a bean?

level: juniorimportance: must knowfreq 72%
basics
~20 s

In 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.

open as a page

Which methods can Spring AOP proxies actually intercept, and which are silently skipped?

level: juniorimportance: must knowfreq 72%
basics
~10 s

Spring 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.

open as a page

What kinds of join points can proxy-based Spring AOP advise, and what can't it?

level: juniorimportance: must knowfreq 62%
basics
~10 s

Spring 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.

open as a page

What is self-invocation in Spring AOP, and why can it make @Transactional silently do nothing?

level: juniorimportance: must knowfreq 78%
basics
~20 s

When 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.

open as a page

Why does an internal self-invoked call to a @Transactional method sometimes run without a transaction?

level: middleimportance: must knowfreq 70%
basics
~10 s

Because @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.

open as a page

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.

level: middleimportance: must knowfreq 50%
basics
~20 s

The 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).

open as a page

Why does calling one @Transactional method from another method in the same class sometimes leave the transaction annotation ignored?

level: juniorimportance: must knowfreq 82%
basics
~10 s

Spring'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.

open as a page

What is Spring AOP and how does it apply an aspect to a bean at runtime?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Spring 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.

open as a page

Which Spring proxy-AOP limitations does AspectJ compile-time weaving remove, and why is it able to?

level: middleimportance: must knowfreq 55%
basics
~20 s

CTW 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.

open as a page

Walk through everything required to enable Spring Load-Time Weaving: the annotation, aop.xml, and the Java agent. What does each piece do?

level: middleimportance: must knowfreq 40%
basics
~10 s

Add @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.

open as a page

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?

level: middleimportance: must knowfreq 65%
basics
~20 s

Spring 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.

open as a page