This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Question: How to distinguish the @Component, @repository, and @service annotations in Spring?

Are the @Component, @Repository, and @Service annotations interchangeable in Spring, or do they provide any specific functionality beyond serving as annotations?

In other words, if I have a Service class and I change the annotation from @Service to @Component, does it still behave the same?

Or do annotations also affect the behavior and functionality of the class?

Answer a

The @Repository annotation is a marker for any class that satisfies the storage role or stereotype (also known as a data access object or DAO). One use of this tag is to automatically translate exceptions.

Spring provides further stereotype annotations :@Component, @Service, and @Controller. @Component is a generic prototype for any Spring-managed Component. @Repository, @Service, and @Controller are specializations of @Component for more specific classifications (in the persistence, Service, and presentation layers respectively). Therefore, you can annotate Component classes with @Component, but by annotating them with @repository, @Service, or @Controller, your classes will be handled better by another tool or Aop aspect.

For example, these prototype annotations are ideal targets for pointcuts. @Repository, @Service, and @Controller can also carry additional semantics in future versions of the Spring framework. Therefore, if there is a choice between using @Component or @service in the Service layer, @Service is clearly the better choice. Similarly, as mentioned earlier, @Repository is already supported as a marker for automatic exception conversions in the persistence layer.

annotations meaning
@Component A common prototype for any Spring management component
@Repository Persistence layer prototype
@Service Service Layer prototype
@Controller Presentation layer prototype (Spring-MVC)

Answer two

Since many of the answers already illustrate the use of these annotations, we’ll focus on some of the nuances here.

Let’s start with the similarities

It is worth emphasizing again that all of these annotations (@Component, @Service, @repository, @Controller) are the same for automatic detection and dependency injection of BeanDefinition. We can substitute one for the other and still make sense.

The difference between these notes

@Component

This is a generic stereotype annotation to indicate that the class is a Spring component

  • What’s so special about @Component?


scans only @Component, not @Controller, @service, and @repository. They are scanned because they have the @Component annotation themselves.

Consider the @Controller, @service, and @repository annotation definitions:

@Component public @interface Service {... .}Copy the code
@Component public @Interface Repository {... .}Copy the code
@Component public @interface Controller {... }Copy the code

Thus, @Controller, @Service, and @Repository are special types of @Component annotations.

selects them and registers their classes as beans, as if they were annotated by @Component.

Special types of annotations are also scanned because they themselves are annotated with @Component, which means they are also @Components. If we define our own custom annotation and annotate it with @Component, it will also be scanned by

.

@Repository

This annotation defines the class as a data store class

  • What’s so special about @Repository?

In addition to pointing out that this is an annotation-based configuration, @Repository’s job is to catch platform-specific exceptions and rethrow them as Spring’s unchecked exceptions. For this reason, provides the PersistenceExceptionTranslationPostProcessor, need to add it to the Spring application context

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
Copy the code

The bean processor adds an Advisor to any bean with the @Repository annotation to catch any platform-specific exceptions

@Controller

The @Controller annotation indicates that a particular class acts as a Controller.

  • What’s so special about @Controller?

We cannot switch this annotation with any other annotation such as @Service or @repository, even if they look the same. The scheduler scans classes annotated with @Controller and detects methods annotated with @requestMapping. We can only use @requestMapping on methods on classes annotated with @Controller; it can’t be used with @Component, @Service, @repository, etc

@Service

@service writes the business logic.

  • What’s so special about @service?

There is nothing special in this annotation except to indicate that the annotated class is used to write business logic. But maybe Spring will add more features to it in the future.

As mentioned above, Spring may add special functionality based on the hierarchical conventions of @Service, @Controller, and @Repository. Therefore, it is always a good idea to respect the convention and use it in layers.

The article translated from Stack Overflow:stackoverflow.com/questions/6…