This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

Q: What is the difference between the @Component, @repository and @service annotations in Spring?

Can @Component, @repository, and @service annotations be used interchangeably in Spring, or do they provide any specific functionality beyond being an annotation device?

In other words, if I had a Service class and changed the annotation from @service to @Component, would it still behave the same way?

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

Answer:


A lot of knowledge points, really need to write out will master ! ! !   \color{purple} a lot of knowledge points, really need to write out just can master!! {~}

From the Spring documentation:

The @repository annotation is a marker for the role of a satisfied Repository (also known as a data access object or DAO) or any class of a fixed type. The purpose of this tag, as described in Exception Translation, is to automatically translate exceptions.

Spring provides further typified annotations: @Component, @Service, and @Controller. @Component is a common stereotype for any Spring-managed Component. @Repository, @Service, and @Controller are specialization of @Component for more specific use cases (at the persistence, Service, and presentation layers, respectively). Therefore, you can annotate your Component classes with @Component, but by annotating them with @repository, @Service, or @Controller, your classes are better suited to be handled by tools or associated with aspects.

For example, these stereotype annotations are ideal targets for pointcuts. @repository, @service, and @Controller could include other semantics in future versions of the Spring framework. Therefore, if you have a choice between using @Component or @service at the Service layer, @Service is clearly the better choice. Also, as mentioned earlier, @Repository is already supported as a marker for automatic exception conversion in the persistence layer.

@Component Generic stereotype for any spring-managed Component @Repository persistence layer stereotype @Service Service layer stereotype @Controller presentation layer stereotype (spring-MVC)Copy the code

Answer:

Since many of the answers already illustrate the purpose of these annotations, we will focus on some of the subtle differences between them.

The first is similarityCopy the code

The first point worth reemphasizing is that all of these annotations (i.e. @Component, @Service, @repository, @Controller) are the same for BeanDefinition scan auto-detection and dependency injection. We can replace one with the other and still solve the problem.

The difference between @ Component, @ Repository, @ Controller and @Service

@Component
Copy the code

This is a generic stereotype comment indicating that the class is a Spring component.

What special @Component context is there: Component-scan only scans @Component and does not look for @Controller, @Service, or @repository. They are scanned because they have the @Component annotation themselves.

Just take a look at the definition of the @Controller, @service, and @repository annotations:

@Component public @interface Service {… .}

@Component public @Interface Repository {… .}

@Component public @interface Controller {… }

So it’s not a mistake to say that @Controller, @Service and @Repository are special types of @Component annotations. Context: Component-scan picks them up and registers their successors as beans, just as they use annotations @Component.

Special types of comments are also scanned because they are themselves annotated by @Component, which means they are also @Components. If we define our own custom annotation and use annotation @component, it will also be scanned using context: Component-scan

@epository
Copy the code

This means that the class defines a data repository.

What’s so special about @Repository?

Besides pointing out that this is an Annotation based Configuration, its @Repository job is to catch platform-specific exceptions and rethrow them as one of Spring’s uniform unchecked exceptions. To this end, we provide the PersistenceExceptionTranslationPostProcessor, we need to add it to the Spring application context, as shown below:

The bean post-handler adds the consultant to any annotated bean, @repository, to catch any platform-specific exceptions and then rethrows it as one of Spring’s unchecked data access exceptions.

@Controller
Copy the code

The @Controller annotation indicates that the specific class supplies the role of the Controller. The @Controller annotation acts as an annotation class stereotype, indicating its usefulness.

What’s so special about @Controller?

Even if they look the same, we can’t compare them to any other similar @service or switch @repository. The scheduler scans @Controller classes annotated with @RequestMapping and detects annotated methods in them. We can use @requestMapping on/only, its class annotation and method @controller, this will not work with @Component, @service, @repository etc…

Note: If a class has passed any other method (e.g. via @bean or via @Component, @service, etc.)… @requestMapping This class can be selected if it has already been annotated with @RequestMapping annotations. But this is a different situation.

@Service
Copy the code

The @Service bean holds the business logic and invocation methods in the repository layer.

What’s so special about @service?

There is nothing to note in this comment other than the fact that it is used to indicate that it maintains business logic. But who knows, Spring may add some additional exceptions in the future.

What else?Copy the code

Similarly, Spring may add special functions for @Service, @Controller, and @repository according to their hierarchical conventions in the future. Therefore, it is always a good idea to respect conventions and work with layers.

The article translated from am2dgbqfb6mk75jcyanzabc67y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 6…

The author suggests: large phase courtyard diameter, stratified development

Thank you for reading this, if this article is well written and if you feel there is something to it

Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!

If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️