2V0-72.22PSE · Question #9
Refer to the exhibit. Based on the default Spring behavior, choose the correct answer. (Choose the best answer.)
The correct answer is A. One AccountRepository bean will be instantiated since the default scope is singleton. In Spring, the default bean scope is singleton, meaning the container creates exactly one instance of a bean per ApplicationContext and returns that same cached instance every time it is requested - regardless of how many other beans depend on it or how many times the factory…
Question
Refer to the exhibit. Based on the default Spring behavior, choose the correct answer. (Choose the best answer.)
Exhibit
Options
- AOne AccountRepository bean will be instantiated since the default scope is singleton.
- BThree AccountRepository beans will be instantiated as the accountRepository() method will be
- CMany AccountRepository beans will be instantiated, depending how often accountRepository(),
- DTwo AccountRepository beans will be instantiated as the accountRepository() method will be
How the community answered
(53 responses)- A89% (47)
- B4% (2)
- C2% (1)
- D6% (3)
Explanation
In Spring, the default bean scope is singleton, meaning the container creates exactly one instance of a bean per ApplicationContext and returns that same cached instance every time it is requested - regardless of how many other beans depend on it or how many times the factory method is called. This works because Spring wraps @Configuration classes with a CGLIB proxy that intercepts @Bean method calls and returns the existing instance instead of invoking the method again.
Options B, C, and D all describe prototype scope behavior, where a new instance is created on each request or injection - that is not the default. The distractors are designed to trick you into thinking Spring calls the method literally (like a plain Java method), which would create multiple objects, but the CGLIB proxy prevents that in a proper @Configuration class.
Memory tip: Think "Spring is stingy by default" - it creates beans once and reuses them everywhere, so singleton is the default until you explicitly annotate otherwise with @Scope("prototype") or another scope.
Topics
Community Discussion
No community discussion yet for this question.
