Bird
Raised Fist0
Spring Bootframework~10 mins

@Component, @Service, @Repository, @Controller in Spring Boot - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - @Component, @Service, @Repository, @Controller
Start Spring Boot App
↓
Scan for Annotations
↓
Find @Component/@Service/@Repository/@Controller
↓
Create Bean Instances
↓
Inject Beans where needed
↓
App runs with managed components
Spring Boot scans for these annotations, creates and manages objects (beans), and injects them where needed to run the app.
Execution Sample
Spring Boot
@Component
public class MyComponent {}

@Service
public class MyService {}

@Repository
public class MyRepository {}

@Controller
public class MyController {}
Defines four classes each marked with a Spring annotation to be managed as beans.
Execution Table
StepActionAnnotation FoundBean CreatedInjection PointResult
1Start app and scan classesNone yetNoNoScanning begins
2Find MyComponent class@ComponentYes: MyComponent beanNoBean ready for injection
3Find MyService class@ServiceYes: MyService beanNoBean ready for injection
4Find MyRepository class@RepositoryYes: MyRepository beanNoBean ready for injection
5Find MyController class@ControllerYes: MyController beanNoBean ready for injection
6Inject beans into dependent classesN/AN/AYesBeans wired where needed
7App runs with all beans managedN/AN/AN/AApp fully functional
💡 All annotated classes are detected, beans created, and injected; app starts successfully.
Variable Tracker
Bean NameStartAfter ScanAfter CreationAfter InjectionFinal
MyComponentNot foundFoundCreatedInjected if neededReady
MyServiceNot foundFoundCreatedInjected if neededReady
MyRepositoryNot foundFoundCreatedInjected if neededReady
MyControllerNot foundFoundCreatedInjected if neededReady
Key Moments - 3 Insights
Why does Spring create objects for these classes automatically?
Because the classes have special annotations like @Component, Spring scans and creates beans for them as shown in execution_table steps 2-5.
What is the difference between @Component and @Service?
@Service is a specialized @Component for service layer logic, but both cause Spring to create beans. See execution_table steps 2 and 3 for both bean creations.
How does Spring know where to put these beans?
Spring injects beans into other classes marked for injection, as shown in execution_table step 6 where injection happens.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the MyRepository bean created?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Check the 'Bean Created' column for MyRepository in the execution_table rows.
According to variable_tracker, what is the state of MyService after injection?
AInjected if needed
BCreated but not injected
CNot found
DDestroyed
💡 Hint
Look at the 'After Injection' column for MyService in variable_tracker.
If a class is missing @Component or similar annotation, what happens during scanning?
ASpring creates a bean anyway
BSpring ignores the class
CSpring throws an error
DSpring injects it automatically
💡 Hint
Refer to execution_table step 1 where no annotation means no bean creation.
Concept Snapshot
@Component, @Service, @Repository, @Controller are Spring annotations.
They mark classes for Spring to create and manage as beans.
Spring scans these annotations at startup, creates beans, and injects them.
@Service is for service logic, @Repository for data access, @Controller for web controllers.
All are specializations of @Component.
Beans are injected where needed to run the app smoothly.
Full Transcript
In Spring Boot, classes marked with @Component, @Service, @Repository, or @Controller are automatically detected during app startup. Spring scans the code, finds these annotations, and creates bean instances for each. These beans are then injected into other classes that need them, enabling easy reuse and management. @Service is a specialized @Component for business logic, @Repository for data access, and @Controller for handling web requests. This process allows the app to run with all components managed by Spring, simplifying development and wiring.

Practice

(1/5)
1. Which annotation is the most general one used to mark a Spring bean in a Spring Boot application?
easy
A. @Service
B. @Component
C. @Repository
D. @Controller

Solution

  1. Step 1: Understand the purpose of each annotation

    @Component is the base annotation for any Spring-managed bean. @Service, @Repository, and @Controller are specialized stereotypes built on top of @Component.
  2. Step 2: Identify the most general annotation

    Since @Component is the general-purpose annotation for any Spring bean, it is the correct answer.
  3. Final Answer:

    @Component -> Option B
  4. Quick Check:

    General Spring bean marker = @Component [OK]
Hint: General bean annotation is @Component, others are special cases [OK]
Common Mistakes:
  • Confusing @Service as the general bean annotation
  • Thinking @Repository is for all beans
  • Assuming @Controller is the general annotation
