Learn details about various Stereotype Annotations provided by Spring Framework. Understand the differences between @Component, @Service, and @Repisitory.
Tutorial Contents
Overview
The Spring Framework provides various stereotype annotations. These annotations are used on various classes in a Spring Application. The Dependency Injection (DI) and Inversion of Control (IoC) in Spring Framework is capable of instantiating classes and managing dependencies of those classes. However, spring needs to know which classes it needs to manage. To do that, we use one of the stereotype annotation on those classes.
In this tutorial we will begin by a short introduction to Spring Stereotype Annotations, and then understand what is the difference between @Component, @Service, and @Repository. Lastly, we will have visit these three annotations individually.
Spring Managed Beans and Stereotype Annotations
In the very old versions of Spring Framework, when there were no annotations, we used to define the Spring Managed beans in XML files. Now, with newer versions we can use annotation to do that
@Configuration
@ComponentScan("com.ammitph.spring)
public class ApplicationConfig {
...
}
Code language: Java (java)
Spring provides a @ComponentScan annotation, where we can provide base package of the Spring managed project. Spring then scans through the specified package and its sub packages to find classes having marked with one of the stereotype annotations. Then, it registers all such classes as Spring managed beans in Application Context.
There are total four such annotations: @Component, @Service, @Repsitory, and @Controller. Out of which, we will be discussing the first three and their differences.
On a high level, all three annotations are technically same, but they differ in the usage. We will talk more about it in the next section. For now, let’s have a brief overview of the three annotations.
- @Service is used on the service layer classes of application
- @Component can be used generically on any class
- @Repository is used to annotation repository or persistent layer classes.
@Service vs @Component vs @Repository
As stated above, these annotations does not differ in terms of behaviour and can be used alternatively. However the main difference between the stereotype annotation is they helps in separating layers and classify components in different groups. Let’s understand this in detail.
Typical applications contain different layers like a Web Layer, Service Layer, and Data Access Layer. Each layer can have multiple classes. Marking these classes with dedicated stereotype annotations helps in classifying them.
The @Component is the parent annotation of rest of the stereotype annotations. Hence we can generically use it on any class. However marking a service layer class with @Service and a repository layer class with @Repository helps differentiating between them.
Also, when doing AOP, we can use these annotations and apply advices on the classes of a particular layer.
@Component Annotation
As stated, @Component is the parent of all stereotype annotations. When Spring performs a component scan, it only looks for classes marked with @Component annotations.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {
String value() default "";
}
Code language: Java (java)
We can use this annotation on all the classes and it won’t cause any difference.
@Service Annotation
The @Service is child of component and used to denote classes from the service layer of the application.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
Code language: Java (java)
As of now, Spring doesn’t do anything different for the classes that are marked with @Service. But, it is recommended to use it on service layer classes.
@Repository Annotation
Similarly, @Repository is also a child of component annotation and used on the classes that belong to persistence data access layer and serves as a data repository.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
Code language: Java (java)
However, here is an implementation specific difference. The @Repository annotation can catch the persistence related exceptions and wrap them into Runtime Exceptions.
Summary
In this quick tutorial we learned about Spring Stereotype Annotations. We learned the difference between @Service, @Comopenent, and @Repository. Also, we understood that they behave the same but help in classifying the classes in application. For more on Spring and Spring Boot please visit Spring Tutorials.