Video summary

Mastering Spring @ComponentScan Annotation: Deep Dive with Examples

Main summary

Key takeaways

Technology

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 @Service and @Controller).
  • Classes that match are converted into Spring beans, making them available for dependency injection.

Why @ComponentScan matters

  • @ComponentScan defines 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 @ComponentScan is typically included indirectly via the annotation stack for @SpringBootApplication.
  • Because of this, you may not see @ComponentScan directly on your application class.

Example application used in the walkthrough

  • Package structure resembles something like com.codsnippet, with an Ecom package.
  • Key elements include:
    • A @Controller (provides an endpoint)
    • A @Service (for example, ProductService)
    • Services implementing interfaces (e.g., PaymentService with implementations like GooglePayService and CreditCardService)
    • A configuration class that manually creates beans for some implementations using @Bean

How the tutorial demonstrates bean discovery

  • ApplicationContext: checking beans programmatically
  • Spring Boot Actuator: using the /actuator/beans endpoint to list beans

Tutorial demonstrations (key behaviors)

1. Default scan scope

  • The app class is in the Ecom package, and @ComponentScan is 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 basePackages can 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)

3. Correct use with configuration classes

  • The video highlights that using @ComponentScan without proper setup may not work as expected.
  • Rule emphasized: @ComponentScan should be used together with @Configuration in 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.

Filter strategies mentioned include:

  • AssignableTypeFilter (filter by assignable type/class)
  • Annotation-based filters
  • AspectJ, Regex, and other strategies (briefly referenced)

Key takeaway

  • @ComponentScan controls 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 excludeFilters to prevent specific classes from becoming beans
    • use a dedicated @Configuration + @ComponentScan class when customizing scanning behavior

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/beans endpoint).

Original video