0
0
Spring Bootframework~5 mins

@Component, @Service, @Repository, @Controller in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Component annotation in Spring Boot?
<p><code>@Component</code> marks a class as a Spring-managed bean. It tells Spring to create an instance of this class and manage its lifecycle automatically.</p>
Click to reveal answer
beginner
How does @Service differ from @Component?
<p><code>@Service</code> is a specialized form of <code>@Component</code> used to mark service layer classes. It helps clarify the role of the class as holding business logic.</p>
Click to reveal answer
intermediate
What role does @Repository play in a Spring Boot application?
<p><code>@Repository</code> marks a class as a data access object (DAO). It also enables Spring to translate database exceptions into Spring’s data access exceptions.</p>
Click to reveal answer
beginner
What is the main use of @Controller in Spring Boot?
<p><code>@Controller</code> marks a class as a web controller that handles HTTP requests and returns views or data responses.</p>
Click to reveal answer
intermediate
Can a class be annotated with more than one of these annotations: <code>@Component</code>, <code>@Service</code>, <code>@Repository</code>, <code>@Controller</code>?
<p>No, these annotations are specialized stereotypes of <code>@Component</code>. You use one that best fits the class role to keep your code clear and organized.</p>
Click to reveal answer
Which annotation would you use to mark a class that handles business logic in Spring Boot?
A@Component
B@Controller
C@Repository
D@Service
What does the @Repository annotation help with besides marking a class as a data access object?
AManaging service layer logic
BHandling HTTP requests
CTranslating database exceptions into Spring exceptions
DCreating REST endpoints
Which annotation is the most general stereotype for Spring-managed beans?
A@Component
B@Service
C@Controller
D@Repository
If you want to create a class that handles web requests and returns views, which annotation should you use?
A@Repository
B@Controller
C@Service
D@Component
Is it good practice to annotate a class with both @Service and @Repository?
ANo, use one annotation that fits the class role
BYes, to combine business and data logic
CYes, to improve performance
DNo, because Spring does not allow multiple annotations
Explain the difference between @Component, @Service, @Repository, and @Controller in Spring Boot.
Think about the role each annotation plays in the app layers.
You got /4 concepts.
    Why is it important to use the correct stereotype annotation for a class in Spring Boot?
    Consider how Spring and other developers understand your code.
    You got /4 concepts.