DEV Community

Narednra Reddy Yadama
Narednra Reddy Yadama

Posted on

๐Ÿš€ Auto-Configuration in Spring Boot โšก

๐Ÿ‘‰ Spring Boot AutoConfiguration = less manual config + faster development.

When we call SpringApplication.run(..,..) from our main class (which also serves as the Configuration class) โ€” a lot happens behind the scenes ๐Ÿ‘€

One of the most powerful operations is AutoConfiguration โ€” where Spring Boot automatically configures beans based on the starters/jars youโ€™ve added to your classpath.


Hereโ€™s how it actually works ๐Ÿ‘‡

๐Ÿงฉ AutoConfiguration Process (Step by Step)

๐Ÿ” The IoC container scans all JARs in the classpath to find a file named spring.factories.
๐Ÿ“‚ It locates this file in multiple JARs.

๐Ÿง  Inside each, it looks for the key:
org.springframework.boot.autoconfigure.EnableAutoConfiguration
โš™๏ธ In the spring-boot-autoconfigure-.jar, it collects all the values mapped to that key โ€” which are pre-built configuration classes such as:

๐Ÿ‘‡
org.springframework.boot.autoconfigure.EnableAutoConfiguration=/,
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfig

๐Ÿ” Spring then processes each of these configuration classes (including their nested and imported @Configuration classes).
๐Ÿงฑ It executes all their @bean methods to create ready-to-use beans โ€” like HikariDataSource, JdbcTemplate, etc.

Thatโ€™s how Spring Boot gives you so much out-of-the-box power ๐Ÿ”ฅ


๐Ÿ›‘ Can We Disable Specific AutoConfigurations?

โœ… Absolutely.
Even if youโ€™ve added certain starters, you can stop specific auto-configurations by using the exclude parameter in @SpringBootApplication:

Example ๐Ÿ‘‡
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class })
public class BootProjRealtimeDiAutoConfigurationApplication {
// ...
...
}

๐Ÿ’ฌ In short:
Spring Boot AutoConfiguration = less manual config + faster development.
And when you need control, Spring gives it back โ€” effortlessly. ๐Ÿ™Œ

Top comments (0)