liveBook · Manning

Spring Boot autoconfiguration

As explained in Testing Spring Boot Applications

Auto-configuration is Spring Boot's automatic configuration of application components and library integrations based on the available environment and configuration.

Spring Boot autoconfiguration configures application components and library integrations from the available environment and configuration. Classpath scanning discovers Spring-annotated classes and adds them to the application context as beans, reducing manual setup. section 1.3 section 1.2

The convenience becomes a constraint when an automatically discovered component is unsuitable, such as a network-calling service that tests must replace with a stub. Using only @SpringBootApplication does not provide enough control; a custom configuration must select relevant autoconfiguration, exclude unwanted configurations, and define the replacement bean. section 1.3

Replacing an automatically discovered bean

A test configuration can use @EnableAutoConfiguration, scan the application package while excluding @SpringBootApplication and WidgetValidator, load test properties, and register a StubWidgetValidator bean. This preserves useful Boot configuration while preventing the real validator from entering the test context. section 1.3

Spring Boot test tooling handles much of this application-context work and can validate that properties, annotations, and configured components cooperate correctly. These tests are slower than unit tests but exercise broader behavior, usually without requiring external services because dependencies can be represented by mocks and stubs. section 1.2

Listing 1.2 A sample configuration for a "test application", without Spring Boot.
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = WidgetApplication.class,
    excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = { SpringBootApplication.class }),
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = { WidgetValidator.class }), })
@PropertySource("classpath:test.properties")
static class TestConfiguration {

  @Bean
  public WidgetValidator testWidgetValidator() {
    return new StubWidgetValidator();
  }

}

// ... your test code ...

Use the configuration listing to inspect the annotations and bean definition required when automatic discovery must be constrained for testing. section 1.3

How Testing Spring Boot Applications covers it

How deep it goes
Explains classpath discovery, bean registration, test exclusions, and replacement configuration; stops short of describing autoconfiguration internals.
Examples
Uses a discovered WidgetValidator, a StubWidgetValidator replacement, test properties, and an annotated custom test configuration.
What it assumes
Assumes familiarity with Spring annotations, beans, component scanning, application contexts, and test doubles.
What you can do after
Configure test application contexts that retain useful autoconfiguration while excluding unsuitable components and substituting programmable stubs.
How it connects
Connects autoconfiguration with Spring beans, component scanning, application properties, mocks, stubs, and integration testing.

Also explained in

Compare how 2 books explain Spring Boot autoconfiguration →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Testing Spring Boot Applications. The section links point to the parts of the book it draws on.