Spring Boot Auto-Configuration and Starters 2 — Questions and Answers
Question 1: Which annotation is placed on an auto-configuration class to ensure it only activates when a specific class is present on the classpath?
- @ConditionalOnMissingClass
- @ConditionalOnClass (Correct answer)
- @ConditionalOnBean
- @ConditionalOnResource
Correct answer: @ConditionalOnClass
@ConditionalOnClass causes Spring Boot to apply the configuration only when all specified classes are on the classpath.
Question 2: What happens when you add spring-boot-starter-data-jpa to your project without any database driver dependency?
- Spring Boot silently ignores JPA auto-configuration
- The application starts with an in-memory database automatically
- The application fails to start with a 'Failed to determine a suitable driver class' error (Correct answer)
- Spring Boot defaults to H2 as the driver
Correct answer: The application fails to start with a 'Failed to determine a suitable driver class' error
Without a JDBC driver on the classpath, Spring Boot cannot auto-configure a DataSource and throws a startup failure.
Question 3: Which file inside a Spring Boot auto-configuration JAR registers auto-configuration classes for Spring Boot 3.x?
- META-INF/spring.factories
- META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (Correct answer)
- META-INF/services/AutoConfiguration
- resources/auto-config.properties
Correct answer: META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
Spring Boot 3.x uses META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports instead of the legacy spring.factories key.
Question 4: What is the purpose of the @AutoConfiguration annotation introduced in Spring Boot 2.7?
- It replaces @SpringBootApplication on the main class
- It marks a class as an auto-configuration candidate and sets ordering defaults (Correct answer)
- It disables component scanning for the annotated class
- It enables AOP proxying on the configuration class
Correct answer: It marks a class as an auto-configuration candidate and sets ordering defaults
@AutoConfiguration is a specialization of @Configuration that signals the class is an auto-configuration, integrating naturally with the new imports file.
Question 5: Which property prefix is used to configure the embedded Tomcat server's port in a Spring Boot application?
- spring.tomcat.port
- server.port (Correct answer)
- embedded.server.port
- spring.server.port
Correct answer: server.port
Spring Boot's server auto-configuration reads server.port (default 8080) to bind the embedded Tomcat.
Question 6: If you define your own DataSource bean, what happens to Spring Boot's DataSource auto-configuration?
- It throws a BeanDefinitionOverrideException
- It backs off because of @ConditionalOnMissingBean on the auto-configured DataSource (Correct answer)
- It creates a second DataSource and merges them
- It ignores your custom DataSource and uses its own
Correct answer: It backs off because of @ConditionalOnMissingBean on the auto-configured DataSource
DataSourceAutoConfiguration uses @ConditionalOnMissingBean(DataSource.class), so it skips creation when you provide your own.
Question 7: Which Spring Boot starter bundles Jackson for JSON serialization/deserialization along with Spring MVC?
- spring-boot-starter-json
- spring-boot-starter-web (Correct answer)
- spring-boot-starter-rest
- spring-boot-starter-jackson
Correct answer: spring-boot-starter-web
spring-boot-starter-web transitively pulls in spring-boot-starter-json, which includes the Jackson databind library.
Which annotation is placed on an auto-configuration class to ensure it only activates when a specific class is present on the classpath?