Quick revision
The short answer
@Transactional stops working because the transaction proxy is created by a BeanPostProcessor, and a BeanPostProcessor only processes beans in its own application context. @EnableTransactionManagement lives in the root context, but the service is scanned in the servlet (child) context, so nothing there wraps it in a proxy. The fix is to split component scanning: controllers in the servlet context, services and repositories in the root context.
What to remember
In this answer, we saw that a classic Spring MVC application has a root context and a servlet context, and that BeanPostProcessors such as the transaction proxy creator only process beans of their own context. When the service is scanned in the servlet context, which has no transaction support registered, it is never proxied, and scanning in both contexts creates a second, unproxied copy that the controller then receives. Splitting the scan, controllers in the servlet context and everything else in the root, leaves one proxied service and makes @Transactional work again.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
@Transactional stops working because the transaction proxy is created by a BeanPostProcessor, and a BeanPostProcessor only processes beans in its own application context. @EnableTransactionManagement lives in the root context, but the service is scanned in the servlet (child) context, so nothing there wraps it in a proxy. The fix is to split component scanning: controllers in the servlet context, services and repositories in the root context.
Why this question is asked
Older Spring MVC applications, the ones with a web.xml or a WebApplicationInitializer, usually have two application contexts instead of one. Most of the time nobody notices, until an annotation that clearly sits on a method simply does nothing. Interviewers ask this because it tests whether we understand that Spring features such as transactions are added by container machinery, and that this machinery has a scope. In this answer, we will recap how the root and servlet contexts relate, see how @Transactional is applied, reproduce the problem, understand why the service is not proxied, look at the duplicate beans that often come with it, and then fix the scanning.
1A Quick Recap of Root and Servlet Contexts
An application context is the Spring container: the object that creates our beans, wires them together and manages their life cycle. A classic Spring MVC application has two of them. ContextLoaderListener creates the root context when the web application starts. Each DispatcherServlet, the front servlet that receives HTTP requests and routes them to controllers, then creates its own servlet context, sometimes called the web context.
The servlet context is a child of the root context. A child can see its parent's beans, so a controller defined in the servlet context can have a service from the root context injected. The parent cannot see the child's beans. The idea was that shared business beans live once in the root context, while each DispatcherServlet keeps its own web beans such as controllers, view resolvers and handler mappings.
- Servlet context (child)created by
DispatcherServlet: controllers, view resolvers, MVC config - Root context (parent)created by
ContextLoaderListener: services, repositories,DataSource, transaction config
2 layers, from the top down. Step through them one at a time.
The child can look up beans in the parent; the parent never sees the child.
2How @Transactional Gets Applied
@Transactional is only metadata. Something has to read it and add the behaviour. When we write @EnableTransactionManagement, Spring registers an infrastructure bean called a BeanPostProcessor, a hook that the container calls for every bean right after creating it. This particular post-processor, an auto proxy creator, looks at each new bean, and if the bean has @Transactional methods it returns a proxy, a wrapper object that starts and commits the transaction around each call, instead of the plain bean.
Here is the detail that matters: a BeanPostProcessor is registered with one context, and that context only calls it for the beans it creates itself. The root context's post-processors never see the child's beans, and the child's post-processors never see the root's beans. Post-processing is not inherited down the hierarchy the way bean lookups are.
3Reproducing the Problem
Here is a typical configuration that shows the bug. The root configuration enables transactions; the servlet configuration scans the whole application package.
Notice that WebConfig has no @EnableTransactionManagement, so the servlet context has no transaction post-processor at all. When it scans com.shop, it creates its own OrderService and never wraps it. The controller lives in the same servlet context and asks for an OrderService. Spring looks in the child first, finds the local plain one, and injects it. Every call runs without a transaction, and nothing fails at startup.
4Why There Are Two OrderService Beans
The configuration above scans com.shop twice, once per context. So there is an OrderService in the root context, correctly proxied because the root context has the post-processor, and a second OrderService in the servlet context, not proxied. Two singletons of a class we believe is a singleton is a bug of its own: any in-memory cache or counter inside the service exists twice, and the scheduled jobs or event listeners in it may run twice.
Because a lookup in the child finds the local bean before it asks the parent, the controller always gets the unproxied copy. The proxied one in the root context is used only by other root beans, which makes the behaviour look random: the same method is transactional when called from a scheduled job and not when called from a controller.
4 messages in order. Play them, or step through them one at a time.
The proxied OrderService in the root context is never reached from the controller.
5The Fix: Split the Scan
The rule is simple: the servlet context scans only web components, and the root context scans everything else. We express this with scan filters.
Now there is exactly one OrderService, in the root context, and it is a transactional proxy. The controller in the servlet context asks for it, does not find one locally, and gets the root's proxy through the parent. useDefaultFilters = false matters: without it, the include filter only adds to the default @Component filter and the servlet context would still pick up services.
Another valid fix is to drop the hierarchy entirely and load all configuration into one context, which is what Spring Boot does by default. Moving @EnableTransactionManagement into WebConfig would also proxy the child copy, but it keeps the duplicates, so it treats the symptom rather than the cause.
6Follow-up questions
Does the same problem affect other annotations?
Yes. Anything applied by a post-processor has the same scope: @Async, @Cacheable, @Scheduled, @PreAuthorize with method security, @Validated method validation and custom aspects declared with @EnableAspectJAutoProxy. Each works only in the context where its enabling annotation is registered. A common variant is @EnableAspectJAutoProxy in the root context with controller aspects that never fire, because controllers live in the child.
How can we confirm we have two instances?
Log the identity of the bean in a @PostConstruct method, for example with System.identityHashCode(this) and the context's display name. If we see the message twice at startup, the class was scanned twice. We can also inject the WebApplicationContext and compare getBean(OrderService.class) with getParent().getBean(OrderService.class); if they are different objects, there are duplicates.
Does Spring Boot have root and servlet contexts?
Not by default. A Spring Boot web application runs one application context that holds both the DispatcherServlet and all business beans, so this bug cannot happen there. It can reappear when we deliberately build a hierarchy with SpringApplicationBuilder, or when Actuator runs on a separate management port, which creates a child context for the management endpoints.
7Conclusion
In this answer, we saw that a classic Spring MVC application has a root context and a servlet context, and that BeanPostProcessors such as the transaction proxy creator only process beans of their own context. When the service is scanned in the servlet context, which has no transaction support registered, it is never proxied, and scanning in both contexts creates a second, unproxied copy that the controller then receives. Splitting the scan, controllers in the servlet context and everything else in the root, leaves one proxied service and makes @Transactional work again.
Open with
The cause is that BeanPostProcessors are scoped to their own application context. @EnableTransactionManagement registers an auto proxy creator in the root context, and that post-processor only wraps beans the root context creates.
Then, if they want more
In this application the servlet context, the child created by DispatcherServlet, also scans the service package. So the child creates its own OrderService, and since the child has no transaction post-processor, that copy is a plain object.
The controller lives in the child too, and bean lookup checks the local context before the parent, so the controller gets the unproxied copy and every call runs without a transaction.
There is usually a second symptom: the class exists twice, once proxied in the root and once plain in the child, so caches or scheduled methods in it run twice. The fix I would make is to split the scanning.
The servlet context scans with useDefaultFilters false and an include filter for @Controller, and the root context scans everything with an exclude filter for @Controller. Then there is exactly one service, in the root, proxied, and the controller receives it from the parent. I would verify it by logging TransactionSynchronizationManager.isActualTransactionActive inside the method.
Longer term, collapsing to a single context, as Spring Boot does, removes this whole class of bug.
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.
- Official documentation
- Spring Framework reference, Core technologies
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 "In a classic Spring MVC application with root and servlet contexts, @Transactional on a...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/1e288a295c/