๐ง How Does Spring Boot Really Find Stuff?! (And How @SpringBootApplication + META-INF/spring.factories Do Their Secret Scan Magic)
๐ต๐ซ When I started working in Spring Boot, I had a silly doubt… but it unlocked a serious concept.
I knew that @SpringBootApplication
does a lot — it’s the entry point of every Spring Boot app.
So far, I only knew the basics:
@SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
But one day I asked myself:
๐ญ “How does Spring Boot load classes from external libraries like
spring-security
,spring-web
, or even a custom JAR I wrote and added in dependencies?
I never wrote any@ComponentScan
for them… so how are their beans magically appearing inside my Spring Context?”
Yeah yeah… people say “Auto-configuration” or “Classpath scanning.” But how? Let’s deep dive ๐
๐ช Let’s First Talk About @ComponentScan
By default, this only scans packages below the class where @SpringBootApplication
is placed.
So if your MainApp.java
is in com.anand.app
, it will scan:
com.anand.app.*
But will it scan classes inside:
org.springframework.security.config.annotation.*
๐ค Nope. Not unless Spring does something behind the scenes.
๐งณ Then How Are External JARs Loaded?
Let’s take Spring Web as an example.
You just add this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
๐ฅ Boom! You get a running server, a DispatcherServlet
, Jackson for JSON, exception resolvers… like magic.
But where’s the magic wand?
That wand is called:
META-INF/spring.factories
๐️ What’s This spring.factories
?
Inside each Spring Boot starter JAR, you’ll find:
META-INF/spring.factories
This file tells Spring Boot:
“Hey, I have some config classes to load. Even if the user didn’t scan me manually.”
Example from spring-boot-autoconfigure
:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\ ...more...
This is how Spring Boot “auto-magically” adds beans — they’re listed here and picked up by @EnableAutoConfiguration
.
๐งช So What Does @EnableAutoConfiguration
Actually Do?
It does the heavy lifting. Here’s how:
-
Scans the classpath for
META-INF/spring.factories
-
Loads all classes listed under
EnableAutoConfiguration
-
Instantiates them as configuration classes
-
Each of them adds beans conditionally using annotations like:
@Bean
@ConditionalOnClass(ObjectMapper.class)
So:
-
If Jackson is present → JSON config is loaded.
-
If not → config is skipped.
✨ It’s intelligent — it won’t load everything blindly.
๐ Spring 2.7+ — What Changed?
Starting with Spring Boot 2.7+, this mechanism evolved for performance and modularity.
Now many starters use:
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This is part of the newer @AutoConfiguration
mechanism using AutoConfigurationImportSelector
.
But the idea is the same — it loads stuff without requiring you to @ComponentScan
them.
๐งช Real Example — How DispatcherServlet
Comes In
Ever used a simple @RestController
and hit it in browser?
You never configured a DispatcherServlet
, right?
But this class is magically loaded from:
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration
Which comes from:
spring.factories > EnableAutoConfiguration
Inside, it does:
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
Spring Boot sees this during auto-config loading and registers it as a servlet.
๐ Boom. Your REST controller works.
๐คน Custom JARs Work the Same Way!
Want to create your own reusable Spring Boot library?
✅ Add your @Configuration
class
✅ Put it in:
src/main/resources/META-INF/spring.factories
Like this:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.mycompany.autoconfig.MyCustomConfig
Now any project that adds your JAR will have this config loaded.
๐ฅ That’s Why Just Adding a Starter Works
Whether it’s:
-
spring-boot-starter-data-jpa
-
spring-boot-starter-security
-
resilience4j-spring-boot2
…they all use either:
-
spring.factories
(older) -
or
AutoConfiguration.imports
(newer)
No manual scanning. No XML. Just plugin and go.
๐ Wrapping Up
This small doubt I had while exploring Spring Boot opened up a whole world of internal magic — how Spring loads, scans, wires, and bootstraps everything without you even noticing.
Next time you wonder,
“How did that bean get in here?”
Remember: Spring invited it through a secret VIP list ๐️ in META-INF
.
๐ฌ Did this help you clear some Spring mystery?
Tell me if you ever faced confusion like this — I’ll try to explain it with more examples .
Stay curious, stay caffeinated ☕ and keep debugging.
— Anand ๐ง๐ป
- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Comments
Post a Comment