DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Trending

  • AI Agents Need an Identity Plane: Rethinking Enterprise Authorization for Autonomous Software
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • YAGNI in the Age of AI Coding
  • Should Developers Switch From SQLite to DuckDB?

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. How Does Spring Boot Auto-Configuration Work?

How Does Spring Boot Auto-Configuration Work?

In this article, we take a look at how Spring Boot auto-configuration makes it easier than ever to have your beans configured for you.

By 
hua liang
·
Apr. 09, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

What Is It?

As its name implies, "autoconfiguration" helps you to configure your Spring Boot application automatically. More precisely, it automatically create beans for you! Strangely, it is a cool feature, but not applicable outside of Spring Boot. 

The Purpose and Method

The purpose is simple: to save you time when you create beans, which is usually done by yourself. It sounds like magic, but behind the scene, it's nothing fancy actually. 

It is simply a bunch of normal Spring configuration classes (like others with the same annotation @Configuration). 

The difference is that these configuration classes are using some different annotations:

  •  @ConditionalOnClass: acts only if a given class is on a classpath
  •  @ConditionalOnMissingBean: acts only if a given bean is not created
  •  @Conditional: acts only if given condition is met

In one sentence, generally, if a class is found in a classpath (@ConditionalOnClass) , and a bean is requested (@Autowired), but no others is creating it ( @ConditionalOnMissingBean), it'll create that bean. 

Let's have a look at one example:

Java
 




xxxxxxxxxx
1
15


 
1
@Configuration(proxyBeanMethods = false)
2
@ConditionalOnClass({ MongoClient.class, ReactiveMongoTemplate.class })
3
@ConditionalOnBean(MongoClient.class)
4
@EnableConfigurationProperties(MongoProperties.class)
5
@Import(MongoDataConfiguration.class)
6
@AutoConfigureAfter(MongoReactiveAutoConfiguration.class)
7
public class MongoReactiveDataAutoConfiguration {
8

          
9
    @Bean
10
    @ConditionalOnMissingBean(ReactiveMongoDatabaseFactory.class)
11
    public SimpleReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory(MongoProperties properties,
12
            MongoClient mongo) {
13
        String database = properties.getMongoClientDatabase();
14
        return new SimpleReactiveMongoDatabaseFactory(mongo, database);
15
    }



What Does the Process Look?

You are asking for a bean, by using an annotation like  @Autowire, Spring BeanFactory tries to find the bean for you.

If you have declared a bean by yourself (i.e.  @Configuration, @Component, @Service, etc.), Spring will pick that up and instantiate it for you. Otherwise, Spring will look into those auto-configuration classes (still @Configuration but with a @Conditional annotation), to find the suitable bean and instantiate it for you.

If still nothing is found, an exception is thrown by Spring, like NoSuchBeanDefinitionException.

How Do I Use This Feature, and Is There Anything I Can Do?

It is used via the annotation @EnableAutoConfiguration, which is included in  @SpringBootApplication already.

Spring does, however, leave options for you to have some control:

  1. You can tell Spring to disable certain auto-configuration classes.  (i.e.  @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) )
  2. You can tell Spring to include your own auto-configuration class. 
  3. You can declare your own bean, which will be respected by Spring and auto-configuration class will be ignored. This is done via @ConditionalOnMissingBean.
Spring Framework Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook