What is the difference between constructor injection and setter injection in Spring?
Answer: A
Constructor injection enforces required dependencies and creates immutable objects; setter injection allows optional dependencies and partial initialization.
Q.22Medium
In Spring Framework, what does the @Autowired annotation do when autowiring candidates are ambiguous?
Answer: D
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.
Q.23Medium
What is the difference between @Bean and @Component annotations in Spring?
Answer: B
@Component is applied to classes making them beans automatically. @Bean is applied to methods in @Configuration classes, giving explicit control over bean creation.
Q.24Medium
Consider a scenario where a Spring application needs to initialize a database connection pool at startup. Which method should be used?
Answer: D
Spring provides multiple mechanisms for initialization: @PostConstruct, InitializingBean interface, and initMethod in @Bean definition. All are equally valid.
Q.25Medium
What is the role of Spring's PropertyPlaceholderConfigurer in application configuration?
Answer: B
PropertyPlaceholderConfigurer (or @PropertySource in modern Spring) enables externalization of configuration by replacing ${property.name} placeholders with actual values from properties files.
Advertisement
Q.26Medium
In Spring Framework, which interface represents a factory pattern implementation for creating objects?
Answer: D
BeanFactory is the basic container, ObjectFactory is for lazy bean retrieval, and FactoryBean allows custom bean creation logic. All represent factory patterns in Spring.
Q.27Medium
What happens when @Lazy annotation is applied to a singleton bean in Spring?
Answer: A
@Lazy defers bean instantiation until it's first requested, overriding the default eager initialization behavior of singleton beans.
Q.28Medium
Which annotation is used to provide external configuration properties to Spring beans in the 2024 Spring Boot version?
Answer: D
@PropertySource loads properties from files, @ConfigurationProperties binds properties to bean properties, and @Value injects individual values. All are used for configuration.
Q.29Medium
What is the significance of the @Qualifier annotation when used alongside @Autowired in Spring?
Answer: B
@Qualifier resolves ambiguity in autowiring by specifying the exact bean name or custom qualifier to inject when multiple beans of the same type exist.
Q.30Medium
In Spring Framework 6.0, what is the role of the @EnableAutoConfiguration annotation in Spring Boot?
Answer: D
@EnableAutoConfiguration (or @SpringBootApplication which includes it) auto-configures Spring beans based on jar dependencies on the classpath.
Q.31Medium
What is the purpose of using @Bean method parameters in Spring @Configuration classes?
Answer: D
Method parameters in @Bean methods allow Spring to inject dependent beans and establish bean relationships, enabling dependency injection at bean creation time.