Saturday 16 October 2021

Spring Stereotype Annotations

    In this post, we can learn what are the Stereotype annotations in Spring and also difference between @Component and @Bean annotations.

In Previous one of the post we discussed all Spring Annotations,  so stereotype annotations also covered but we need to see which are those Spring annotations are stereotype annotations.

     The Stereotype annotations are special type of annotations and are used to create Spring beans automatically in the application context. The Stereotype annotations are @Component, @Controller, @Service and @Repository. @Component annotation is a parent or we can say generic stereotype. The other three annotations are derived from Component annotation.

  • @Component :- Used at class level that makes the class a component.
               @Component("employee")
       class Employee{
           private String name;
           // other fields 
       }

  • @Controller:-  Used to create Spring beans at the controller layer.
                @Controller
        @RequestMapping("employee")
        public class EmployeeController {

        }
  • @Service:-  Used to create Spring beans at the Service layer.
                 @Service
        public class EmployeeService{

        }
  • @Repository:-  Used to create Spring beans for the repositories at the DAO layer.
                @Repository
        public class EmployeeRepository{

        }

Stereotype Annotations


Difference between Component and Bean annotation:-