Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

When Boot does something unexpected, report first, fix second, and use the gentlest fix that works. A property changes one detail, a bean replaces one object, an exclude removes a whole feature, so reach for them in that order.

The silent H2 databaseStep 1 of 4
  1. Run the report

    Start with --debug. Under positive matches, the embedded database configuration matched: no spring.datasource.url was given and H2 was on the classpath.

The trap is jumping straight to exclude for every surprise. Excluding the data source auto-configuration to escape H2 would also switch off the PostgreSQL setup you need.

Drills

Answer fast — trust the rule.

quick drill

How do you see which auto-configurations were applied and why?

quick drill

You only want the app on port 9090 instead of 8080. What is the gentlest fix?

quick drill

How do you switch off one auto-configuration class completely?

quick drill

In the condition report, what does a negative match mean?

Remember

  • Ask the report why; then override with a property, a bean, or an exclude.
  • Report first, fix second, gentlest fix first: property, then bean, then exclude.
  • Trap: excluding a whole feature to fix one detail.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainDebug a surprise

After adding a library, library-service suddenly exposes a new bean you did not expect. Explain how you would find out which auto-configuration created it and why, and walk through the three ways to change it, saying when each is the right choice.