DevNotes
Support
Spring Boot · Chapter 02

@Component

The foundation of Spring's component model — what @Component does, scanning, metadata reading vs reflection, bean naming, singletons, stateless beans and meta-annotations.

◷ 6 min read§ 18 sections? 9 interview questions{ } 17 code examples

#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:

Java
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

Java
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

Java
@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?

SpringApplication.run()
ApplicationContext created
Component scan
Found EmailService.class
Has @Component?
Create BeanDefinition
Ignored
Instantiate object
Store in IoC container
Bean ready

#Where Is the Bean Stored?

Inside the IoC container (ApplicationContext). Think of it like a warehouse:

Bean nameObjectScope
emailServiceEmailService@2fa89cSingleton

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
Java
@SpringBootApplication
public class DemoApplication {
}
@ComponentScan
Scan com.demo.*
Read every class
Has @Component?
Register bean

#Reflection vs Metadata Reading

Conceptually:

Java
if (class.hasAnnotation(Component.class)) {
    registerBean();
}

In reality, Spring reads the annotation information from bytecode before loading the class.

#Bean Naming

Java
@Component
public class EmailService {
}

Default bean name: emailService. The rule is class name with the first letter lowercased.

Class nameBean name
UserServiceuserService
EmailServiceemailService
PaymentServicepaymentService

Custom Bean Name

Java
@Component("gmailService")
public class EmailService {
}

The container now stores gmailService → EmailService object. Inject it with:

Java
@Autowired
@Qualifier("gmailService")
private EmailService emailService;

#What Is a Bean?

Java
// Normal object
EmailService service = new EmailService();

// Spring bean
@Component
public class EmailService {
}
ObjectSpring bean
Created using newCreated by Spring
Not managedManaged by the IoC container
No lifecycle callbacksLifecycle managed
No dependency injectionSupports dependency injection
Exists independentlyStored inside the ApplicationContext

#Singleton Behaviour

By default, only one instance is created:

Java
@Service
public class UserService {

    @Autowired
    EmailService emailService;
}

@RestController
public class UserController {

    @Autowired
    EmailService emailService;
}
UserService.emailServiceUserController.emailService
EmailServiceone shared instance
Spring injects the same singleton instance into both classes

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:

Java
@Component
public class EmailService {

    public void sendEmail(String email) {
        System.out.println(email);
    }
}

❌ Bad (stateful):

Java
@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:

Java
@Component
class PaymentService {}

@Component
class UserRepository {}

But Spring provides specialized stereotypes for better readability and additional behaviour:

AnnotationPurpose
@ComponentGeneric component
@ServiceBusiness logic
@RepositoryPersistence layer (adds exception translation)
@ControllerMVC controller
@RestControllerREST controller

#Meta-Annotation

Internally, @Service is defined (conceptually) as:

Java
@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

Java
public class EmailService {
}

@Autowired
EmailService emailService;
Output
NoSuchBeanDefinitionException: No qualifying bean of type 'EmailService' available

…because Spring never registered the class as a bean.

3. Multiple Beans of the Same Type

Java
@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

Controller
Service
Repository
Database
Java
@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

  • @Component marks a class as a Spring-managed bean.
  • Component scanning discovers candidate classes.
  • Spring registers a BeanDefinition, then creates and manages the bean.
  • By default, @Component beans are singleton-scoped.
  • @Service, @Repository and @Controller are built on top of @Component.
  • Stateless singleton beans are the recommended design for most services.