Spring Boot Core Container and Beans 3 — Questions and Answers
Question 1: What is the role of BeanPostProcessor in the Spring container lifecycle?
- It validates bean configurations before startup
- It intercepts beans before and after initialization to apply custom logic (Correct answer)
- It registers beans dynamically at runtime
- It disposes of beans when the context closes
Correct answer: It intercepts beans before and after initialization to apply custom logic
BeanPostProcessor allows custom modification of new bean instances both before and after their init callbacks are called.
Question 2: Which method in the BeanFactory interface retrieves a bean by type?
- getBean(String name)
- getBean(Class<T> requiredType) (Correct answer)
- lookupBean(Class<T> type)
- resolveBean(Type type)
Correct answer: getBean(Class<T> requiredType)
BeanFactory.getBean(Class<T>) looks up a bean by its type and returns it without needing to know its name.
Question 3: In Spring, what does @DependsOn("beanA") guarantee when placed on beanB?
- beanA is injected into beanB automatically
- beanA is initialized before beanB (Correct answer)
- beanA and beanB share the same scope
- beanA proxies all calls to beanB
Correct answer: beanA is initialized before beanB
@DependsOn ensures the listed beans are fully initialized before the annotated bean is created.
Question 4: Which Spring feature allows you to conditionally register a bean based on the presence of a class on the classpath?
- @ConditionalOnClass (Correct answer)
- @Profile
- @EnableAutoConfiguration
- @ConditionalOnProperty
Correct answer: @ConditionalOnClass
@ConditionalOnClass registers the bean only when the specified class is present on the classpath.
Question 5: What is the purpose of the @Primary annotation on a Spring bean?
- It marks the bean as the highest-priority singleton
- It designates the preferred bean when multiple candidates exist for autowiring (Correct answer)
- It forces eager initialization of the bean
- It prevents the bean from being overridden by child contexts
Correct answer: It designates the preferred bean when multiple candidates exist for autowiring
@Primary tells Spring to prefer this bean over other candidates when resolving an ambiguous autowiring dependency.
Question 6: Which callback interface allows a bean to receive a reference to the ApplicationContext it lives in?
- BeanNameAware
- ApplicationContextAware (Correct answer)
- EnvironmentAware
- ResourceLoaderAware
Correct answer: ApplicationContextAware
Implementing ApplicationContextAware causes Spring to inject the ApplicationContext into the bean via setApplicationContext().
Question 7: When using constructor injection with multiple constructors, how does Spring Boot 2.7+ determine which constructor to use if none is annotated?
- It always uses the no-arg constructor
- It fails with an exception unless @Autowired is present
- It picks the single constructor automatically if only one exists, or requires @Autowired for multiple (Correct answer)
- It uses the constructor with the most parameters
Correct answer: It picks the single constructor automatically if only one exists, or requires @Autowired for multiple
Spring uses the single constructor automatically; when multiple constructors exist, @Autowired must explicitly mark the target.
What is the role of BeanPostProcessor in the Spring container lifecycle?