In Spring, @Component marks a class as a candidate for auto-detection. During classpath scanning, Spring creates one instance of every annotated class, registers it as a bean in the application context, and makes it available for dependency injection. It is the base stereotype annotation: @Service, @Repository and @Controller are specialisations of it.
You use @Component for classes that do not fit a more specific role, and you rely on component scanning, which Spring Boot enables by default through @SpringBootApplication.
@Component (in org.springframework.stereotype) tells Spring that the class is a bean definition without you writing any explicit registration. The container then manages its lifecycle: instantiation, dependency injection, initialisation callbacks such as @PostConstruct, and destruction on shutdown.
By default the bean name is the decapitalised class name (priceCalculator), and the scope is singleton: one shared instance per application context. You can set a name with @Component("calculator") and change the scope with @Scope("prototype").
Scanning starts from a base package. In Spring Boot, @SpringBootApplication includes @ComponentScan for the package of the main class and all its subpackages.
A class in com.example.shop.pricing is found if the main class lives in com.example.shop. A class in com.other.lib is not. To scan another package, add @ComponentScan to a configuration class:
Outside Spring Boot, you must declare @ComponentScan yourself, because plain Spring does not scan anything unless told to.
Once PriceCalculator is a bean, other beans receive it by declaring it as a dependency. Constructor injection is the recommended style: the field can be final, the class is easy to test, and since Spring 4.3 a single constructor needs no @Autowired.
If two beans of the same type exist, Spring fails with NoUniqueBeanDefinitionException. Resolve it with @Primary on one of them or @Qualifier("name") at the injection point.
| Annotation | Typical layer | Extra behaviour |
|---|---|---|
@Component | Generic bean | None |
@Service | Business logic | None, documents intent |
@Repository | Data access | Translates persistence exceptions to DataAccessException |
@Controller / @RestController | Web layer | Request mapping support |
@Configuration | Bean factory class | CGLIB proxying so @Bean methods return singletons |
Choosing the most specific stereotype makes the code easier to read and lets tools and aspects target a layer, for example pointcuts on @Repository.
@Component is class-level and works with scanning. @Bean is method-level and lives in a @Configuration class. Use @Bean when you need to construct an object yourself, or when the class belongs to a library you cannot annotate.
Spring supports both in the same application, and many projects use @Component for their own classes and @Bean for third-party ones.
NoSuchBeanDefinitionException.new PriceCalculator() yourself. The new object is not managed by Spring, so injection and proxies do not apply.@Autowired, which hides dependencies and makes unit tests harder.Functionally yes. @Service is meta-annotated with @Component; it exists to communicate the role of the class.
Not on a constructor when the class has exactly one. You still need it on fields or setters if you use those styles, and a bean is injected only into other Spring-managed beans.
No. Annotate the implementing class. Spring injects it by the interface type.
Use @Profile, @ConditionalOnProperty, or replace it with @MockitoBean in a Spring Boot test.
@Component registers a class as a Spring bean through classpath scanning, so the container manages its lifecycle and injects its dependencies. Spring Boot scans from the main class package downward, constructor injection is the preferred wiring style, and @Service, @Repository and @Controller refine the same idea. Use @Bean methods when you must build the object yourself.