Spring Boot Auto-Configuration and Starters Questions and Answers — Questions and Answers
Question 1: What is the primary mechanism Spring Boot uses to trigger auto-configuration based on the contents of the classpath?
- Scanning for classes annotated with @Component in third-party JARs.
- The presence of specific 'starter' dependencies in the build configuration. (Correct answer)
- Explicitly importing configuration classes using @Import in the main application class.
- Reading bean definitions from XML files found in the META-INF directory.
Correct answer: The presence of specific 'starter' dependencies in the build configuration.
Spring Boot's auto-configuration is fundamentally driven by the starter dependencies you include in your project. For example, including `spring-boot-starter-web` brings in dependencies like Tomcat and Spring MVC. Spring Boot detects these classes on the classpath and automatically configures beans like a `DispatcherServlet` and an embedded `TomcatServletWebServerFactory`.
Question 2: A developer wants to prevent `DataSourceAutoConfiguration` from running because they intend to define a custom `DataSource` bean. Which of the following is a valid way to achieve this?
- Add `@ComponentScan(excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = DataSourceAutoConfiguration.class))` to the main application class.
- Create a bean named `excludeDataSourceAutoConfiguration` in any `@Configuration` class.
- Set the property `spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration` in `application.properties`. (Correct answer)
- Comment out the `@EnableAutoConfiguration` annotation.
Correct answer: Set the property `spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration` in `application.properties`.
Spring Boot provides multiple ways to exclude specific auto-configuration classes. One common method is to use the `spring.autoconfigure.exclude` property in `application.properties` or `application.yml`, specifying the fully qualified class name of the auto-configuration to disable. Another way is using the `exclude` attribute of `@SpringBootApplication` or `@EnableAutoConfiguration` (e.g., `@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)`).
Question 3: In a custom Spring Boot starter, which file is the modern standard (since Spring Boot 2.7) for registering auto-configuration classes so that they can be discovered by other applications?
- META-INF/spring.factories
- META-INF/services/org.springframework.boot.autoconfigure.EnableAutoConfiguration
- META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (Correct answer)
- resources/autoconfiguration.properties
Correct answer: META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
Starting with Spring Boot 2.7 and becoming the standard in 3.0, auto-configuration classes are registered by listing their fully qualified names in the `META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports` file. The older `META-INF/spring.factories` file is now considered a legacy approach.
Question 4: A Spring Boot application needs to configure a `NotificationService` bean, but only if a property `notification.service.enabled` is set to `true` in the `application.properties`. Which annotation should be used on the bean definition?
- @ConditionalOnClass(name = "notification.service.enabled")
- @ConditionalOnProperty(name = "notification.service.enabled", havingValue = "true") (Correct answer)
- @ConditionalOnBean(name = "notification.service.enabled")
- @ConditionalOnExpression("'${notification.service.enabled}' == 'true'")
Correct answer: @ConditionalOnProperty(name = "notification.service.enabled", havingValue = "true")
The `@ConditionalOnProperty` annotation is specifically designed for this purpose. It allows the creation of a bean to be conditioned on the presence and value of a property in the Spring Environment. The `name` attribute specifies the property key, and `havingValue` specifies the required value for the condition to be met.
Question 5: A developer includes the `spring-boot-starter-data-jpa` dependency. They do not define a `DataSource` bean but provide the necessary database connection details in `application.properties`. What is the expected outcome?
- The application will fail to start because a `DataSource` bean must always be explicitly defined.
- The application will start, but database operations will fail at runtime due to a missing `EntityManagerFactory`.
- Spring Boot will not configure anything related to the database, requiring manual setup.
- Spring Boot will auto-configure a `DataSource`, a `JpaTransactionManager`, and an `EntityManagerFactory`. (Correct answer)
Correct answer: Spring Boot will auto-configure a `DataSource`, a `JpaTransactionManager`, and an `EntityManagerFactory`.
When `spring-boot-starter-data-jpa` is on the classpath and database connection properties are provided, Spring Boot's auto-configuration detects this setup. It will automatically configure a connection pool `DataSource`, an `EntityManagerFactory`, and a `JpaTransactionManager`, enabling the application to work with a database without explicit configuration beans.
Question 6: Which of the following best describes the relationship between Spring Boot Starters and Auto-Configuration?
- Starters are a replacement for auto-configuration in modern Spring Boot versions.
- Auto-configuration is a feature used to create custom starter dependencies.
- Starters provide dependencies, and auto-configuration uses the presence of those dependencies on the classpath to configure beans. (Correct answer)
- Auto-configuration must be manually triggered by a property within the starter's `pom.xml`.
Correct answer: Starters provide dependencies, and auto-configuration uses the presence of those dependencies on the classpath to configure beans.
Starters and auto-configuration work together. Starters are dependency descriptors that bundle common libraries needed for a specific functionality (e.g., web development, data access). Auto-configuration is the mechanism by which Spring Boot detects the classes from those libraries on the classpath and automatically configures the necessary beans for the application.
What is the primary mechanism Spring Boot uses to trigger auto-configuration based on the contents of the classpath?