Auto-configuration means Spring Boot creates the usual beans for you, based on what is on the classpath: the jars your app is built with. It is why a web app gets a running server without any server code. Once you know how Boot decides, you can change what it built, give your own library the same setup, turn one auto-configuration off, and test it.

How Boot decides

@SpringBootApplication switches auto-configuration on. Each Boot jar carries a short list of auto-configuration classes. These are ordinary configuration classes with conditions on them. At startup, Boot reads every list and keeps only the classes whose conditions are true.

Our small shop app has one class and one annotation, yet Tomcat starts on port 8080. To see why, start it with --debug. Boot prints the conditions evaluation report: every auto-configuration it checked, sorted into positive matches (applied) and negative matches (skipped), each with a reason.

The server came from TomcatServletWebServerAutoConfiguration. Two conditions on it decide almost everything:

  • @ConditionalOnClass: build this only if certain classes are on the classpath. The web starter brought Tomcat, so it matched. Gson is not on our classpath, so Boot skipped its Gson setup.
  • @ConditionalOnMissingBean: build this bean only if the app has none of its own. Boot creates its Tomcat factory only when you have not defined one.

Your bean wins where Boot backs off

Add a factory of our own and watch Boot step aside:

@Bean
TomcatServletWebServerFactory shopServer() {
    return new TomcatServletWebServerFactory(7070);
}

ShopApp.java

Tomcat now starts on 7070, and the report moves Boot’s factory to negative matches. Boot applies auto-configuration after it has registered your own beans, so it sees yours in time.

This only works where Boot’s bean carries @ConditionalOnMissingBean. The report shows you where that is.

Change what Boot built, keep the rest

Replacing the factory also replaced everything Boot set up on its server. To change one thing, try a property first: server.servlet.context-path=/shop puts every address under /shop, and Boot still builds the server. The Common Application Properties list names these settings.

When no property fits, add a customizer: a bean that changes something Boot builds without replacing it. Here the shop opens a second port, 8081, for an internal stock tool:

@Bean
WebServerFactoryCustomizer<TomcatServletWebServerFactory> secondPort() {
    return factory -> {
        Connector connector = new Connector();
        connector.setPort(8081);
        factory.addAdditionalConnectors(connector);
    };
}

ShopApp.java

Boot passes its own Tomcat factory to the customizer. Boot applies your properties through a customizer too, and that one runs first, so the context path and the extra port both survive.

Make your own library set itself up

Our price library creates a dollar PriceFormatter in a plain configuration class. The shop adds the library, yet startup fails: there is no PriceFormatter bean. Component scanning only searches the shop’s own packages, and a library cannot know where each app keeps its classes.

So the library does what Boot’s jars do. It depends on spring-boot-autoconfigure, marks its class @AutoConfiguration, and names that class in its own list, the file META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. Now the shop shows coffee in dollars.

When the shop declares its own euroPrices formatter, startup fails again, this time with two candidates. A good library steps aside on its own:

@Bean
@ConditionalOnMissingBean
PriceFormatter priceFormatter() {
    return new PriceFormatter("USD");
}

PriceConfiguration.java

The shop now shows euros, and a shop without its own formatter still gets dollars.

Turn one auto-configuration off

Adding the JDBC starter before the database is ready stops the shop with “Failed to configure a DataSource”. DataSourceAutoConfiguration saw the jar and tried to build a data source with no URL. Keep the starter and exclude just that class:

spring.autoconfigure.exclude=org.springframework.boot.jdbc.autoconfigure.DataSourceAutoConfiguration

application.properties

The shop starts without a database. @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) does the same in code, and either way the Exclusions section of the --debug report lists the class.

Copy the exact class name from the jar’s own list. Many older examples use an older package name, and with that name nothing is turned off.

Test both cases

The shop’s own test starts the whole shop, so it only checks the one case the shop happens to be in. ApplicationContextRunner starts a tiny inside a test, with only the library’s PriceConfiguration, so each test can set up exactly the case it needs.

The first test adds nothing and checks for dollars. The second adds the app’s own formatter:

this.runner.withBean("euroPrices", PriceFormatter.class, () -> new PriceFormatter("EUR"))
    .run((context) -> assertThat(context.getBeansOfType(PriceFormatter.class)).containsOnlyKeys("euroPrices"));

PriceConfigurationTest.java

Only euroPrices exists, so the library stepped aside. If someone deletes @ConditionalOnMissingBean, this test fails long before a real app meets two formatters. See Creating Your Own Auto-configuration for more.

Recap

  • Auto-configuration builds the usual beans from what is on the classpath; --debug prints why each one applied or was skipped.
  • Prefer a property, then a customizer. Your own bean wins only where Boot’s bean has @ConditionalOnMissingBean.
  • A library should auto-configure itself through the imports file and step aside with @ConditionalOnMissingBean.
  • Turn one auto-configuration off with spring.autoconfigure.exclude or exclude, using the exact class name.
  • Test both cases with ApplicationContextRunner.

Sources

Official documentation and well-known tutorials read while writing this article. The wording here is our own.