Quick revision
The short answer
After the change, Spring no longer subclasses the configuration class, so a @Bean method that calls dataSource() directly, for example new JdbcTemplate(dataSource()), now runs that method as plain Java and builds a second DataSource with its own pool, outside the container. The fix is to receive the bean as a parameter: @Bean JdbcTemplate jdbcTemplate(DataSource dataSource). Until then, the extra pool is also never closed at shutdown, because the container does not know it exists.
What to remember
In this answer, we traced two connection pools back to a @Bean method that called dataSource() directly, for example new JdbcTemplate(dataSource()). With proxyBeanMethods = false there is no CGLIB subclass to intercept that call, so it creates a second DataSource outside the container. We fixed it by injecting the bean as a method parameter, @Bean JdbcTemplate jdbcTemplate(DataSource dataSource), and saw that until then the extra pool is never closed at shutdown and is invisible to health checks and metrics, because the container does not manage it.
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.
After the change, Spring no longer subclasses the configuration class, so a @Bean method that calls dataSource() directly, for example new JdbcTemplate(dataSource()), now runs that method as plain Java and builds a second DataSource with its own pool, outside the container. The fix is to receive the bean as a parameter: @Bean JdbcTemplate jdbcTemplate(DataSource dataSource). Until then, the extra pool is also never closed at shutdown, because the container does not know it exists.
Why this question is asked
This is the classic bug that follows a well-meant cleanup. Someone reads that Spring Boot's auto-configurations use proxyBeanMethods = false for faster startup, applies it to the team's configuration classes, and the next day the database team asks why the service holds twice as many connections. Nothing fails, which is what makes it hard to spot. Interviewers ask this to check that we can connect a one-attribute change to its runtime effect. In this answer, we will recap what proxyBeanMethods controls, find the code that caused the second pool, prove it, fix it, and look at what else the unmanaged pool breaks.
1A Quick Recap: What proxyBeanMethods Controls
By default, a @Configuration class runs in full mode: Spring generates a CGLIB subclass of it at startup, and that subclass intercepts calls between @Bean methods. When one @Bean method calls another, the subclass returns the existing bean from the container instead of running the method again.
@Configuration(proxyBeanMethods = false) switches to lite mode. No subclass is generated, so @Bean methods are ordinary Java methods when our own code calls them. Spring still calls each method once to create its bean, but any extra call we make ourselves runs the method body again.
2Finding the Code That Created the Second Pool
The symptom tells us where to look. Two connection pools means two DataSource objects were constructed, so some code ran the dataSource() method body twice. In lite mode, that happens when another @Bean method calls dataSource() directly. Here is the configuration class after the change.
6 messages in order. Play them, or step through them one at a time.
The container manages pool 1; pool 2 belongs to nobody.
Before the change, the CGLIB subclass intercepted both direct calls and returned the single container bean. After it, each call builds a new HikariDataSource. If both methods call dataSource(), we get three pools, and the JdbcTemplate and the transaction manager use different pools, which is worse than wasteful: a transaction started by txManager is on a connection that jdbcTemplate never sees, so its queries run outside the transaction.
3Proving It
Hikari names its pools in order and logs each start, so the startup log usually shows the problem directly: HikariPool-1 - Starting and then HikariPool-2 - Starting, where we expected only one. We can also compare objects in a test.
On the database side, the number of open connections from the service doubles, for example from 10 to 20 with a pool size of 10, which is often how the bug is first noticed.
4The Fix: Take the Bean as a Parameter
We change the dependent methods to receive the DataSource as a parameter. Spring resolves the parameter from the container before calling the method, so it always passes the one managed bean, in lite mode and in full mode alike.
Now the log shows only HikariPool-1, the test passes, and the template and the transaction manager share one pool, so transactions work again. In a Spring Boot application, it is also worth asking whether these beans need defining at all: Boot's auto-configuration creates a DataSource, JdbcTemplate and transaction manager from spring.datasource properties when we do not define our own.
5What the Unmanaged Pool Breaks at Shutdown and Beyond
The container manages the lifecycle of its beans. At shutdown it calls close() on the DataSource bean, which Spring infers for @Bean methods that return objects with a close or shutdown method, so pool 1 releases its connections cleanly. Pool 2 was never registered as a bean, so nothing closes it. Its connections stay open until the JVM exits, which in tests that create many application contexts can exhaust the database's connection limit, and in production delays connection cleanup.
The unmanaged pool is also invisible to everything bean-aware. Actuator's health check and Hikari metrics are bound to DataSource beans, so dashboards show one healthy pool while the busy one, used by jdbcTemplate, has no metrics at all. That is why the problem can run unnoticed for a long time.
| Aspect | Pool 1 (bean) | Pool 2 (direct call) |
|---|---|---|
| Created by | the container calling dataSource() | jdbcTemplate() calling dataSource() |
| Closed at shutdown | yes, destroy method inferred | no |
| Health check and metrics | yes | no |
Shares transactions with txManager | only if txManager uses it | no, separate connections |
6Follow-up questions
How can we find such calls before switching a class to lite mode?
Search the configuration class for calls to its own @Bean methods; any call other than through a parameter is a candidate. An ArchUnit rule or a simple static check can flag methods annotated with @Bean that are invoked from other methods in the same class. Spring itself does not warn, because a direct call is legal Java.
Why did it work before the change?
In full mode, the CGLIB subclass of the configuration class intercepted the direct calls to dataSource() and returned the container's singleton. The code depended on that interception without saying so, and setting proxyBeanMethods = false removed it.
Should we just revert to full mode?
Reverting removes the symptom quickly and is a fine emergency fix. The better long-term fix is parameter injection, which works in both modes, makes dependencies explicit and lets the class keep the benefits of lite mode.
7Conclusion
In this answer, we traced two connection pools back to a @Bean method that called dataSource() directly, for example new JdbcTemplate(dataSource()). With proxyBeanMethods = false there is no CGLIB subclass to intercept that call, so it creates a second DataSource outside the container. We fixed it by injecting the bean as a method parameter, @Bean JdbcTemplate jdbcTemplate(DataSource dataSource), and saw that until then the extra pool is never closed at shutdown and is invisible to health checks and metrics, because the container does not manage it.
Open with
The change removed the CGLIB subclass that Spring generates for full-mode configuration classes. That subclass is what made direct calls between @Bean methods safe: when jdbcTemplate called dataSource(), the subclass intercepted it and returned the existing bean from the container.
Then, if they want more
So I would look for exactly that pattern, a @Bean method that calls dataSource() itself, like new JdbcTemplate(dataSource()) or a transaction manager built the same way. With proxyBeanMethods = false, that is a plain Java call, so it runs the method body again and builds a second HikariDataSource with its own pool.
The log confirms it, because Hikari logs HikariPool-1 and HikariPool-2 starting, and a test that asserts jdbcTemplate.getDataSource() is the same object as the DataSource bean fails. The fix is to take the dependency as a method parameter, jdbcTemplate(DataSource dataSource), so the container injects its single bean; that works in both modes.
I would also point out what the extra pool breaks: the container never registered it, so it is not closed at shutdown and leaks connections, especially in tests that start many contexts, and it has no health check or metrics. And if the template and transaction manager ended up on different pools, transactions would silently not cover the template's queries.
In Spring Boot I would also check whether we need these beans at all, since auto-configuration provides them.
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 "After changing a configuration class to @Configuration(proxyBeanMethods = false), the...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/777c99e63c/