The @Component annotation marks a Java class as a Spring-managed bean. When component scanning is active, Spring finds the class on the classpath, creates one instance of it, and registers it in the application context so it can be injected into other beans. It is the base stereotype: @Service, @Repository and @Controller are specializations of it.
Without @Component (or one of its specializations), you would have to declare every bean by hand in a @Configuration class or in XML. The annotation lets you describe "this class is a bean" right next to the class itself.
@Component is a class-level annotation from org.springframework.stereotype. It is a marker: it does not change how your class behaves, it only tells the container that the class is a candidate for auto-detection.
During startup, Spring scans the configured packages, turns each annotated class into a bean definition, and then instantiates it as a singleton by default. The bean name defaults to the uncapitalized simple class name, so MyService becomes myService. You can override it with @Component("customName").
Annotating a class is not enough on its own. Something has to scan the package. In Spring Boot, @SpringBootApplication already includes @ComponentScan, which scans the package of the annotated class and all of its sub-packages.
If your component lives outside the application class's package tree, it is silently ignored. Either move it or widen the scan with @ComponentScan(basePackages = "com.example").
Once registered, a component can be injected by type. Constructor injection is the recommended style: it makes dependencies explicit, supports final fields and works without reflection tricks in tests. With a single constructor, @Autowired is optional.
All of these are meta-annotated with @Component, so scanning treats them identically. The difference is intent and a few extra behaviours.
| Annotation | Layer | Extra behaviour |
|---|---|---|
@Component | Any generic bean | None |
@Service | Business logic | None, documents intent |
@Repository | Data access | Translates persistence exceptions into Spring's DataAccessException hierarchy |
@Controller | Web layer | Handler methods are detected by Spring MVC |
@RestController | Web layer | @Controller plus @ResponseBody |
Use @Component for classes that do not fit a specific layer, such as validators, mappers or utility beans. Prefer the more specific stereotype when one applies, because tooling and AOP pointcuts can target it.
@Component is placed on your own classes. @Bean is placed on a method inside a @Configuration class and is the right choice when you cannot edit the class, for example one from a third-party library, or when construction needs custom logic.
By default a component is a singleton. Add @Scope("prototype") for a new instance per injection, or request and session scopes in a web application. Lifecycle hooks work as for any bean: annotate methods with @PostConstruct and @PreDestroy (from jakarta.annotation in Spring 6 and Spring Boot 3 and later; the old javax.annotation package no longer applies).
No. @Service is itself annotated with @Component, so it registers the class just the same.
The most common cause is that the class sits outside the packages scanned by @ComponentScan or @SpringBootApplication. Check the package structure first, then check for a missing public constructor or a typo in the injected type.
Yes. Pass a value: @Component("pricing"). Inject it by type, or by name with @Qualifier("pricing") when there are several candidates.
Avoid it. Constructor injection keeps classes immutable and testable without a Spring context.
@Component declares a class as a Spring bean that component scanning will discover and manage. Make sure the class is inside a scanned package, inject it through the constructor, and choose @Service, @Repository or @Controller when the class clearly belongs to one of those layers. Use @Bean methods for classes you do not own.