Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

How does Spring apply features such as @Transactional, @Async and @Cacheable to your beans?

Spring applies @Transactional, @Async and @Cacheable by wrapping the bean in a proxy. After the bean is created and initialised, a BeanPostProcessor (the auto-proxy creator, or for @Async a dedicated post-processor) checks whether any of its methods match these annotations and, if so, returns a proxy in place of the raw object. Other beans receive the proxy, which runs the extra behaviour (start a transaction, hand the call to an executor, look in the cache) and then calls the real method, so only calls that go through the proxy get that behaviour, and only method executions can be advised.

Why this question is asked

We use these annotations every day, and most of the time they just work. The times they do not work, such as a self-call that skips the transaction or a private method that is never cached, all come from the same mechanism. Interviewers ask this question because once we understand that mechanism, a whole family of surprises stops being surprising. In this answer, we will recap what a proxy is, see where in the bean lifecycle Spring creates it, follow one call through the proxy for each of the three annotations, and then look at the two limits that follow from this design.

1A Quick Recap: Cross-Cutting Behaviour and Proxies

Transactions, asynchronous execution and caching are cross-cutting concerns: behaviour that many methods need but that is not the business logic of any of them. Spring keeps that behaviour out of our classes using aspect-oriented programming, AOP for short. The piece of behaviour is called advice, and the rule that says which methods get it is called a pointcut.

Spring's way of attaching advice is a proxy, a wrapper object generated at runtime that looks like our bean from the outside. In Spring Boot it is usually a CGLIB proxy, a generated subclass of our class; in some setups it is a JDK dynamic proxy that implements the same interfaces. Either way, the proxy has the same methods as our bean, and each method first runs the advice and then forwards the call to the real object, which Spring calls the target.

2Where Spring Creates the Proxy

Spring creates each bean in a fixed order: it instantiates the class, injects dependencies, runs initialisation callbacks such as @PostConstruct, and then gives every registered BeanPostProcessor a chance to replace the object. A BeanPostProcessor is a hook that receives each finished bean and may return a different object.

The auto-proxy creator is one of these hooks. When @EnableTransactionManagement or @EnableCaching is active (Spring Boot turns them on through auto-configuration), Spring registers advisors, pairs of a pointcut and an advice, such as one that matches methods annotated with @Transactional. In postProcessAfterInitialization the auto-proxy creator asks each advisor whether it matches any method of the bean. If at least one does, it builds a proxy around the bean and returns the proxy instead. @Async works the same way through its own AsyncAnnotationBeanPostProcessor, enabled by @EnableAsync.

Java
@Service
class ReportService {
@Transactional public void save(Report r) { ... }
@Cacheable("reports") public Report find(long id) { ... }
@Async public CompletableFuture<Void> email(long id) { ... }
}
// Somewhere with the application context:
ReportService bean = ctx.getBean(ReportService.class);
System.out.println(AopUtils.isAopProxy(bean)); // true
System.out.println(bean.getClass().getName()); // com.acme.ReportService$$SpringCGLIB$$0
System.out.println(AopUtils.getTargetClass(bean).getSimpleName()); // ReportService
Step throughHow a bean becomes a proxy
  1. Instantiate ReportServicethe constructor runs once
  2. Inject dependencies
  3. Run init callbacks@PostConstruct, afterPropertiesSet
  4. Does any advisor match a method?transaction, cache or async advisors
    YesWrap the bean in a proxyNoKeep the raw bean
  5. Register the result as the beanother beans are injected with it

5 steps in order. Play them, or step through them one at a time.

The auto-proxy creator runs after initialisation and may swap the bean for a proxy.

Notice that the object we get from the context is not our ReportService instance; it is a generated subclass with the same methods. Because the replacement happens inside the container before injection into other beans, every bean that depends on ReportService receives the proxy, never the raw object. The raw object still exists, held inside the proxy as its target.

3What the Proxy Does on Each Call

When another bean calls a method on the proxy, the proxy runs a chain of interceptors for that method and then calls the target. Each annotation has its own interceptor. For @Transactional, the TransactionInterceptor asks the transaction manager to begin a transaction, calls the target, and commits on return or rolls back on a runtime exception. For @Cacheable, the CacheInterceptor computes a key from the arguments and looks in the cache; on a hit it returns the cached value without calling the target at all, and on a miss it calls the target and stores the result. For @Async, the interceptor wraps the call in a task, submits it to a TaskExecutor and returns immediately, so the target runs later on another thread.

