Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring has @Component annotation, what is the real purpose of the annotations @Repository, @Service, @Controller? [duplicate]

I've developed web applications for a few years with Spring framework. Recently, a fresher of my team asked me a question, Spring has @Component annotation, what is the real purpose of the annotations @Repository, @Service, @Controller? I tried to give him the answer that there is no difference among them, only for identifying types of java Bean. As you know, my explanation lacks of persuasion, he didn't buy it.

So I want to ask a question, what is the real purpose of the annotations @Repository, @Service, @Controller? what the real difference among these annotations?

like image 918
Brady Zhu Avatar asked Mar 20 '13 12:03

Brady Zhu


People also ask

What is the purpose of @component in Spring?

@Component is an annotation that allows Spring to automatically detect our custom beans. In other words, without having to write any explicit code, Spring will: Scan our application for classes annotated with @Component. Instantiate them and inject any specified dependencies into them.

What do @component @service @controller and @repository do?

@Component is a generic stereotype for any Spring-managed component. @Service annotates classes at the service layer. @Repository annotates classes at the persistence layer, which will act as a database repository.

What happens if use @component instead of @controller @service repository?

There is no difference between @Component , @Service , @Controller , @Repository . @Component is the Generic annotation to represent the component of our MVC.

What is the difference between @component @repository @service and @controller?

@Component is a generic stereotype for any Spring-managed component or bean. @Repository is a stereotype for the persistence layer. @Service is a stereotype for the service layer. @Controller is a stereotype for the presentation layer (spring-MVC).


1 Answers

This question's been asked before: What's the difference between @Component, @Repository & @Service annotations in Spring?

In Spring 2.0 and later, the @Repository annotation is a marker for any class that fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository. Among the uses of this marker is the automatic translation of exceptions.

Spring 2.5 introduces further stereotype annotations: @Component, @Service, and @Controller. @Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases, for example, in 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 instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts.

Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

like image 139
Stijn Haus Avatar answered Nov 11 '22 19:11

Stijn Haus