When would you use request-scoped beans in a Spring web application?
Answer: A
Request-scoped beans are instantiated per HTTP request and destroyed after request completion, ideal for storing user-specific or request-specific information.
Q.462Medium
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.463Medium
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.464Medium
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.465Medium
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.
Advertisement
Q.466Medium
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.
Q.467Medium
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.468Medium
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.469Medium
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.470Medium
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.471Medium
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.472Medium
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.