What are Stereotype annotations in Spring?



The @Component annotation which is a parent stereotype annotation can be used to define all beans. However, the Spring Framework supports different stereotype annotations to divide components by layer as listed here:

@Component

It is a generic stereotype annotation, which defines a class as a bean. It is required to import org.springframework.stereotype, in order to use this annotation.
Example:
@Component
class ComponentDemo{
    public String getValue() {
        return "Hello World";
    }
}

Check out a complete example at https://www.javaguides.net/2018/11/spring-component-annotation-example.html.

@Repository

Annotate all your repository classes with @Repository annotation, which is a marker for a class. A repository class serves in the persistence layer of the application as Data Access Objects (DAO) that contains all your database access logic. It is required to import, org.springframework.stereotype.Repository to use @Repository annotation.
Example:
@Repository
interface UserRepository extends JpaRepository < User, Integer > {

}

Check out a complete example at https://www.javaguides.net/2018/11/spring-repository-annotation.html.

@Service

Annotate all your service classes with @Service annotation, which contains all your business logic. It is required to import org.springframework.stereotype.Service, in order to use @Service.

Example:
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import net.guides.springboot2.springaop.model.Employee;

/**
 * Employee Service
 * 
 * @author Ramesh
 *
 */
@Service
public class EmployeeService {

    private List < Employee > employees = new ArrayList < > ();

    public List < Employee > getAllEmployees() {
        System.out.println("Method getAllEmployees() called");
        return employees;
    }

    public Employee getEmployeeById(Long employeeId) {
        System.out.println("Method getEmployeeById() called");
        for (Employee employee: employees) {
            if (employee.getId() == Long.valueOf(employeeId)) {
                return employee;
            }
        }
        return null;
    }

    public void addEmployee(Employee employee) {
        System.out.println("Method addEmployee() called");
        employees.add(employee);
    }

    public void updateEmployee(Employee employeeDetails) {
        System.out.println("Method updateEmployee() called");
        for (Employee employee: employees) {
            if (employee.getId() == Long.valueOf(employeeDetails.getId())) {
                employees.remove(employee);
                employees.add(employeeDetails);
            }
        }
    }

    public void deleteEmployee(Long employeeId) {
        System.out.println("Method deleteEmployee() called");
        for (Employee employee: employees) {
            if (employee.getId() == Long.valueOf(employeeId)) {
                employees.remove(employee);
            }
        }
    }
}

@Controller

The @Controller indicates that the annotated class is a Spring component of type "controller". It is a class-level annotation that indicates that an annotated class serves the role of a controller in Spring MVC. It is required to import org.springframework.stereotype.Controller, in order to use @Controller.
Example:
@Controller
@RequestMapping("/api/v1")
public class EmployeeController {
    @Autowired
    private EmployeeRepository employeeRepository;

    @GetMapping("/employees")
    public List<Employee> getAllEmployees() {
        return employeeRepository.findAll();
    }}
Read the complete example at https://www.javaguides.net/2018/11/the-spring-controller-and-restcontroller-annotations-with-examples.html.

Comments