These annotations help organize your code by marking classes for specific roles. They tell Spring Boot how to manage and use these classes automatically.
@Component, @Service, @Repository, @Controller in Spring Boot
@Component @Service @Repository @Controller
@Component is a general annotation for any Spring-managed class.
@Service, @Repository, and @Controller are specializations of @Component with specific roles.
@Component
public class MyComponent {
// general purpose bean
}@Service
public class UserService {
// business logic here
}@Repository
public class UserRepository {
// database access code
}@Controller
public class UserController {
// handles web requests
}This example shows four classes each marked with one of the annotations. Each class has a simple method returning a message. The main method creates instances and prints their messages.
import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Controller; @Component public class MyComponent { public String greet() { return "Hello from Component!"; } } @Service public class MyService { public String serve() { return "Service is running."; } } @Repository public class MyRepository { public String fetch() { return "Data fetched from repository."; } } @Controller public class MyController { public String handle() { return "Controller handling request."; } } public class Application { public static void main(String[] args) { MyComponent component = new MyComponent(); MyService service = new MyService(); MyRepository repository = new MyRepository(); MyController controller = new MyController(); System.out.println(component.greet()); System.out.println(service.serve()); System.out.println(repository.fetch()); System.out.println(controller.handle()); } }
All these annotations make Spring manage the classes automatically as beans.
@Repository also helps with database exception translation.
@Controller is used in web apps to map HTTP requests to methods.
@Component is the general marker for Spring beans.
@Service is for business logic classes.
@Repository is for data access classes.
@Controller is for web request handling classes.
Practice
Solution
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.Step 2: Identify the most general annotation
Since @Component is the general-purpose annotation for any Spring bean, it is the correct answer.Final Answer:
@Component -> Option BQuick Check:
General Spring bean marker = @Component [OK]
- Confusing @Service as the general bean annotation
- Thinking @Repository is for all beans
- Assuming @Controller is the general annotation
Solution
Step 1: Recall the role of each annotation
@Controller is specifically designed for classes that handle web requests and return views or data.Step 2: Match the annotation to the web controller role
Since the question asks about handling HTTP requests, @Controller is the correct annotation.Final Answer:
@Controller -> Option CQuick Check:
Web request handler = @Controller [OK]
- Using @Service for web controllers
- Confusing @Repository with @Controller
- Using @Component instead of @Controller for HTTP handling
@Service
public class UserService {
public String getUser() {
return "Alice";
}
}
What is the role of this class in the application?Solution
Step 1: Identify the annotation and its meaning
The class is annotated with @Service, which is used for business logic components in Spring.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.Final Answer:
Contains business logic related to users -> Option AQuick Check:
@Service = business logic class [OK]
- Thinking @Service accesses the database directly
- Confusing @Service with @Controller
- Assuming @Service is a generic bean without role
@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 valueSolution
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.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.Final Answer:
Class should be an interface extending JpaRepository -> Option AQuick Check:
Repositories usually extend JpaRepository interface [OK]
- Thinking @Repository cannot be on classes
- Assuming @Service is needed for repositories
- Believing saveUser must return a value
@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?Solution
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.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.Final Answer:
@Component is for utility beans; @Service handles business logic; @Repository manages data access; @Controller handles web requests -> Option DQuick Check:
Annotations match roles: Component-utility, Service-logic, Repository-data, Controller-web [OK]
- Mixing up @Service and @Controller roles
- Thinking all annotations are interchangeable
- Confusing @Component with @Repository
Ready to prove it?
Pass the test and this pattern moves unknown
