October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

Spring Boot Under the Hood, Part 5: How Auto-configuration Works

Spring Boot considers auto-configuration candidates from dependencies, applies conditions against your classpath, properties, and beans, and often lets your own bean replace a default. Learn how to inspect matches, customize defaults, and exclude a configuration safely.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Spring Boot auto-configuration uses your application’s classpath, environment, and existing beans to decide which configuration classes should contribute bean definitions. It is enabled by @EnableAutoConfiguration, included in @SpringBootApplication; it is conditional rather than a fixed set of defaults, and an application-defined bean can often take the place of a Boot-provided one.

How does Spring Boot opt into auto-configuration?

A typical application starts with @SpringBootApplication. That annotation combines @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan. The first marks the application’s configuration class, the second enables Boot’s auto-configuration, and the third discovers application components. You can use the annotations separately when you need more control over that arrangement. See the Spring Boot annotation reference.

As an Amazon Associate I earn from qualifying purchases.

Auto-configuration is not the same thing as component scanning. Scanning finds application components in packages; auto-configuration considers configuration candidates made available by dependencies.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Where do auto-configuration candidates come from?

Dependencies can publish auto-configuration classes in the resource META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports, with one class name per line. When auto-configuration is enabled, Spring Boot considers the listed candidates. A candidate is not automatically applied just because it is present: its conditions must match. The current auto-configuration authoring guide documents this discovery mechanism.

This is why adding a library can change what an application is capable of configuring, but does not necessarily cause every feature in that library to activate. The library contributes candidates; conditions determine which ones apply in the current application.

How do conditions decide what gets configured?

Conditions test whether an auto-configuration or an individual bean definition is appropriate for the application. A configuration may require a library class to be available, a property to have a particular value, or a particular kind of application context. The guide documents conditions for classes, beans, properties, resources, web-application types, WAR deployment, and SpEL expressions.

Class and bean conditions

@ConditionalOnClass and @ConditionalOnMissingClass check whether classes are present or absent. @ConditionalOnBean and @ConditionalOnMissingBean test the application context. A common pattern is to provide a default bean only when the application has not already supplied a suitable one.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Bean conditions depend on which bean definitions have been processed. For that reason, Spring Boot’s authoring guide recommends applying them to auto-configuration classes, which are loaded after user-defined bean definitions. Optional class references also need care: class-level conditions and conditions on @Bean methods have different class-loading implications. When an optional type might be absent, place references to it in a separately conditioned configuration class.

Property and environment conditions

@ConditionalOnProperty checks environment properties. By default, a present value other than false matches; havingValue and matchIfMissing let configuration specify the expected value and whether an absent property should count as a match. The guide also documents @ConditionalOnBooleanProperty. Check the documentation for your project’s Spring Boot version before using an annotation, since availability can vary by version.

Other condition types

Resource conditions check whether a resource exists. Web conditions distinguish servlet and reactive applications, and deployment conditions can distinguish traditional WAR deployment. Expression conditions evaluate SpEL. Together, these tests let a library offer configuration only in the environments where it is relevant.

What happens when a condition matches?

A matched auto-configuration contributes bean definitions to the application context. That does not mean every corresponding bean instance is created immediately: ordinary bean creation still depends on its dependencies and Spring’s lifecycle.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Auto-configuration ordering controls when configuration definitions are added, not the later order in which bean instances are created. Ordering metadata is useful when one configuration’s definitions genuinely depend on another’s being processed first; it is not a general way to control runtime initialization.

How can your application replace a Boot default?

Spring Boot describes auto-configuration as “non-invasive.” In practice, many defaults are conditional on the absence of an application-provided bean. For example, the reference guide explains that defining a DataSource causes default embedded-database support to back away. For other defaults, check the relevant configuration and condition rather than assuming every bean is replaceable in the same way. See the auto-configuration reference.

When deciding how to customize behavior, use the narrowest supported control that fits the need:

  • Replace one default: define the relevant application bean when the auto-configuration is designed to yield to it.
  • Change a setting: use the documented configuration property when the behavior is property-driven.
  • Remove a whole configuration: exclude it when the configuration itself should not apply.

Prefer documented properties and public auto-configuration class names. Internal nested configuration classes and bean methods are not stable exclusion targets.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How do you find out why Spring Boot configured a bean?

Start the application with --debug to produce a conditions report. Inspect both positive and negative outcomes to see which configurations matched and which conditions did not. This is more useful than guessing from a bean’s name: it shows whether the relevant class, property, bean, or application type satisfied a condition.

  1. Run the application with the --debug command-line argument.
  2. Find the conditions report in the startup output.
  3. Locate the relevant auto-configuration and inspect its positive and negative matches.
  4. Use the matching condition to choose a property or user bean customization; exclude the configuration only if it should not apply at all.

How do you exclude an auto-configuration?

Spring Boot supports exclusions through annotation attributes or the spring.autoconfigure.exclude property. Use the public auto-configuration class name, and confirm that it exists in the Spring Boot version used by the project.

An exclusion is broader than replacing a single default bean: it removes the named auto-configuration from consideration. Diagnose the condition report first, and use a bean or property customization instead when that is the intended extension point. The auto-configuration reference documents both exclusion approaches and cautions against relying on internal nested classes or bean methods.

How should library authors provide auto-configuration?

For a reusable library, publish auto-configuration classes through META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports rather than relying on component scanning to find them. The auto-configuration classes should not be discovered through component scanning and should not enable component scanning themselves; use explicit @Import for additional configuration where needed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Apply conditions at the configuration-class or bean-method level, and use ordering only when there is a real dependency between bean definitions. Test combinations of classpath contents, environment properties, and user-defined beans in controlled contexts with ApplicationContextRunner. The authoring guide covers these practices.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.