Spring Boot Core Container and Beans 5 — Questions and Answers
Question 1: Which annotation, when placed on a @Configuration class, imports bean definitions from another @Configuration class?
- @Include
- @Import (Correct answer)
- @Use
- @Extend
Correct answer: @Import
@Import allows one @Configuration class to pull in bean definitions from one or more other @Configuration classes.
Question 2: What does the @PostConstruct annotation indicate in a Spring-managed bean?
- The method runs before the bean's constructor
- The method runs after all dependencies have been injected (Correct answer)
- The method runs when the context is refreshed
- The method runs inside a transaction after bean creation
Correct answer: The method runs after all dependencies have been injected
@PostConstruct marks a method to be executed after dependency injection is complete, serving as an init callback.
Question 3: In Spring, what is an inner bean?
- A bean nested inside a prototype-scoped bean
- A bean defined inline inside another bean's property without a global id (Correct answer)
- A private field injected without @Autowired
- A bean that can only be accessed from within the same package
Correct answer: A bean defined inline inside another bean's property without a global id
An inner bean is defined as a local bean inside another bean's configuration and cannot be referenced by other beans.
Question 4: Which of the following correctly describes field injection vs. constructor injection in Spring?
- Field injection makes dependencies mandatory; constructor injection makes them optional
- Constructor injection makes dependencies mandatory and supports immutability; field injection does not (Correct answer)
- Both approaches are equivalent in terms of testability
- Field injection is preferred by the Spring team for production code
Correct answer: Constructor injection makes dependencies mandatory and supports immutability; field injection does not
Constructor injection enforces required dependencies at object creation and enables immutable (final) fields, which field injection cannot do.
Question 5: What happens if you annotate a @Bean method inside a non-@Configuration class (e.g., a @Component class) in Spring?
- Spring throws an exception at startup
- The @Bean method works but without CGLIB proxying, so inter-bean calls are plain Java calls (Correct answer)
- The bean is registered but not managed by the container
- The bean is always created with prototype scope
Correct answer: The @Bean method works but without CGLIB proxying, so inter-bean calls are plain Java calls
In a lite-mode (non-@Configuration) class, @Bean methods are not CGLIB-proxied, so calling one bean method from another bypasses the container.
Question 6: Which Spring mechanism allows you to register beans programmatically after the ApplicationContext has started?
- BeanDefinitionRegistry.registerBeanDefinition() (Correct answer)
- ApplicationContext.refresh()
- ConfigurableListableBeanFactory.addBean()
- BeanFactory.instantiateBean()
Correct answer: BeanDefinitionRegistry.registerBeanDefinition()
BeanDefinitionRegistry.registerBeanDefinition() lets you add new bean definitions to the registry at runtime.
Question 7: When would you use @Autowired(required = false) in Spring?
- When you want Spring to throw an exception if the bean is missing
- When the dependency is optional and the application should still start without it (Correct answer)
- When the dependency is a primitive type
- When you need to inject multiple beans of the same type
Correct answer: When the dependency is optional and the application should still start without it
Setting required = false tells Spring not to fail if no matching bean is found, allowing the field to remain null.
Which annotation, when placed on a @Configuration class, imports bean definitions from another @Configuration class?