How Spring @Component & @ComponentScan Work |Video upload date:  · Duration: PT9M28S  · Language: EN

Learn how Spring @Component and @ComponentScan let Spring Boot discover and wire beans and how to control scanning and injection

Quick verdict

If you want Spring to find your beans so you can stop wiring them by hand like it is 2003, use @Component and @ComponentScan and let Spring Boot do the heavy lifting. This guide explains what those annotations do how beans get registered and why constructor injection is the one you should actually trust when writing tests.

What @Component actually means

Annotate a plain Java class with @Component or a specialized stereotype such as @Service or @Repository and Spring treats the class as a candidate for bean creation. That means Spring will create an instance and manage its lifecycle inside the ApplicationContext. It saves you from XML rituals and from writing a factory for every tiny helper class.

Example component

@Component
public class GreetingService {
    public String greet(String name) {
        return "Hello " + name
    }
}

How scanning finds your classes

@ComponentScan makes Spring walk packages looking for annotated types and register them as beans. In Spring Boot you usually do not need to add @ComponentScan manually because auto configuration and the default package scanning from your main class handle the common cases. If your classes sit outside the main package then use explicit basePackages or basePackageClasses on @ComponentScan.

Typical configuration

@Configuration
@ComponentScan(basePackages = "com.example.app")
public class AppConfig {
}

// Or in Spring Boot the main application can be enough
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args)
    }
}

What happens during bean registration

When the ApplicationContext starts Spring scans for annotated classes instantiates them applies post processors and registers the resulting bean definitions. Bean names follow rules derived from the class or the explicit name you provide on the annotation. Bean post processors and lifecycle callbacks run so any AOP proxying or init methods are applied before your bean gets handed off to the wiring team.

Injection patterns and practical advice

Prefer constructor injection because it makes dependencies explicit helps with immutable design and plays nicely with unit testing. Field injection is convenient for toy examples but causes headaches when you try to instantiate objects without the whole Spring context during tests.

  • Constructor injection makes mandatory dependencies clear
  • Use @Autowired on the constructor when you need to disambiguate in older Spring versions
  • Use @Qualifier to pick between multiple beans of the same type
@Component
public class OrderController {
    private final OrderService orderService

    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService
    }
}

Filtering scanning and scopes

Component scanning supports include and exclude filters so you can surgically pick which classes become beans. You can also control scopes with @Scope if you want prototype instances or request scoped beans in web apps. Lifecycle annotations such as @PostConstruct and @PreDestroy let you run init and cleanup logic when the context controls the object lifecycle.

Example filter

@ComponentScan(
  basePackages = "com.example",
  includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyCustomAnnotation.class),
  excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = SomeLegacyClass.class)
)
public class FilterConfig {
}

Auto configuration and package layout

Spring Boot gives you sane defaults by scanning from the package of your main application class. Follow that layout and most of the time you can rely on auto configuration and stereotypes without extra declarations. When you stray outside that layout add explicit @ComponentScan settings or move the main class so scanning covers your packages.

Final tips that will save time and dignity

  • Favor stereotypes like @Service and @Repository to express intent and to help tooling
  • Keep related components under the same base package so auto configuration does its job
  • Use constructor injection for testability and clearer contracts
  • Limit @ComponentScan only when you need to avoid picking up test or generated classes

There you have it a compact map of how @Component and @ComponentScan make Spring find register and wire beans. Follow package layout rely on stereotypes and use constructor injection so you can sleep at night knowing your dependencies are not silently missing.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.