2. Which annotation should you use to mark a class that handles HTTP requests in a Spring Boot application?
easy
A. @Repository
B. @Service
C. @Controller
D. @Component

Solution

  1. Step 1: Recall the role of each annotation

    @Controller is specifically designed for classes that handle web requests and return views or data.
  2. Step 2: Match the annotation to the web controller role

    Since the question asks about handling HTTP requests, @Controller is the correct annotation.
  3. Final Answer:

    @Controller -> Option C
  4. Quick Check:

    Web request handler = @Controller [OK]
Hint: Web controllers use @Controller annotation [OK]
Common Mistakes:
  • Using @Service for web controllers
  • Confusing @Repository with @Controller
  • Using @Component instead of @Controller for HTTP handling
3. Consider the following Spring Boot class:
@Service
public class UserService {
    public String getUser() {
        return "Alice";
    }
}
What is the role of this class in the application?
medium
A. Contains business logic related to users
B. Accesses the database to retrieve user data
C. General Spring bean without specific role
D. Handles HTTP requests and responses

Solution

  1. Step 1: Identify the annotation and its meaning

    The class is annotated with @Service, which is used for business logic components in Spring.
  2. Step 2: Understand the method and class purpose

    The method getUser() returns a user name, indicating business logic rather than database access or HTTP handling.
  3. Final Answer:

    Contains business logic related to users -> Option A
  4. Quick Check:

    @Service = business logic class [OK]
Hint: @Service means business logic, not web or data access [OK]
Common Mistakes:
  • Thinking @Service accesses the database directly
  • Confusing @Service with @Controller
  • Assuming @Service is a generic bean without role
4. What is wrong with this Spring Boot repository class?
@Repository
public class UserRepository {
    public void saveUser(String user) {
        // save logic
    }
}
Options: A) Class should be an interface extending JpaRepository B) Missing @Service annotation C) @Repository cannot be used on classes D) Method saveUser must return a value
medium
A. Class should be an interface extending JpaRepository
B. Missing @Service annotation
C. @Repository cannot be used on classes
D. Method saveUser must return a value

Solution

  1. Step 1: Understand typical repository pattern in Spring Boot

    Spring Data JPA repositories are usually interfaces extending JpaRepository or CrudRepository to get built-in methods.
  2. Step 2: Identify the issue with the class declaration

    The class is a concrete class, not an interface extending JpaRepository, so it misses Spring Data JPA features.
  3. Final Answer:

    Class should be an interface extending JpaRepository -> Option A
  4. Quick Check:

    Repositories usually extend JpaRepository interface [OK]
Hint: Repositories are interfaces extending JpaRepository, not plain classes [OK]
Common Mistakes:
  • Thinking @Repository cannot be on classes
  • Assuming @Service is needed for repositories
  • Believing saveUser must return a value
5. You have a Spring Boot application with these classes:
@Component
public class UtilityHelper { }
@Service
public class OrderService { }
@Repository
public interface OrderRepository extends JpaRepository { }
@Controller
public class OrderController { }
Which statement correctly describes the roles and how Spring manages these beans?
hard
A. @Component manages data access; @Service handles web requests; @Repository is for business logic; @Controller is a utility bean
B. @Component handles web requests; @Service manages data access; @Repository is for utility beans; @Controller handles business logic
C. All annotations are interchangeable and have the same role in Spring
D. @Component is for utility beans; @Service handles business logic; @Repository manages data access; @Controller handles web requests

Solution

  1. Step 1: Recall the purpose of each annotation

    @Component is a general-purpose bean, often used for utilities. @Service is for business logic. @Repository is for data access layers. @Controller is for handling web requests.
  2. Step 2: Match each class role to its annotation

    UtilityHelper is a utility bean, so @Component fits. OrderService contains business logic, so @Service fits. OrderRepository accesses data, so @Repository fits. OrderController handles HTTP requests, so @Controller fits.
  3. Final Answer:

    @Component is for utility beans; @Service handles business logic; @Repository manages data access; @Controller handles web requests -> Option D
  4. Quick Check:

    Annotations match roles: Component-utility, Service-logic, Repository-data, Controller-web [OK]
Hint: Match annotation to role: Component-utility, Service-logic, Repository-data, Controller-web [OK]
Common Mistakes:
  • Mixing up @Service and @Controller roles
  • Thinking all annotations are interchangeable
  • Confusing @Component with @Repository
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard