Spring component annotations

0

I'm starting to use and, so I've seen, the difference between the annotations @Controller , @Repository and @Service is the layer where you have to use it since it could be said that they are an extension / specialization of the @Component of Spring.

Thus, @Repository is for the persistence layer, @Service is for the business layer, and @Controller is for the presentation layer.

Is that so? Do they only differ at that point or are there more connotations?

    
asked by joc 23.11.2016 в 16:29
source

3 answers

4

In principle this is:

  • @Component: It is the general stereotype and allows you to write down a bean so that Spring considers it one of its objects.
  • @Repository: It is the stereotype that is responsible for registering a bean to implement the repository pattern that is responsible for storing data in a database or repository of information that is needed. By marking the bean with this annotation Spring provides cross-sectional services such as conversion of exception types.
  • @Service: This stereotype is responsible for managing the most important business operations at the application level and agglutinates calls to several repositories simultaneously. Its fundamental task is that of aggregator.
  • @Controller: The last of the stereotypes that is the one that performs the tasks of controller and management of the communication between the user and the application. For this, it is usually supported by a template engine or a library of labels that facilitate the creation of pages.

But we can see that since each one has a context for its implementation there may be cases in which they behave differently than expected, either due to the life cycle of the bean or because they have extra functionality.

  

@Service vs. @Repository vs. @Component   Note that in the current version of Spring the @Service annotation does not have a semantics defined other than @Component. That is, it simply helps the one who reads the code to know that the bean belongs to the business layer and it is otherwise indifferent to use one or the other. The @Repository annotation does have an effect on the automatic transactionality, as we will see in the next session. However, the development team of Spring reserves the possibility of adding semantics to these annotations in future versions of the framework.

Use annotations carefully.

Sources:

answered by 23.11.2016 в 19:24
0

The concepts of @Component, @Repository, @Service and @Controller:

@Component is a generic stereotype for the components managed by Spring, @Repository, @Service, and @Controller are @Component specializations for more specific uses:

  • @Repository for persistence
  • @Service for services and transactions
  • @Controller for MVC controllers

Why use @Repository, @Service, @Controller over @Component? We can write down our component classes with @Component, but instead we use some of the previous alternatives where applicable, our classes adapt more and better in each particular case. For example, how they are processed and how they relate to specific aspects of spring.

    
answered by 21.06.2018 в 02:26
0

You're right.

As the Spring documentation says well , @Component is the main stereotype and indicates that a class with this annotation is a component or Spring Bean.

@Repository , @Service and @Controller are specifications of the @Component annotation for specific cases, for example, for the persistence of data, services or for the presentation layer respectively.

However, not only do they differ at this point, the classes registered with @Service , @Repository or @Controller will have other benefits that you would not get with the @Component annotation since some of the Spring modules process these annotations differently.

For example, Spring Data JPA will process @Repository and will attempt to replace implementations of all interfaces marked with this annotation. Another example, Spring Web MVC will process @Controller , and will use the classes marked with that annotation in URL mappings.

    
answered by 27.06.2018 в 11:45