What happens when a prototype-scoped bean has a dependency on a singleton-scoped bean in Spring?
Answer: B
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.
Q.202Hard
In Spring Framework 2024-25, which feature allows lazy initialization of beans?
Answer: A
@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.
Q.203Hard
A microservices architecture uses Spring with circular dependency between ServiceA and ServiceB. What is the best solution?
Answer: B
Circular dependencies indicate design issues. Best solutions: refactor code, use setter injection (instead of constructor) combined with @Lazy, or introduce an intermediary service to break the cycle.
Q.204Hard
How does Spring handle circular dependency between two beans?
Answer: A
Spring can resolve circular dependencies when at least one injection is done via setter method rather than constructor, allowing lazy initialization.
Q.205Hard
An enterprise application requires different bean implementations based on runtime conditions. Which Spring feature is most suitable?
Answer: D
@Conditional provides fine-grained control, @Profile handles environment-specific beans, and factory beans offer explicit creation logic. Choice depends on use case requirements.
Advertisement
Q.206Hard
Consider a Spring application where method execution time tracking is required across multiple methods. Which approach is most appropriate?
Answer: A
Spring AOP with a custom aspect provides centralized, reusable cross-cutting concern implementation without code duplication in individual methods.
Q.207Hard
In Spring framework, what is the purpose of FactoryBean interface?
Answer: A
FactoryBean allows creating beans with sophisticated initialization logic through getObject() method, useful for complex object creation.
Q.208Hard
What is the primary difference between BeanFactory and ApplicationContext in Spring?
Answer: A
ApplicationContext extends BeanFactory and adds features like event publishing, message resource handling, and easier AOP integration.
Q.209Hard
In a complex Spring application, when would you prefer to use ObjectFactory<T> over direct bean injection?
Answer: D
ObjectFactory enables lazy initialization, provides new instances for prototype beans, and can help resolve circular dependencies through deferred access.
Q.210Hard
Consider a Spring application where you need different bean implementations based on the deployment environment. Which approach is most suitable?
Answer: D
@Profile is for environment-based activation, @Conditional for custom logic, and @ConditionalOnProperty for property-driven bean registration. Choice depends on specific requirements.
Q.211Hard
In a multi-threaded Spring application with singleton beans, what thread-safety consideration must be kept in mind?
Answer: B
Spring doesn't enforce thread-safety for singleton beans. Developers must implement thread-safe code, use immutable objects, or utilize synchronization where necessary.