Spring Stereotype Annotations

Anish Antony
Apr 23 · 4 min read

In the previous article, we discussed different spring core annotations. Here we are discussing the main stereotype annotations in Spring.

Photo by Markus Spiske on Unsplash

The Spring Framework provides you with some special annotations. These annotations are used to create Spring beans automatically in the application context. The main stereotype annotation is @Component.

There are some Stereotype meta-annotations which is derived from @Component those are

@Service: Used to create Spring beans at the Service layer,

@Repository: Which is used to create Spring beans for the repositories at the DAO layer, and

@Controller: Which is used to create Spring beans at the controller layer.

The stereotype annotations in spring are @Component, @Service, @Repository and @Controller.

@Component

This annotation is used in classes to indicate a Spring component. The @Component annotation marks the Java class as a bean or says component so that the component-scanning mechanism of Spring can add to the application context.

What is the difference between @Component and @Bean annotation?

Both of these annotations are used for creating spring beans. Here follows the major comparison points between @Component and @Bean annotations

  1. @Component auto-detects and configures the beans using classpath scanning whereas @Bean explicitly declares a single bean, So if you want to create a bean for the class then you need to create a method that returns the class instance and needs to declare that method with @Bean annotation.
  2. @Component does not decouple the declaration of the bean from the class definition whereas @Bean decouples the declaration of the bean from the class definition. So we can keep the class that is completely independent of spring dependency. Mainly the @Bean annotation is used for added spring beans for the class which is defined on external jars.
  3. @Component is a class-level annotation whereas @Bean is a method level annotation and the name of the method serves as the bean name.
  4. @Component need not be used with the @Configuration annotation whereas @Bean annotation has to be used within the class which is annotated with @Configuration.
  5. We cannot create a bean of a class using @Component if the class is outside the spring container whereas we can create a bean of a class using @Bean even if the class is present outside the spring container.

What is Special about the @Component annotation as compared with other stereotype annotations.
<context:component-scan> only scans @Component and does not look for @Controller, @Service and @Repository in general. They are scanned because they themselves are annotated with @Component.

Also if you look at the source of other stereotypes then you can see all other stereotypes are components. Which you can see on going forward.

@Service

@Service is a stereotype for the service layer. The @Service marks a Java class that performs some service, such as execute business logic, perform calculations and call external APIs. This annotation is a specialized form of the @Component annotation intended to be used in the service layer.

Here below explains the code for the @Service annotation. Here you can see it is added @Component annotation in the code. which means @Service annotation is a kind of @Component annotation.

We mark beans with @Service to indicate that it’s holding the business logic. As compared with @Component annotation, there’s not any other specialty except using it in the service layer. But we can expect Spring may add some additional exceptional in future.

@Repository

This annotation is used on Java classes that directly access the database. The @Repository annotation works as a marker for any class that fulfills the role of repository or Data Access Object.

The annotation @Repository is a kind of @Component annotation. Here below explains the code for @Repository annotation.

@Repository’s job is to catch persistence specific exceptions and rethrow them as one of Spring’s unified unchecked exception.

@Repository’s job is to catch platform-specific exceptions and re-throw them as one of Spring’s unified unchecked exceptions. For this, Spring provided with PersistenceExceptionTranslationPostProcessor, that required to add in our Spring’s application context like this:

This bean post-processor adds an advisor to any bean that’s annotated @Repository so that any platform-specific exceptions are caught and then re-thrown as one of Spring’s unchecked data access exceptions.

@Controller

The @Controller annotation is used to indicate the class is a Spring controller. This annotation can be used to identify controllers for Spring MVC or Spring WebFlux.

The @Controller spring bean is also a kind of spring component. Here below explains the code for @Controller.

We cannot switch this annotation with any other like @Service or @Repository, even they look the same. The dispatcher scans the classes annotated with @Controller and detects methods annotated with @RequestMapping annotations within them. We can use @RequestMapping on/in only those methods whose classes are annotated with @Controller and it will NOT work with @Component, @Service, @Repository etc...

Javarevisited

Medium’s largest Java publication, followed by 10000+ programmers. Follow to join our community.

Anish Antony

Written by

Fullstack Developer | Blogger | Experience on Java, Python, React, Angular, Golang | http://www.behindjava.com

Javarevisited

A humble place to learn Java and Programming better.

Anish Antony

Written by

Fullstack Developer | Blogger | Experience on Java, Python, React, Angular, Golang | http://www.behindjava.com

Javarevisited

A humble place to learn Java and Programming better.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store