0
0
Spring Bootframework~5 mins

@Component, @Service, @Repository, @Controller in Spring Boot

Choose your learning style9 modes available
Introduction

These annotations help organize your code by marking classes for specific roles. They tell Spring Boot how to manage and use these classes automatically.

When you want to mark a general-purpose class to be managed by Spring.
When you create a service class that contains business logic.
When you write a class that handles database operations.
When you build a class that handles web requests and responses.
Syntax
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.

Examples
Marks a general class as a Spring-managed component.
Spring Boot
@Component
public class MyComponent {
    // general purpose bean
}
Marks a class as a service layer component.
Spring Boot
@Service
public class UserService {
    // business logic here
}
Marks a class as a data access object (DAO).
Spring Boot
@Repository
public class UserRepository {
    // database access code
}
Marks a class as a web controller to handle HTTP requests.
Spring Boot
@Controller
public class UserController {
    // handles web requests
}
Sample Program

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.

Spring Boot
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());
    }
}
OutputSuccess
Important Notes

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.

Summary

@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.