Spring Boot Core Container and Beans 4 — Questions and Answers
Question 1: What annotation is used to inject a value from application.properties into a Spring bean field?
- @Inject
- @Value (Correct answer)
- @Property
- @ConfigParam
Correct answer: @Value
@Value("${property.key}") reads the specified property from the environment and injects it into the annotated field.
Question 2: Which ApplicationContext implementation is best suited for a standalone Spring Boot application reading properties from the classpath?
- ClassPathXmlApplicationContext
- AnnotationConfigApplicationContext (Correct answer)
- AnnotationConfigServletWebServerApplicationContext
- GenericWebApplicationContext
Correct answer: AnnotationConfigApplicationContext
AnnotationConfigApplicationContext loads bean definitions from @Configuration classes without needing XML or a web server.
Question 3: What does Spring's @Qualifier annotation allow you to do?
- Restrict a bean to a specific environment profile
- Specify which bean to inject when multiple candidates exist (Correct answer)
- Qualify a bean as thread-safe
- Reduce the bean's initialization priority
Correct answer: Specify which bean to inject when multiple candidates exist
@Qualifier combined with @Autowired narrows the injection to the bean with the matching qualifier name.
Question 4: Which destroy callback runs when a singleton bean's ApplicationContext is closed?
- @PreDestroy method (Correct answer)
- BeanFactory.destroyBean()
- DisposableBean.close()
- SmartLifecycle.stop()
Correct answer: @PreDestroy method
Methods annotated with @PreDestroy are called by Spring just before a singleton bean is destroyed during context shutdown.
Question 5: What is the effect of setting @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) on a request-scoped bean?
- The bean is created once and cached for all requests
- A CGLIB proxy is injected so the singleton can delegate to the correct request-scoped instance at runtime (Correct answer)
- The bean bypasses Spring's dependency injection entirely
- The bean is destroyed after each method call
Correct answer: A CGLIB proxy is injected so the singleton can delegate to the correct request-scoped instance at runtime
A scoped proxy wraps the short-lived bean so singletons can hold a reference without capturing a stale instance.
Question 6: Which Spring annotation enables component scanning for a specific base package?
- @EnableBeans
- @ComponentScan (Correct answer)
- @ScanPackages
- @AutoDetect
Correct answer: @ComponentScan
@ComponentScan tells Spring where to look for @Component-annotated classes to register as beans.
Question 7: How can you override a Spring Boot auto-configured bean with your own custom implementation?
- Annotate your bean with @Override
- Define a bean of the same type; @ConditionalOnMissingBean backs off the auto-config (Correct answer)
- Disable auto-configuration in application.properties
- Extend the auto-configured class and annotate with @Primary
Correct answer: Define a bean of the same type; @ConditionalOnMissingBean backs off the auto-config
Spring Boot's auto-configuration classes use @ConditionalOnMissingBean, so defining your own bean of that type prevents auto-configuration from registering its default.
What annotation is used to inject a value from application.properties into a Spring bean field?