Stereotype Annotations in Spring Framework

Posted By : Dilshad Ahmad | 30-Apr-2018

In spring framework there are four types of stereotype annotations spring has provided and these are very useful for the web application development. If any class or bean is annotated with one of these annotation spring will automatically create the object of that bean and register that bean in spring container. Now that bean will be available for entire application to use. We can inject those beans in any layer of the application. These annotations are found in the org.springframework.stereotype package.

 

These annotations are :

 

1. @Controller: This annotation is basically used for the controller classes of the application. This annotation is available for presentation layer component. 

 

2. @Service: This annotation is used for the service classes of the application where we write the business logic.

 

3. @Component: This is a generic annotation and can be used anywhere in the application. We can annotate REST resource classes with this annotation. 

 

4. @Repository: This annotation is used for the repository layer components from where we can contact to the database directly.

                                                                                    

Spring 2.0 introduced the first stereotype annotation named as @Repository. The @Component annotation introduced in Spring 2.5 version. Spring stereotype annotations are the markers for any class that fulfills a role within the application. These annotations have greatly reduced the burden of developers to write the code for configuring the beans in the spring configuration document.

 

Where should we use these stereotype annotations?

 

These annotations are used for concrete classes but not for interfaces.

 

@Controller annotation is for the class as a Spring MVC controller. It is a meta-annotation of @Component, so beans annotated with @Controller that means Spring container will detect these annotations and loads those classes marked with this annotation and container will create those objects and fully initialized bean will be ready into the container for use. If we are using this annotation then we can use handler mapping annotation @RequestMapping to mark our methods inside the controller classes at which request what method should invoke.

 

@Controller
class AccountController{
       @RequestMapping(value = "/deposit", method = RequestMethod.POST)
       public String deposit(@Model User user){
             return "success";
       }
}

@Service annotation is basically used for service classes in our application.

@Service
class AccountService{
}

@Component annotation is basically used for when our classes do not fall into either of three categories i.e. @Controller, @Service, and DAOs.

 

@Repository annotation is basically used for persistence layer components. The repository is a very suitable annotation for DAOs. Thus, providing extra benefits to the DAOs.

 

The @Repository annotation is a meta-annotation of the @Component annotation with similar use and functionality. In repository annotated class we can write the code to contact the persistence layer which is nothing but a database.

 

Enabling Component Scanning: Spring Container does not create instances for those classes whose annotated with these stereotype annotations. So we have to enable component scanning explicitly in the Spring configuration document by using < context:component-scan> tag. So stereotype annotations will be scanned and configured only when they are scanned by Spring Container of Spring framework.

 

<context:component-scan base-package="com.java.trading.service" />
<context:component-scan base-package="com.java.trading.repository" />
<context:component-scan base-package="com.java.trading.controller" />
Spring recommends do not use your top package for scanning, so we  should declare specific component-scan elements.

 

Where should we use these annotations?


We should always use these annotations over the concrete classes of the application; not over the interfaces.

 

Thanks!

About Author

Author Image
Dilshad Ahmad

Dilshad Ahmad working as a Developer is always ready to face challenges and likes to work with full dedication and coordination.

Request for Proposal

Name is required

Comment is required

Sending message..