In a multi-threaded Spring application with singleton beans, what thread-safety consideration must be kept in mind?
ASingleton beans are automatically thread-safe by Spring
BDevelopers must ensure singleton bean implementations are thread-safe
CUse synchronized keyword on all methods in singleton beans
DConvert singleton beans to prototype scope for thread safety
Correct Answer:
B. Developers must ensure singleton bean implementations are thread-safe
EXPLANATION
Spring doesn't enforce thread-safety for singleton beans. Developers must implement thread-safe code, use immutable objects, or utilize synchronization where necessary.
What is the purpose of using @Bean method parameters in Spring @Configuration classes?
ATo inject dependencies into the bean being created
BTo pass configuration values from properties files
CTo define bean relationships through method parameters
DBoth A and C are correct
Correct Answer:
D. Both A and C are correct
EXPLANATION
Method parameters in @Bean methods allow Spring to inject dependent beans and establish bean relationships, enabling dependency injection at bean creation time.
Consider a Spring application where you need different bean implementations based on the deployment environment. Which approach is most suitable?
AUse @Profile annotation on bean definitions
BUse @Conditional annotation with custom conditions
CUse @ConditionalOnProperty for property-based selection
DAll of the above are valid approaches
Correct Answer:
D. All of the above are valid approaches
EXPLANATION
@Profile is for environment-based activation, @Conditional for custom logic, and @ConditionalOnProperty for property-driven bean registration. Choice depends on specific requirements.
What is the significance of the @Qualifier annotation when used alongside @Autowired in Spring?
AIt improves performance of bean injection
BIt specifies which bean to inject when multiple candidates exist
CIt marks a bean as high quality
DIt reduces memory consumption
Correct Answer:
B. It specifies which bean to inject when multiple candidates exist
EXPLANATION
@Qualifier resolves ambiguity in autowiring by specifying the exact bean name or custom qualifier to inject when multiple beans of the same type exist.
In a complex Spring application, when would you prefer to use ObjectFactory over direct bean injection?
ATo get a new instance of the bean each time it's accessed
BTo defer bean creation until it's actually needed
CTo break circular dependencies between beans
DAll of the above scenarios
Correct Answer:
D. All of the above scenarios
EXPLANATION
ObjectFactory enables lazy initialization, provides new instances for prototype beans, and can help resolve circular dependencies through deferred access.
Which annotation is used to provide external configuration properties to Spring beans in the 2024 Spring Boot version?
A@PropertySource
B@ConfigurationProperties
C@Value
DAll of the above
Correct Answer:
D. All of the above
EXPLANATION
@PropertySource loads properties from files, @ConfigurationProperties binds properties to bean properties, and @Value injects individual values. All are used for configuration.