Load auto-configurations from META-INF/spring-boot

Implements a new AutoConfigurationLoader, which loads
auto-configurations from a file in META-INF/spring-boot.

Adapts the AutoConfigurationImportSelector to use the new loader.

Adapts the ImportAutoConfigurationImportSelector to use the new loader.

Adapts the metadata plugin in the build to additionally load the
auto-configurations from the new file.

Updates the documentation for auto-configurations and test slices.

Closes gh-29872
This commit is contained in:
Moritz Halbritter
2022-02-17 09:10:53 +01:00
parent a55759d531
commit 7d68b58c8f
17 changed files with 292 additions and 28 deletions

View File

@@ -24,16 +24,18 @@ You can browse the source code of {spring-boot-autoconfigure-module-code}[`sprin
[[features.developing-auto-configuration.locating-auto-configuration-candidates]]
=== Locating Auto-configuration Candidates
Spring Boot checks for the presence of a `META-INF/spring.factories` file within your published jar.
The file should list your configuration classes under the `EnableAutoConfiguration` key, as shown in the following example:
Spring Boot checks for the presence of a `META-INF/spring-boot/org.springframework.boot.autoconfigure.AutoConfiguration` file within your published jar.
The file should list your configuration classes, as shown in the following example:
[indent=0]
----
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mycorp.libx.autoconfigure.LibXAutoConfiguration,\
com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration
com.mycorp.libx.autoconfigure.LibXAutoConfiguration
com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration
----
TIP: You can use comments via `#` in this file.
NOTE: Auto-configurations must be loaded that way _only_.
Make sure that they are defined in a specific package space and that they are never the target of component scanning.
Furthermore, auto-configuration classes should not enable component scanning to find additional components.

View File

@@ -769,13 +769,18 @@ include::code:MyJdbcTests[]
NOTE: Make sure to not use the regular `@Import` annotation to import auto-configurations as they are handled in a specific way by Spring Boot.
Alternatively, additional auto-configurations can be added for any use of a slice annotation by registering them in `META-INF/spring.factories` as shown in the following example:
Alternatively, additional auto-configurations can be added for any use of a slice annotation by registering them in a file stored in `META-INF/spring-boot` as shown in the following example:
.META-INF/spring-boot/org.springframework.boot.test.autoconfigure.jdbc.JdbcTest
[indent=0]
----
org.springframework.boot.test.autoconfigure.jdbc.JdbcTest=com.example.IntegrationAutoConfiguration
com.example.IntegrationAutoConfiguration
----
In this example, the `com.example.IntegrationAutoConfiguration` is enabled on every test annotated with `@JdbcTest`.
TIP: You can use comments via `#` in this file.
TIP: A slice or `@AutoConfigure...` annotation can be customized this way as long as it is meta-annotated with `@ImportAutoConfiguration`.