Govt. Exams
Entrance Exams
Spring doesn't enforce thread-safety for singleton beans. Developers must implement thread-safe code, use immutable objects, or utilize synchronization where necessary.
@Profile is for environment-based activation, @Conditional for custom logic, and @ConditionalOnProperty for property-driven bean registration. Choice depends on specific requirements.
ObjectFactory enables lazy initialization, provides new instances for prototype beans, and can help resolve circular dependencies through deferred access.
ApplicationContext extends BeanFactory and adds features like event publishing, message resource handling, and easier AOP integration.
FactoryBean allows creating beans with sophisticated initialization logic through getObject() method, useful for complex object creation.
Spring AOP with a custom aspect provides centralized, reusable cross-cutting concern implementation without code duplication in individual methods.
@Conditional provides fine-grained control, @Profile handles environment-specific beans, and factory beans offer explicit creation logic. Choice depends on use case requirements.
Spring can resolve circular dependencies when at least one injection is done via setter method rather than constructor, allowing lazy initialization.
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.
@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.