Quick revision
The short answer
It prints the Repo line once. A @Configuration class is, by default, subclassed with CGLIB at startup, and the subclass intercepts the calls to repo() inside a() and b(), returning the singleton that is already in the container instead of running the method body again. So both services share the same Repo instance. If the class were a plain @Component, or used @Configuration(proxyBeanMethods = false), each call would be an ordinary Java call and Repo would be constructed three times.
What to remember
In this answer, we saw that the example prints the Repo line once. Full-mode @Configuration classes are subclassed with CGLIB, and the subclass intercepts direct calls to @Bean methods such as repo(), returning the existing singleton from the container, so both services share one Repo instance. With a plain @Component or proxyBeanMethods = false, the call is an ordinary Java call and Repo is constructed three times. Injecting Repo as a @Bean method parameter gives one instance in either mode.
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.
It prints the Repo line once. A @Configuration class is, by default, subclassed with CGLIB at startup, and the subclass intercepts the calls to repo() inside a() and b(), returning the singleton that is already in the container instead of running the method body again. So both services share the same Repo instance. If the class were a plain @Component, or used @Configuration(proxyBeanMethods = false), each call would be an ordinary Java call and Repo would be constructed three times.
Why this question is asked
This small puzzle looks like plain Java, where calling a method three times runs it three times, yet in Spring the answer is different. Interviewers like it because it tests whether we know that Spring changes our configuration classes at runtime, and it leads naturally to lite mode, proxyBeanMethods and a real class of production bugs. In this answer, we will write the example out, run it, explain the CGLIB subclass that makes the difference, prove that both services share one Repo, and then see the same code print three times in lite mode.
1A Quick Recap of @Bean Methods
A @Bean method is a factory method: Spring calls it to create a bean and registers the returned object under the method's name. By default the bean is a singleton, meaning the container creates one instance and gives that same instance to everyone who needs it.
Inside a configuration class we often need one bean to build another. Here ServiceA and ServiceB both need a Repo, and the code expresses that by calling repo() directly. In plain Java that would create a new Repo each time. The question is whether Spring keeps its singleton promise when we call the method ourselves.
2Running the Example
Here is the code from the question, made runnable.
Repo is created exactly once, and the Repo inside ServiceA, the Repo inside ServiceB and the Repo bean in the container are the same object. The last line gives away how: the bean registered for AppConfig is not an AppConfig but a generated subclass of it.
3Why: The CGLIB Subclass Intercepts repo()
During startup, ConfigurationClassPostProcessor finds classes annotated with @Configuration whose proxyBeanMethods attribute is true, which is the default. For each one it uses CGLIB, a byte code generation library bundled inside Spring, to create a subclass at runtime and swaps the bean definition's class to that subclass. The subclass overrides every @Bean method.
The overriding method does not simply run our code. It first asks the bean factory whether a bean with that name already exists or is being created. If it does, it returns the container's instance. Only when the container itself is creating the bean does it call the original method body, our super.repo(). So when a() runs and calls repo(), the call is dispatched to the subclass, which finds the existing Repo singleton and returns it. b() gets the same object. The singleton promise holds even though our code calls the method directly.
This is also why @Configuration classes must not be final and their @Bean methods must not be private or final: CGLIB has to subclass the class and override the methods. Spring reports an error at startup if they are.
8 messages in order. Play them, or step through them one at a time.
Our call to repo() reaches the override, which returns the container's singleton.
4The Same Code in Lite Mode
Now change the annotation to @Configuration(proxyBeanMethods = false), or replace it with @Component. Spring no longer generates a subclass. The @Bean methods are still used to create beans, but our call to repo() inside a() is now a normal Java method call on a normal object.
Three Repo objects exist, and only one of them is managed by the container. The other two have no lifecycle callbacks and no proxies: if Repo were @Transactional, the copies inside the services would have no transactions. The order of the three lines depends on the order in which Spring creates the beans, but the count is always three.
5The Fix That Works in Both Modes
The robust way to express a dependency between beans is a method parameter. Spring resolves the parameter from the container, so it gets the singleton whether or not the class is proxied.
| Configuration style | Calls repo() directly | Takes Repo as a parameter |
|---|---|---|
@Configuration (default, full mode) | once | once |
@Configuration(proxyBeanMethods = false) | three times | once |
@Component with @Bean methods | three times | once |
With parameters, the configuration class no longer depends on the CGLIB trick at all, which is why Spring Boot's own auto-configurations are written this way and use proxyBeanMethods = false.
6Follow-up questions
Would the answer change if Repo were a prototype bean?
Yes. With @Scope("prototype") on repo(), the subclass asks the container for the bean, and the container creates a new prototype instance each time. So a() and b() would each get their own Repo, and the line would be printed twice, once per service, plus once more for any other lookup of repo.
Does calling repo() from outside the container also return the singleton?
If we take the AppConfig bean from the context and call repo() on it, yes, because that object is the CGLIB subclass. If we write new AppConfig().repo() ourselves, no, because a plain AppConfig instance has no interception; it just runs the method.
Is the CGLIB subclass the same as an AOP proxy?
No. It is a subclass created by ConfigurationClassPostProcessor specifically to intercept @Bean method calls. AOP proxies for @Transactional or @Async are created later by BeanPostProcessors and wrap beans from the outside. Both use byte code generation, but they serve different purposes.
7Conclusion
In this answer, we saw that the example prints the Repo line once. Full-mode @Configuration classes are subclassed with CGLIB, and the subclass intercepts direct calls to @Bean methods such as repo(), returning the existing singleton from the container, so both services share one Repo instance. With a plain @Component or proxyBeanMethods = false, the call is an ordinary Java call and Repo is constructed three times. Injecting Repo as a @Bean method parameter gives one instance in either mode.
Open with
It prints Repo created once. The reason is that a class annotated with @Configuration is, by default, not used as written. At startup ConfigurationClassPostProcessor generates a CGLIB subclass of it, and the subclass overrides every @Bean method.
Then, if they want more
When a() calls repo(), that call goes to the override, which first asks the bean factory whether a bean named repo already exists, and since it does, returns the container's singleton instead of running new Repo() again. b() gets the same object. So ServiceA and ServiceB share one Repo, and it is the same instance as ctx.getBean(Repo.class).
You can see it if you print the class of the config bean: it is AppConfig with a SpringCGLIB suffix. This is also why configuration classes cannot be final.
If the class were a plain @Component, or used @Configuration with proxyBeanMethods = false, there would be no subclass, so repo() inside a() and b() would be ordinary Java calls, and Repo would be constructed three times: once for the container's bean and once inside each service, with the extra two not managed by Spring at all.
That is why I prefer declaring the dependency as a method parameter, a(Repo repo), which works the same in both modes.
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 "What does this print, and why?": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/c48116f2cb/