Spring `@Component`

`@Component` tells Spring that a class is a candidate for component scanning and should be registered as a Spring-managed bean. Once registered, the bean can participate in dependency injection and other container-managed behavior.

`@Component` is a stereotype annotation used on a Java class that Spring should manage. When component scanning examines a package containing the class, Spring can detect the annotation and register a bean definition for it.

A Spring-managed bean is created and managed by the application context rather than being instantiated manually with `new` throughout application code. Other beans can then receive it through dependency injection, typically using constructor injection.

Component scanning must actually cover the package containing the annotated class. In a typical Spring Boot application, scanning commonly starts from the package containing the application class and its subpackages, although scanning can be configured differently.

`@Component` is the general-purpose stereotype. More specialized stereotypes such as `@Service`, `@Repository`, and `@Controller` are intended to communicate a class's role while also serving as component-scanning candidates.

Key points

Interview questions

What happens if a `@Component` class is outside the scanned packages?

Spring will not normally discover that class through component scanning if its package is outside the configured scan scope. Consequently, no bean is registered from that annotation alone. The application can instead expand its scan configuration or register the object through another mechanism such as `@Bean`.

What does `@Component` do in Spring?

`@Component` marks a class as a candidate for Spring's component scanning. If scanning finds the class, Spring registers it as a bean in the application context. The container can then manage its lifecycle and make it available for dependency injection.

Does `@Component` itself perform dependency injection?

No. `@Component` makes the class eligible to become a Spring-managed bean, while dependency injection supplies that bean's dependencies. For example, a component can declare dependencies in its constructor, and Spring can resolve those dependencies when creating the bean.

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

Both annotations can make a class discoverable through component scanning. `@Component` is the generic stereotype, whereas `@Service` communicates that the class belongs to the service layer. The specialized annotation improves semantic clarity without changing the basic component-scanning mechanism.

How does `@Component` relate to the Spring application context?

A component discovered during scanning contributes a bean definition to the application context. The context then uses that definition to create and manage the bean according to its configuration and lifecycle rules. Other managed objects can obtain the bean through dependency injection rather than constructing it directly.