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?
@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.
@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.
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.
@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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With