Skip to the workspace
Pebellslearn by building
Auto-configuration  ›  Capstone Project

Overriding and Debugging Auto-configuration

Autoconfiguration 14 minThe condition evaluation report explains what Boot configured and why, and you override it with a property, your own bean, or an exclude, gentlest first.

spring boot / Overriding and Debugging Auto-configuration

Explore each scene at your own pace.

01 · The rule

The rule

Overriding and Debugging Auto-configuration

A teammate adds the H2 database to library-service for quick experiments and forgets about it. Weeks later, the app starts on a server without its PostgreSQL settings and, instead of failing loudly, quietly runs on an empty in-memory database. Auto-configuration did exactly what it was told. When Boot's choices surprise you, you need two skills: seeing what it decided, and changing it.

The rule

Ask the report why; then override with a property, a bean, or an exclude.

Seeing what Boot decided. Start the app with --debug (or set debug=true) and Boot prints a condition evaluation report:

============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
   DataSourceAutoConfiguration matched:
      - @ConditionalOnClass found required classes 'javax.sql.DataSource' ...
Negative matches:
   MongoDataAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'com.mongodb.client.MongoClient'

Positive matches were applied; negative matches were skipped, each with the reason. With Actuator, the same report is available at /actuator/conditions, and /actuator/beans lists every bean in the context.

Changing what Boot decided, from gentlest to strongest:

  1. A property. Most auto-configured beans read properties: server.port, spring.datasource.url, spring.jpa.open-in-view=false. Try this first.
  2. Your own bean. Define a bean of the same type and @ConditionalOnMissingBean makes Boot's version back off.
  3. An exclude. Switch off a whole auto-configuration class:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class LibraryApplication { ... }

or, without touching code, spring.autoconfigure.exclude= followed by the class's full name.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.