🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
@Component, @Service, or @Controller, and Spring picks it up. No XML, no manual config.But how does this actually work? How does Spring know which classes to scan, and how does it register them as beans?
In this article, we’ll break down how Spring’s component scanning mechanism works behind the scenes — step-by-step — so you can understand what’s really going on when your app starts.
What Is Component Scanning?
Component scanning is a feature in Spring that automatically detects classes annotated with specific stereotypes (like @Component, @Service, @Repository, @Controller) and registers them as beans in the Spring container.
You activate component scanning using:
@ComponentScan("com.example")Or implicitly via Spring Boot:
@SpringBootApplication // includes @ComponentScan(basePackages = "your.package")What Spring Looks For
During scanning, Spring searches for classes with these annotations:

These are all meta-annotated with @Component, so Spring recognizes them as candidates.
How Component Scanning Works (Behind the Scenes)
Let’s break it down step-by-step:
1. Spring Boot / ApplicationContext Starts
When your app launches, Spring creates an ApplicationContext — the core container.
If you’re using Spring Boot:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}👉 Behind the scenes, @SpringBootApplication includes:
@Configuration@EnableAutoConfiguration@ComponentScan
This triggers component scanning in the current package and sub-packages.
2. Spring Finds the Base Package
@ComponentScan("com.example") tells Spring where to look for components.
If no package is defined, Spring uses the package of the class annotated with @SpringBootApplication.
3. Spring Uses ClassPathScanningCandidateComponentProvider
This internal class:
- Walks through all classes in the package
- Applies a filter to check if a class has
@Componentor meta-annotations - Marks it as a candidate for registration
Example filters used:
AnnotationTypeFilter(Component.class)— includes@Service,@Controller, etc.- Custom include/exclude filters if defined
4. Creates a BeanDefinition
For every eligible class found, Spring creates a BeanDefinition, which stores:
- Bean class name
- Scope
- Lifecycle callbacks
- Dependencies
These are blueprints that will be used to instantiate actual beans later.
5. Registers the BeanDefinition
Spring registers these definitions with the BeanDefinitionRegistry.
It doesn’t instantiate the beans yet — just prepares the metadata for later.
6. Bean Instantiation Happens Later
When the container is refreshed, Spring goes through all bean definitions and:
- Instantiates the classes
- Injects dependencies
- Applies lifecycle annotations (
@PostConstruct, etc.) - Adds them to the context
Now your beans are available via @Autowired, @GetBean(), etc.
Example Walkthrough
@Component
public class EmailService {
...
}
@Service
public class UserService {
...
}If your main app class is in com.example:
@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}Then Spring will:
- Look in
com.exampleand sub-packages - Find
EmailServiceandUserService - Register them as beans
- Make them available for injection
⚙️ Advanced: Customizing Component Scan
Include/Exclude filters:
@ComponentScan(
basePackages = "com.example",
includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyCustomAnnotation.class),
excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com\\.example\\.legacy\\..*")
)Exclude default scanning behavior:
@ComponentScan(useDefaultFilters = false)🔄 Summary Flowchart
@SpringBootApplication
↓
@ComponentScan kicks in
↓
Spring scans package(s)
↓
Finds classes with @Component / @Service / ...
↓
Creates BeanDefinitions
↓
Registers them in BeanFactory
↓
Beans are created, injected, and ready to useFinal Thoughts
Component scanning is one of Spring’s most powerful features — and now you know exactly how it works behind the curtain. With just a few annotations, Spring does all the heavy lifting to wire your app together.
The magic isn’t in the annotation — it’s in what Spring does when it sees that annotation.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment