Skip to content

@ImportAutoConfiguration and @EnableAutoConfiguration import ordering #12366

@bclozel

Description

@bclozel

While testing for #12318, @snicoll found a limitation in the current ordering between @ImportAutoConfiguration and @EnableAutoConfiguration.

Consider the following test application:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {
		WebTestClientSpringBootTestIntegrationTests.TestConfiguration.class,
		ExampleWebFluxApplication.class })
@AutoConfigureWebTestClient
public class WebTestClientSpringBootTestIntegrationTests {
}

This test starts a Spring Boot application with all auto-configurations and use the @AutoConfigureWebTestClient test annotation. The current issue is that all auto-configurations imported by @AutoConfigureWebTestClient cannot rely on @AutoConfigureAfter/@AutoConfigureBefore, since things are processed in two different phases.

Currently:

  • @EnableAutoConfiguration is using AutoConfigurationImportSelector, which is ordered at Ordered.LOWEST_PRECEDENCE - 1
  • @ImportAutoConfiguration is using ImportAutoConfigurationImportSelector (itself extending from AutoConfigurationImportSelector), which is ordered at super.getOrder() - 1, so Ordered.LOWEST_PRECEDENCE - 2.

Because of this, ImportAutoConfigurationImportSelector auto-configurations are processed before Spring Boot auto-configurations.

Solutions

Setting ImportAutoConfigurationImportSelector::getOrder to Ordered.LOWEST_PRECEDENCE fixes the issue, and the whole Spring Boot build is green.

Doing that change would imply that:

  1. Ordering and bean conditions (like @AutoConfigureBefore/@AutoConfigureAfter) can only be applied to a set of auto-configurations imported by the same selector. So for @ImportAutoConfiguration, those are only applied within the scope of test slicing annotations.
  2. In the case of @SpringBootTest, we agree that @ImportAutoConfiguration should always run after the Spring Boot auto-configurations and that ordering and bean conditions on test auto-configurations won't apply to auto-configurations contributed by @EnableAutoConfiguration

Flagging this as for:team-attention.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions