Spring / Spring7 basics Interview questions
What is the purpose of the @Configuration annotation?
@Configuration marks a class as a source of bean definitions written in plain Java rather than XML. Spring processes the class specially so that any @Bean methods inside it are only ever invoked once per container, even if one @Bean method calls another directly - the container intercepts the call and returns the existing singleton instance.
@Configuration public class AppConfig { @Bean public Clock systemClock() { return Clock.systemUTC(); } }
This behavior relies on CGLIB subclassing the configuration class at startup, which is why @Configuration classes cannot be declared final. It's the modern, type-safe replacement for the XML beans file used in older Spring applications.
More Related questions...