Video summary
Mastering Spring @ComponentScan Annotation: Deep Dive with Examples
Main summary
Key takeaways
Overview
This video is a deep dive tutorial on Spring’s @ComponentScan / component scanning, explaining how Spring Boot automatically discovers and registers beans in the application context.
Main concepts covered
What component scanning is (core idea)
- When a Spring application starts, Spring initializes the IoC container / application context.
- It then scans Java classes/packages for metadata (such as annotations like
@Serviceand@Controller). - Classes that match are converted into Spring beans, making them available for dependency injection.
Why @ComponentScan matters
@ComponentScandefines where Spring looks for components (beans).- If scan scope isn’t controlled, Spring may scan unintended classes (such as test code), so scan boundaries are important.
How Spring Boot uses it “behind the scenes”
- The video notes that
@ComponentScanis typically included indirectly via the annotation stack for@SpringBootApplication. - Because of this, you may not see
@ComponentScandirectly on your application class.
Example application used in the walkthrough
- Package structure resembles something like
com.codsnippet, with anEcompackage. - Key elements include:
- A
@Controller(provides an endpoint) - A
@Service(for example,ProductService) - Services implementing interfaces (e.g.,
PaymentServicewith implementations likeGooglePayServiceandCreditCardService) - A configuration class that manually creates beans for some implementations using
@Bean
- A
How the tutorial demonstrates bean discovery
ApplicationContext: checking beans programmatically- Spring Boot Actuator: using the
/actuator/beansendpoint to list beans
Tutorial demonstrations (key behaviors)
1. Default scan scope
- The app class is in the
Ecompackage, and@ComponentScanis effectively tied to that scope. - If the speaker adds a new annotated class in a different package (e.g.,
EcomAssistant...):- it is not found/initialized
- no corresponding bean appears in
/actuator/beans
2. Expanding scan scope with basePackages
- Adding
@ComponentScan(basePackages = "...")allows scanning an additional package (example:com.codsnippet.EcomAssistant). - Nuance shown:
- using
basePackagescan result in Spring scanning only what you explicitly list - previously discovered beans might disappear unless you include both packages (e.g., via a comma-separated list / array-like configuration)
- using
3. Correct use with configuration classes
- The video highlights that using
@ComponentScanwithout proper setup may not work as expected. - Rule emphasized:
@ComponentScanshould be used together with@Configurationin a separate configuration class. - After annotating the config class appropriately (with
@Configuration), the additional package’s beans are discovered.
Exclusions / filters (excludeFilters)
- After scanning a package containing multiple service classes, the tutorial demonstrates excluding specific components using
excludeFilters. - Example:
- A class annotated with
@Service(e.g., “DeprecatedUtilityService”) is excluded, so it no longer becomes a bean.
- A class annotated with
Filter strategies mentioned include:
AssignableTypeFilter(filter by assignable type/class)- Annotation-based filters
- AspectJ, Regex, and other strategies (briefly referenced)
Key takeaway
@ComponentScancontrols package scanning boundaries—Spring creates beans only from classes within scanned packages.- Spring Boot simplifies this by including component scanning via
@SpringBootApplication. - You can:
- add extra scan locations using
basePackages - use
excludeFiltersto prevent specific classes from becoming beans - use a dedicated
@Configuration+@ComponentScanclass when customizing scanning behavior
- add extra scan locations using
Main speakers / sources
- Main speaker: Not explicitly named in the subtitles (presenter referenced as instructional/code snippet channel).
- Sources referenced in-video: Spring Framework / Spring Boot, and Spring Boot Actuator (the
/actuator/beansendpoint).