Spring Framework Spring Boot & Auto-Configuration 1 — Questions and Answers
Question 1: What is the primary annotation on the main class of a Spring Boot application?
- @SpringApplication
- @EnableSpringBoot
- @SpringBootApplication (Correct answer)
- @BootstrapApplication
Correct answer: @SpringBootApplication
@SpringBootApplication is a convenience annotation combining @Configuration, @EnableAutoConfiguration, and @ComponentScan for the main entry point.
Question 2: Where does Spring Boot look for application properties by default?
- src/main/java/application.properties
- src/main/resources/application.properties (Correct answer)
- src/config/application.properties
- WEB-INF/application.properties
Correct answer: src/main/resources/application.properties
Spring Boot automatically loads application.properties (or application.yml) from src/main/resources on the classpath.
Question 3: What does Spring Boot's auto-configuration feature do?
- Generates boilerplate code automatically
- Automatically configures beans based on classpath dependencies and properties (Correct answer)
- Automatically deploys the application to a server
- Auto-generates API documentation
Correct answer: Automatically configures beans based on classpath dependencies and properties
Auto-configuration detects the libraries on the classpath and automatically creates and configures Spring beans with sensible defaults.
Question 4: Which Spring Boot starter dependency should be added for building REST APIs?
- spring-boot-starter-data
- spring-boot-starter-web (Correct answer)
- spring-boot-starter-rest
- spring-boot-starter-mvc
Correct answer: spring-boot-starter-web
spring-boot-starter-web includes Spring MVC, embedded Tomcat, and JSON serialization support for building RESTful web services.
Question 5: How do you override a specific auto-configuration class in Spring Boot?
- Delete the auto-configuration JAR
- Use @SpringBootApplication(exclude = {ClassName.class}) (Correct answer)
- Set spring.autoconfigure.enable=false
- Override using @OverrideAutoConfiguration
Correct answer: Use @SpringBootApplication(exclude = {ClassName.class})
The exclude attribute of @SpringBootApplication (or @EnableAutoConfiguration) accepts an array of auto-configuration classes to disable.
Question 6: What is an 'opinionated' framework in the context of Spring Boot?
- A framework that enforces a specific coding style
- A framework that provides sensible defaults and reduces configuration decisions (Correct answer)
- A framework with strict licensing opinions
- A framework that only supports one database type
Correct answer: A framework that provides sensible defaults and reduces configuration decisions
Spring Boot is opinionated in that it provides pre-configured defaults for most settings, reducing the time developers spend on boilerplate configuration.
What is the primary annotation on the main class of a Spring Boot application?