Sitemap
Javarevisited

A humble place to learn Java and Programming better.

Member-only story

When to Use @Service, @Repository, @Controller, and @Component Annotations in Spring Boot

--

This is a member-only article. For non-members, read this article for free on my blog: @Service, @Repository, @Controller, and @Component Annotations in Spring Boot.

🚀 Introduction

Spring Boot provides multiple stereotype annotations (@Service, @Repository, @Controller, and @Component) to mark beans for dependency injection. But when should you use each of them?

This guide explains:
Differences between @Service, @Repository, @Controller, and @Component
Their specific use cases
How Spring Boot manages them in the application context

1️⃣ @Component — The Generic Stereotype

The @Component annotation is the most generic annotation used for marking a class as a Spring-managed bean.

📌 When to Use @Component?

  • When a class does not fit into @Service, @Repository, or @Controller categories.
  • For custom utility classes, helper services, or third-party integrations.

✅ Example: Defining a General Component

@Component
public class EmailUtility {
public void sendEmail(String recipient, String message) {
System.out.println("Sending email to: " + recipient);
}
}

--

--

No responses yet