Introduction:

In this tutorial, you will learn the different Spring stereotype annotations (@Component, @Repository and @Service) to declare any class as a Spring-managed Bean. Spring MVC also has several other annotations like @Controller and @RestController. The classes that are managed as beans in Spring are known as Managed Components.

There are basically few stereotype annotations in spring that declare classes as Managed Components.

  • The @Component annotation declares any class as a managed spring bean.
  • The @Service annotation declares any Service/Business layer class as a managed component.
  • Similarly, the @Repository annotation declares any DTO or Repository class as a managed bean.
  • In Spring MVC, @Controller and @RestController are used to declare Controllers for the presentation layer.

Why use Stereotype annotations?

Stereotypes annotations denote the roles of types or methods in the overall architecture (at a conceptual, rather than implementation, level). Intended for use by tools and aspects (making an ideal target for pointcuts).

Stereotype annotation distinguishes the classes from the architecture prospective, it has nothing to do with the actual implementations inside.

How to use the annotations?

Step-1
The simple way to use these annotations is first annotate the @Configuration class with @ComponentScan annotation and specify the package/base-package in which the classes present.

@Configuration
@ComponentScan("com.jbd.webapp")
public class AppConfig {
  //Extra config goes
}

Step-2
Next step is to annotate the classes with respective annotations.

package com.jbd.webapp;

@Repository
public class AppRepository {
  //...
}

Differences between @Component, @Repository, @Service

1. The @Component annotation

We can use @Component annotation in our application to declare any class as the Spring’s managed components. Spring internally picks up and registers beans with @Component and doesn’t look for @Service and @Repository in general. Because the @Service and @Repository are also annotated with @Component.

@Component
public class PdfReader {
  //class members

}

As you can see in the below, @Component annotation accepts optional component name as the parameter.

@Target(ElementType.TYPE) // Can be used on Class, Interface and Enum
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any (or empty String otherwise)
	 */
	String value() default "";

}

2. The @Service annotation

Any class that includes business logic is annotated with @Service annotation. Just like the @Component, all you need is to annotate the class with @Service("optionalName") with an optional name.

@Service("orderService")
public class OrderService {

  //Class details
}

The @Service annotation internally looks as below. It is clear that this annotation is just an alias of @Component. It just does not have any other special feature till now.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any (or empty String otherwise)
	 */
	@AliasFor(annotation = Component.class)
	String value() default "";

}

3. The @Repository annotation

Any class that holds the database or persistent logic is annotated with @Repository. In other words, the DTO/DAO is annotated with @Repository with an optional name parameter.

@Repository
public class OrderDaoImpl implements OrderDao {

  //Order manipulation methods.
}

As you can see this annotation is just an alias of @Component. Remember, @Repository catches persistence specific checked exceptions and rethrow them as one of Spring’s unified unchecked exception.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any (or empty String otherwise)
	 */
	@AliasFor(annotation = Component.class)
	String value() default "";

}

Conclusion:

I hope you learned about the managed beans in Spring using different annotations @Component, @Service and @Repository. Let me know your feedback on this article.