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
✗ Incorrect
@Service is used for classes that contain business logic.
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
✗ Incorrect
@Repository enables exception translation for database errors.
Which annotation is the most general stereotype for Spring-managed beans?
A@Component
B@Service
C@Controller
D@Repository
✗ Incorrect
@Component is the general-purpose stereotype annotation.
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
✗ Incorrect
@Controller is used for web controllers handling HTTP requests.
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
✗ Incorrect
Use one stereotype annotation per class to keep roles clear.
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.
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
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 B
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
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 C
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
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 A
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
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 A
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
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 D
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 unknownstrongwhile earning +1 Grip toward your certificate.