Step throughA call to a @Cacheable and @Transactional bean
CallerProxyInterceptorsTarget
find(42)
run the chain for find()
cache lookup: miss
find(42)
Report 42
store in cache
Report 42
find(42) again
cache hit, target not called

9 messages in order. Play them, or step through them one at a time.

The second call never reaches the target because the cache interceptor answers it.

So the three features look very different, but they share one shape: something happens before the target, around it, or instead of it, and that something lives entirely in the proxy.

4Limit 1: Only Calls Through the Proxy Are Advised

The proxy can only act on calls that reach it. When code inside ReportService calls one of its own methods, for example save() calling find(), the call is this.find(), and this is the target, not the proxy. The call goes straight to the method and no interceptor runs, so the cache is not consulted and no new transaction starts. This is the self-invocation problem, and it affects every proxy-based annotation in the same way.

The same limit explains why private methods are never advised: a subclass proxy cannot override a private method, so there is nothing for it to intercept. Final methods have the same problem with CGLIB proxies, and a JDK proxy only sees methods declared on the interfaces.

5Limit 2: Spring AOP Advises Method Executions Only

Because the proxy works by overriding or implementing methods, method execution is the only kind of join point, the only kind of moment in a program, that Spring AOP can advise. It cannot intercept reading or writing a field, running a constructor, or a static method, and it cannot advise objects that Spring did not create, such as entities built with new or loaded by Hibernate. Full AspectJ, which changes the bytecode itself at compile time or load time, can advise all of these, which is why Spring offers an AspectJ mode for the rare cases that need it. For transactions, caching and async work at service boundaries, the proxy model is simpler and enough.

6Follow-up questions

Is the proxy created for every bean?

No. The auto-proxy creator only wraps a bean when at least one advisor matches one of its methods. A bean with no matching annotations or pointcuts stays the raw object, which is why most beans in an application are not proxies.

What happens with circular dependencies and proxies?

If bean A needs a proxied bean B that is still being created, Spring hands out an early reference. The auto-proxy creator supports this through getEarlyBeanReference, so the early reference is already the proxy. Spring Boot disallows circular references by default since 2.6, which avoids most of these subtleties.

In what order do the interceptors run when a method has several annotations?

Each advisor has an order. By default the transaction and cache advisors use the lowest precedence, and they can be ordered with the order attribute on @EnableTransactionManagement and @EnableCaching. When order matters, for example a retry that must wrap the transaction, we set it explicitly.

7Conclusion

In this answer, we saw that Spring applies @Transactional, @Async and @Cacheable through proxies. After a bean is initialised, a BeanPostProcessor, usually the auto-proxy creator, checks the bean against its advisors and returns a proxy when something matches, so other beans are injected with the proxy. On each call the proxy runs interceptors that start transactions, consult caches or submit tasks, then calls the target. Two limits follow: only calls through the proxy are advised, and only method executions can be advised.

What to say in the interviewAbout 80 seconds aloud

Open with

Spring applies these annotations with proxies. When a bean is created, Spring instantiates it, injects its dependencies and runs its init callbacks, and then the BeanPostProcessors get a chance to replace it.

Then, if they want more

One of them is the auto-proxy creator: it checks the bean against the registered advisors, like the transaction advisor that matches @Transactional or the cache advisor for @Cacheable, and if any method matches, it returns a proxy instead of the raw bean. @Async does the same thing through AsyncAnnotationBeanPostProcessor. In Spring Boot the proxy is normally a CGLIB subclass.

Every other bean is injected with the proxy, so when they call a method, the proxy runs an interceptor chain first: the transaction interceptor begins a transaction and commits or rolls back, the cache interceptor checks the cache and may skip the target completely, and the async interceptor submits the call to an executor and returns.

Then it calls the target. Two consequences come from this design. Only calls that go through the proxy are advised, so a self-call on this skips everything, and private or final methods cannot be intercepted. And Spring AOP only advises method executions on Spring beans; for field access, constructors or objects Spring did not create, you need AspectJ weaving.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does Spring apply features such as @Transactional, @Async and @Cacheable to your...": https://scrivio-interview-library.netlify.app/spring-boot/aop/2cfad5dabf/

More in Aspect-oriented programming