Spring Boot Auto-Configuration and Starters 5 β Questions and Answers
Question 1: Which @ConditionalOn* annotation checks that NO bean of the specified type is already registered before creating a new one?
- @ConditionalOnSingleCandidate
- @ConditionalOnMissingBean (Correct answer)
- @ConditionalOnBean
- @ConditionalOnNoBean
Correct answer: @ConditionalOnMissingBean
@ConditionalOnMissingBean activates configuration only when no bean of the given type or name is already present in the ApplicationContext.
Question 2: You want your auto-configuration to activate only when exactly one candidate bean of a given type exists. Which annotation enforces this?
- @ConditionalOnBean
- @ConditionalOnMissingBean
- @ConditionalOnSingleCandidate (Correct answer)
- @Primary
Correct answer: @ConditionalOnSingleCandidate
@ConditionalOnSingleCandidate matches when exactly one bean of the specified type is registered, or one is marked @Primary among multiple.
Question 3: Which property controls whether Spring Boot's auto-configured Jackson ObjectMapper serializes null values?
- spring.jackson.default-property-inclusion=non_null (Correct answer)
- spring.jackson.serialization.WRITE_NULL_PROPERTIES=false
- spring.mvc.json.null-values=false
- spring.jackson.mapper.exclude-null=true
Correct answer: spring.jackson.default-property-inclusion=non_null
Setting spring.jackson.default-property-inclusion=non_null tells the auto-configured ObjectMapper to omit null fields from JSON output.
Question 4: What does adding spring-boot-devtools to a Spring Boot project do during development?
- It enables production-level caching of static resources
- It provides automatic application restart and LiveReload on classpath changes (Correct answer)
- It disables all auto-configurations for faster startup
- It replaces embedded Tomcat with Jetty for faster startup
Correct answer: It provides automatic application restart and LiveReload on classpath changes
spring-boot-devtools watches the classpath and triggers automatic restarts and LiveReload browser refreshes when files change.
Question 5: Which condition checks for the existence of a specific file or classpath resource before applying auto-configuration?
- @ConditionalOnClass
- @ConditionalOnProperty
- @ConditionalOnResource (Correct answer)
- @ConditionalOnFile
Correct answer: @ConditionalOnResource
@ConditionalOnResource applies configuration only when a specified resource path (file: or classpath:) exists.
Question 6: How does spring-boot-starter-validation add bean validation support to a Spring Boot app?
- It registers a custom DispatcherServlet that validates request bodies
- It auto-configures a LocalValidatorFactoryBean using Hibernate Validator on the classpath (Correct answer)
- It enables database-level constraint validation via JPA
- It adds a validation filter to the embedded Tomcat pipeline
Correct answer: It auto-configures a LocalValidatorFactoryBean using Hibernate Validator on the classpath
The starter brings in Hibernate Validator, and Spring Boot auto-configures a LocalValidatorFactoryBean so @Valid/@Validated annotations work out of the box.
Question 7: If spring.main.allow-bean-definition-overriding is set to false (the default in Spring Boot 2.1+) and two auto-configurations define the same bean name, what happens?
- The later bean silently overwrites the earlier one
- Spring Boot throws a BeanDefinitionOverrideException at startup (Correct answer)
- The application starts but logs a warning
- Spring Boot picks the bean with the higher @Order value
Correct answer: Spring Boot throws a BeanDefinitionOverrideException at startup
With overriding disabled, duplicate bean definitions cause a BeanDefinitionOverrideException and prevent the context from starting.
Which @ConditionalOn* annotation checks that NO bean of the specified type is already registered before creating a new one?