@Component
The foundation of Spring's component model — what @Component does, scanning, metadata reading vs reflection, bean naming, singletons, stateless beans and meta-annotations.
#Introduction
If there is one annotation you must completely understand before learning Spring Boot, it is @Component. Why? Because almost every other stereotype annotation (@Service, @Repository, @Controller, @RestController) is built on top of @Component.
#What is @Component?
@Component tells the Spring container:
import org.springframework.stereotype.Component;
@Component
public class EmailService {
public void sendEmail() {
System.out.println("Email Sent");
}
}That's all you write. You never call new EmailService() — Spring creates and manages the instance.
#Life Without Spring
public class EmailService {
public void sendEmail() {
System.out.println("Sending...");
}
}
public class UserService {
private EmailService emailService = new EmailService();
}You are responsible for creating objects, managing dependencies, managing lifecycle and reusing objects. As applications grow, this becomes difficult to maintain.
#Life With Spring
@Component
public class EmailService {
}
@Service
public class UserService {
@Autowired
private EmailService emailService;
}Now Spring handles object creation, dependency injection, lifecycle, singleton management and memory management (bean scope).
#What Happens Internally?
#Where Is the Bean Stored?
Inside the IoC container (ApplicationContext). Think of it like a warehouse:
| Bean name | Object | Scope |
|---|---|---|
emailService | EmailService@2fa89c | Singleton |
Whenever anyone needs EmailService, Spring returns this managed instance.
#How Does Spring Discover @Component?
Spring scans packages using component scanning.
- com.demo
- DemoApplication
- service
- EmailService
- controller
- UserController
- repository
- UserRepository
@SpringBootApplication
public class DemoApplication {
}#Reflection vs Metadata Reading
Conceptually:
if (class.hasAnnotation(Component.class)) {
registerBean();
}In reality, Spring reads the annotation information from bytecode before loading the class.
#Bean Naming
@Component
public class EmailService {
}Default bean name: emailService. The rule is class name with the first letter lowercased.
| Class name | Bean name |
|---|---|
UserService | userService |
EmailService | emailService |
PaymentService | paymentService |
Custom Bean Name
@Component("gmailService")
public class EmailService {
}The container now stores gmailService → EmailService object. Inject it with:
@Autowired
@Qualifier("gmailService")
private EmailService emailService;#What Is a Bean?
// Normal object
EmailService service = new EmailService();
// Spring bean
@Component
public class EmailService {
}| Object | Spring bean |
|---|---|
Created using new | Created by Spring |
| Not managed | Managed by the IoC container |
| No lifecycle callbacks | Lifecycle managed |
| No dependency injection | Supports dependency injection |
| Exists independently | Stored inside the ApplicationContext |
#Singleton Behaviour
By default, only one instance is created:
@Service
public class UserService {
@Autowired
EmailService emailService;
}
@RestController
public class UserController {
@Autowired
EmailService emailService;
}What If There Are 100 Requests?
Even with 100 concurrent HTTP requests, all requests share the same singleton bean. Because singleton beans are shared, they should generally be stateless.
#Stateless vs Stateful Beans
✅ Good (stateless) — no instance variables hold request-specific data:
@Component
public class EmailService {
public void sendEmail(String email) {
System.out.println(email);
}
}❌ Bad (stateful):
@Component
public class EmailService {
private String currentEmail;
public void send(String email) {
currentEmail = email;
}
}If multiple threads call send() simultaneously, currentEmail may be overwritten, leading to race conditions.
#Can @Component Be Used Everywhere?
Technically, yes:
@Component
class PaymentService {}
@Component
class UserRepository {}But Spring provides specialized stereotypes for better readability and additional behaviour:
| Annotation | Purpose |
|---|---|
@Component | Generic component |
@Service | Business logic |
@Repository | Persistence layer (adds exception translation) |
@Controller | MVC controller |
@RestController | REST controller |
#Meta-Annotation
Internally, @Service is defined (conceptually) as:
@Component
public @interface Service {
}Likewise @Repository → @Component and @Controller → @Component. This is why component scanning detects all of them.
- @Component
- @Service
- @Repository
- @Controller
- @RestController
#Common Mistakes
1. Package Not Scanned
@SpringBootApplication is in com.demo.app, but the component is in org.example.service. Result: no qualifying bean.
Fix: @ComponentScan("org.example"), or move the class under the application's base package.
2. Missing Annotation
public class EmailService {
}
@Autowired
EmailService emailService;NoSuchBeanDefinitionException: No qualifying bean of type 'EmailService' available…because Spring never registered the class as a bean.
3. Multiple Beans of the Same Type
@Component
class GmailService implements EmailService {
}
@Component
class OutlookService implements EmailService {
}
@Autowired
EmailService service;Spring doesn't know which bean to inject and throws NoUniqueBeanDefinitionException. Solutions include @Qualifier and @Primary (covered in the Dependency Injection chapter).
#Real-World Example
@RestController
public class UserController {
@Autowired
UserService service;
}
@Service
public class UserService {
@Autowired
UserRepository repository;
}
@Repository
public class UserRepository {
}All three classes are discovered through component scanning because they are stereotype annotations built on @Component.
#Interview Questions
Q1What is @Component?
A stereotype annotation that marks a class as a Spring-managed bean eligible for component scanning and dependency injection.
Q2Who creates a @Component bean?
The Spring IoC container (ApplicationContext) creates and manages it.
Q3Is every @Component a singleton?
By default, yes — unless another scope (such as prototype, request or session) is specified.
Q4Where are Spring beans stored?
Inside the ApplicationContext (IoC container).
Q5Does @Component automatically inject dependencies?
No. It only registers the class as a bean. Dependency injection is performed using constructor injection or @Autowired.
Q6What is the difference between an object and a bean?
An object is any Java instance. A Spring bean is an object created, configured and managed by the Spring container.
Q7Why do @Service, @Repository and @Controller work with component scanning?
Because they are stereotype annotations that are meta-annotated with @Component.
#Summary
@Componentmarks a class as a Spring-managed bean.- Component scanning discovers candidate classes.
- Spring registers a
BeanDefinition, then creates and manages the bean. - By default,
@Componentbeans are singleton-scoped. @Service,@Repositoryand@Controllerare built on top of@Component.- Stateless singleton beans are the recommended design for most services.