In this tutorial, we are going to learn about stereotype annotations in spring framework.

@Component, @Service, @Repository, @Controller  are considered to be the stereotype annotations in spring.

You can also find the advantages of Spring framework along with this tutorial so that you can have a better understanding of spring framework.

Here is the list of spring core stereotype annotations:

Stereotype annotations in Spring Core :

Stereotype annotations were introduced in spring 2.0 version, with the single type @Repository. In Spring 2.5 the spring comes up with the generic stereotype annotation called @Component, this is a continuation for spring 2.0 stereotype annotations with different component types.

As we all know, a component is a generic term. We can term, everything is a component. So that in spring, @Component comes in different flavors like @Service, @Repository, @Controller. Let’s examine the each and every @Component type annotation.

In order to reduce the spring configuration file and to reduce the burden on developers, spring stereotype annotations are introduced.

Spring @Component @Service @Repository

Spring core annotation @Component

@Component is a generic stereotype annotation. We can define @Component annotation on top of a class. If we declare a @Component on top of a class, then that class will be automatically configured by the Spring IOC container, with the unqualified class name as id.

Example:

[sourcecode language=”java”]
package com.onlinetutorialspoint.beans;

import org.springframework.stereotype.Component;

@Component
public class DemoBean {

}

[/sourcecode]

Spring IOC container will automatically configure the above DemoBean class with id “demoBean”.

In order to tell the spring container about scanning for the components (beans) in a package or its sub packages, we need to configure the component-scan in spring configuration XML like below.

 

<context:component-scan base-package="com.onlinetutorialspoint.beans" />

We can also define the multiple base-packages in component-scan like below:

 

<context:component-scan base-package="com.onlinetutorialspoint.beans, com.onlinetutorialspoint.controllers" />

In order to tell Spring IOC container to process the annotations, we need to configure the information in spring configuration file like below.

<context:annotation-config />

 

Spring Core annotation @Service :

@Service is a sub-annotation of the @Component annotation. We can define the @Service annotation on top of a class. In order to indicate a  class comes under the business layer, we can define that class with @Service annotation.

[sourcecode language=”java”]
package com.onlinetutorialspoint.beans;

import org.springframework.stereotype.Service;

@Service
public class ProductService {

}

[/sourcecode]

If we declare @Service annotation on top of a class, then it will automatically be configured by IOC container, because @Service annotation is a sub-annotation of @Component annotation.

Spring Core annotation @Repository :

We can define the @Repository annotation on top of a class. In order to indicate a  class comes under the persistent layer, we can define that class with @Repository annotation. @Repository is a sub-annotation of the @Component.

[sourcecode language=”java”]

package com.onlinetutorialspoint.beans;

import org.springframework.stereotype.Repository;

@Repository
public class ProductDAOImpl {

}

[/sourcecode]

Spring Core annotation @Controller :

We can define the @Controller annotation on top of a class. In order to indicate a  class comes under the presentation layer, we can define that class with @Controller annotation.

[sourcecode language=”java”]

package com.onlinetutorialspoint.controllers;

import org.springframework.stereotype.Controller;

@Controller
public class ProductController {

}

[/sourcecode]

@Controller is a sub-annotation of the @Component annotation, hence it will automatically configure by the Spring IOC container.

Happy Learning 🙂