In Spring Framework 2024-25, which feature allows lazy initialization of beans?
A@Lazy annotation on @Bean or @Component
B@DeferredInit annotation
CSetting lazy-init='true' only in XML
DSpring automatically detects and lazily initializes all beans
Correct Answer:
A. @Lazy annotation on @Bean or @Component
EXPLANATION
@Lazy annotation (available in modern Spring versions) marks beans for lazy initialization. It can be used on class level or with @Bean methods. XML also supports lazy-init attribute.
What happens when a prototype-scoped bean has a dependency on a singleton-scoped bean in Spring?
ACompilation error occurs
BThe singleton bean is correctly injected into each prototype instance
CThe prototype bean becomes singleton
DA runtime exception is thrown
Correct Answer:
B. The singleton bean is correctly injected into each prototype instance
EXPLANATION
A prototype bean can depend on singleton beans without issues. Each prototype instance gets the same singleton instance injected. The reverse (singleton depending on prototype) is problematic.
An enterprise application needs different bean implementations based on environment (dev/prod). Which approach is most suitable?
A@Bean with conditional logic inside @Configuration
B@Conditional annotation with custom conditions
C@Profile annotation to define environment-specific beans
DManual bean creation in main() method
Correct Answer:
C. @Profile annotation to define environment-specific beans
EXPLANATION
@Profile allows defining beans for specific environments (dev, prod, test). Alternatively, @Conditional provides more granular control with custom conditions.
What is the role of ApplicationContext in Spring Framework?
ATo execute SQL queries
BTo manage beans, enable dependency injection, and provide application-wide features
CTo compile Java code
DTo generate REST endpoints automatically
Correct Answer:
B. To manage beans, enable dependency injection, and provide application-wide features
EXPLANATION
ApplicationContext is the central Spring container that manages bean lifecycle, performs DI, and provides additional features like event publishing and message resolution.