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.
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.
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.
In Spring Framework, which interface represents a factory pattern implementation for creating objects?
AObjectFactory
BFactoryBean
CBeanFactory
DAll of the above
Correct Answer:
D. All of the above
EXPLANATION
BeanFactory is the basic container, ObjectFactory is for lazy bean retrieval, and FactoryBean allows custom bean creation logic. All represent factory patterns in Spring.
What is the role of Spring's PropertyPlaceholderConfigurer in application configuration?
AIt encrypts sensitive properties
BIt replaces ${} placeholders with values from properties files
CIt validates XML configuration files
DIt manages database transactions
Correct Answer:
B. It replaces ${} placeholders with values from properties files
EXPLANATION
PropertyPlaceholderConfigurer (or @PropertySource in modern Spring) enables externalization of configuration by replacing ${property.name} placeholders with actual values from properties files.
Consider a scenario where a Spring application needs to initialize a database connection pool at startup. Which method should be used?
AUse @PostConstruct annotation on a method
BUse InitializingBean interface
CUse initMethod parameter in @Bean
DAll of the above are valid approaches
Correct Answer:
D. All of the above are valid approaches
EXPLANATION
Spring provides multiple mechanisms for initialization: @PostConstruct, InitializingBean interface, and initMethod in @Bean definition. All are equally valid.
What is the difference between @Bean and @Component annotations in Spring?
A@Bean is class-level while @Component is method-level
B@Bean is method-level while @Component is class-level
CThey are functionally identical with no difference
D@Bean cannot be used in @Configuration classes
Correct Answer:
B. @Bean is method-level while @Component is class-level
EXPLANATION
@Component is applied to classes making them beans automatically. @Bean is applied to methods in @Configuration classes, giving explicit control over bean creation.
In Spring Framework, what does the @Autowired annotation do when autowiring candidates are ambiguous?
AIt automatically selects the first bean found
BIt throws NoUniqueBeanDefinitionException
CIt uses the @Primary annotation to resolve conflicts
DBoth B and C are correct
Correct Answer:
D. Both B and C are correct
EXPLANATION
When multiple beans match the injection point, Spring throws an exception unless @Primary is used to indicate the preferred bean or @Qualifier specifies which bean to inject.