Component Scanning

Component scanning discovers classes marked as components and registers them as beans managed by a dependency-injection container. In Spring, it commonly works with annotations such as `@Component`, `@Service`, `@Repository`, and `@Controller`.

Component scanning is a mechanism used by dependency-injection frameworks to find eligible classes without requiring each bean to be registered manually. In Spring, scanning examines configured packages and identifies classes carrying component stereotypes or other recognized configuration annotations.

When Spring finds a candidate such as a class annotated with `@Service`, it creates bean metadata for that class and can later instantiate it as part of the application context. Other beans can then receive it through dependency injection, such as constructor injection.

The packages being scanned matter. A component outside the configured scan scope will not normally be discovered, even if it has `@Component` or a related stereotype. Spring Boot applications commonly scan from the package containing the main application class and its subpackages when using `@SpringBootApplication`.

Component scanning is different from dependency injection itself. Scanning answers "which classes should become beans?" while dependency injection answers "how should those beans receive their required dependencies?"

Key points

Interview questions

How does `@SpringBootApplication` relate to component scanning?

`@SpringBootApplication` is a composed annotation that includes configuration and component-scanning behavior along with auto-configuration support. Its component scan is normally rooted at the package containing the application class and extends into its subpackages. Therefore, placing the main application class in an appropriate root package is important for conventional Spring Boot discovery.

What is component scanning in Spring?

Component scanning is Spring's way of discovering classes that should be managed as beans. It searches configured packages for eligible annotations such as `@Component` and its common stereotypes. Discovered classes can then participate in the application context and dependency injection.

What is the difference between `@Component`, `@Service`, and `@Repository`?

All three can make a class eligible for component scanning. `@Service` communicates that a class represents service-layer logic, while `@Repository` identifies persistence-oriented components and has additional Spring semantics for exception translation in appropriate configurations. `@Component` is the general-purpose stereotype.

Why might a class annotated with `@Component` not be available for injection?

One common reason is that its package is outside the component-scan scope. The class might also fail to be discovered because the relevant configuration is not loaded into the application context. Another possibility is an injection problem after discovery, such as multiple matching beans or an unsuitable dependency definition.

When would you prefer `@Bean` over component scanning?

`@Bean` is useful when bean construction requires explicit configuration or when the class is not under your control and cannot be annotated. It also makes the creation logic and supplied dependencies visible in configuration. Component scanning is generally convenient for application-owned classes with straightforward construction and clear component roles.