@Component,@Service,@Repository,@Controller in spring - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn about stereotype annotations in the spring framework.
@Component, @Service, @Repository, @Controller  are considered to be the stereotype annotations in spring.
You can also find the advantages of the Spring framework along with this tutorial so that you can have a better understanding of the spring framework.
Here is the list of spring core stereotype annotations:
Stereotype annotations were introduced in the spring 2.0 with a single type called @Repository. In Spring 2.5 spring introduced a generic stereotype annotation called @Component, this is an extension of spring 2.0 stereotype annotations with different component types.
As we all know that, a component is a generic term. We can term anything as a component. So that the spring also followed the same rule, that’s why the @Component annotation is defined as a generic/super. The @Service, @Repository, @Controller are the annotation are the sub/concrete types of @Component.
@Component is a generic stereotype annotation, it can be used 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 like below.
Example:
package com.onlinetutorialspoint.beans;

import org.springframework.stereotype.Component;

@Component
public class DemoBean {

}
Spring IOC container will automatically configure the above DemoBean class with id demoBean.
In order to tell the spring container that scan for the components (beans) in a package or its sub-packages, we have 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 the Spring IOC container to process the annotations, we need to configure the information in the spring configuration file like below.
<context:annotation-config />
@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 that comes under the business layer, we can define those classes with @Service annotation.
package com.onlinetutorialspoint.beans;

import org.springframework.stereotype.Service;

@Service
public class ProductService {

}
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.
We can define the @Repository annotation on top of a class. In order to indicate a class that comes under the persistent layer, we can define those classes with @Repository annotation. @Repository is a sub-annotation of the @Component.
package com.onlinetutorialspoint.beans;

import org.springframework.stereotype.Repository;

@Repository
public class ProductDAOImpl {

}
We can define the @Controller annotation on top of a class. In order to indicate a class that comes under the presentation layer, we can define those class with @Controller annotation.
package com.onlinetutorialspoint.controllers;

import org.springframework.stereotype.Controller;

@Controller
public class ProductController {

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

AngularJs Custom Service Example
Step by Step – How to push the project into GIT Repository
Spring MVC Login Form Example Tutorials
Steps to Create AngularJs Controller
Spring boot exception handling rest service (CRUD) operations
Types of Spring Bean Scopes Example
@Autowired,@Qualifier,@Value annotations in Spring
Angularjs Services Example Tutorials
Spring Java Configuration Example
Spring Hibernate Example
How to push docker image to docker hub ?
@Qualifier annotation example in Spring
How to add OJDBC jar to Maven Repository
Spring Boot Scheduler Job @EnableScheduling example
spring expression language example

AngularJs Custom Service Example
Step by Step – How to push the project into GIT Repository
Spring MVC Login Form Example Tutorials
Steps to Create AngularJs Controller
Spring boot exception handling rest service (CRUD) operations
Types of Spring Bean Scopes Example
@Autowired,@Qualifier,@Value annotations in Spring
Angularjs Services Example Tutorials
Spring Java Configuration Example
Spring Hibernate Example
How to push docker image to docker hub ?
@Qualifier annotation example in Spring
How to add OJDBC jar to Maven Repository
Spring Boot Scheduler Job @EnableScheduling example
spring expression language example






vasanth
May 29, 2016 at 5:33 pm - Reply 

Hello Chandrasekar
It would be great helpful,if you explain the difference between @Component ,@Service ,@Repository, @Controller  or what is the purpose of using these annotation?
Thanks.









Alex
July 23, 2016 at 8:27 am - Reply 

Hello Chandrasekar. Really great article. In order to add something to the conversation and to answer the above poster’s (Vasanth) question. The difference between the annotations are to identify different layers in a tier web application. Components within the persistence, business and presentation layers are annotated respectively by @Repository, @Service and @Component as follows:
Repository -> Data access components
Service-> Service components
Controller -> Controller components
Component -> any other component
I decided to write a fuller explanation in a blog post here: https://readlearncode.com/2016/02/13/insights-from-stackoverflow-most-voted-for-spring-4-questions/#1










vasanth
May 29, 2016 at 5:33 pm - Reply 

Hello Chandrasekar
It would be great helpful,if you explain the difference between @Component ,@Service ,@Repository, @Controller  or what is the purpose of using these annotation?
Thanks.




Hello Chandrasekar
It would be great helpful,if you explain the difference between @Component ,@Service ,@Repository, @Controller  or what is the purpose of using these annotation?
Thanks.





Alex
July 23, 2016 at 8:27 am - Reply 

Hello Chandrasekar. Really great article. In order to add something to the conversation and to answer the above poster’s (Vasanth) question. The difference between the annotations are to identify different layers in a tier web application. Components within the persistence, business and presentation layers are annotated respectively by @Repository, @Service and @Component as follows:
Repository -> Data access components
Service-> Service components
Controller -> Controller components
Component -> any other component
I decided to write a fuller explanation in a blog post here: https://readlearncode.com/2016/02/13/insights-from-stackoverflow-most-voted-for-spring-4-questions/#1




Hello Chandrasekar. Really great article. In order to add something to the conversation and to answer the above poster’s (Vasanth) question. The difference between the annotations are to identify different layers in a tier web application. Components within the persistence, business and presentation layers are annotated respectively by @Repository, @Service and @Component as follows:
Repository -> Data access components
Service-> Service components
Controller -> Controller components
Component -> any other component
I decided to write a fuller explanation in a blog post here: https://readlearncode.com/2016/02/13/insights-from-stackoverflow-most-voted-for-spring-4-questions/#1