Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Spring annotation @Controller same as @Service?

Is Spring annotation @Controller same as @Service?

I have idea about @Controller which can be used for URL mapping and invoking business logic.

while @Service used to annotate service class which contains business logic.

Can I use @Controller instead of @Service to annotate Service class?

like image 204
Ketan Avatar asked Apr 10 '13 10:04

Ketan


People also ask

What is the difference between @controller and @service?

@Controller annotation indicates that a particular class serves the role of a controller. @Service annotation is used with classes that provide some business functionalities. @Controller annotation is a specialization of @Component annotation. @Service Annotation is also a specialization of @Component Annotation.

What is the difference between @controller @service and @repository in Spring?

@Repository Annotation is used to indicate that the class provides the mechanism for storage, retrieval, update, delete and search operation on objects. @Controller annotation indicates that a particular class serves the role of a controller. @Service Annotation is a specialization of @Component Annotation.

What's the difference between @component @controller @repository and @service annotation in Spring?

Here's a quick overview of a few of these annotations: @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 is & diff between @service @controller and @component?

@Component : It is a basic auto component scan annotation, it indicates annotated class is an auto scan component. @Controller : Annotated class indicates that it is a controller component, and mainly used at the presentation layer. @Service : It indicates annotated class is a Service component in the business layer.


2 Answers

No, they are pretty different from each other.

Both are different specializations of @Component annotation (in practice, they're two different implementations of the same interface) so both can be discovered by the classpath scanning (if you declare it in your XML configuration)

@Service annotation is used in your service layer and annotates classes that perform service tasks, often you don't use it but in many case you use this annotation to represent a best practice. For example, you could directly call a DAO class to persist an object to your database but this is horrible. It is pretty good to call a service class that calls a DAO. This is a good thing to perform the separation of concerns pattern.

@Controller annotation is an annotation used in Spring MVC framework (the component of Spring Framework used to implement Web Application). The @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations.

So looking at the Spring MVC architecture you have a DispatcherServlet class (that you declare in your XML configuration) that represent a front controller that dispatch all the HTTP Request towards the appropriate controller classes (annotated by @Controller). This class perform the business logic (and can call the services) by its method. These classes (or its methods) are typically annotated also with @RequestMapping annotation that specify what HTTP Request is handled by the controller and by its method.

For example:

@Controller @RequestMapping("/appointments") public class AppointmentsController {      private final AppointmentBook appointmentBook;      @Autowired     public AppointmentsController(AppointmentBook appointmentBook) {         this.appointmentBook = appointmentBook;     }      @RequestMapping(method = RequestMethod.GET)     public Map<String, Appointment> get() {         return appointmentBook.getAppointmentsForToday();     } 

This class is a controller.

This class handles all the HTTP Request toward "/appointments" "folder" and in particular the get method is the method called to handle all the GET HTTP Request toward the folder "/appointments".

I hope that now it is more clear for you.

like image 181
AndreaNobili Avatar answered Sep 21 '22 00:09

AndreaNobili


If you look at the definitions of @Controller, @Service annotations, then you'll find that these are special type of @Component annotation.

@Component public @interface Service {     …. } 

 

@Component public @interface Controller {     … } 

So what's the difference?

@Controller

The @Controller annotation indicates that a particular class serves the role of a controller. The @Controller annotation acts as a stereotype for the annotated class, indicating its role.

What’s special about @Controller?

You cannot switch this annotation with any other like @Service or @Repository, even though they look same. The dispatcher scans the classes annotated with @Controller and detects @RequestMapping annotations within them. You can only use @RequestMapping on @Controller annotated classes.


@Service

@Services hold business logic and call method in repository layer.

What’s special about @Service?

Apart from the fact that it is used to indicate that it's holding the business logic, there’s no noticeable specialty that this annotation provides, but who knows, spring may add some additional exceptional in future.

Linked answer: What's the difference between @Component, @Repository & @Service annotations in Spring?

like image 29
Raman Sahasi Avatar answered Sep 21 '22 00:09

Raman Sahasi