Can we interchange @Controller and @Service in spring? I tried and it was working. How are they implemented internally?
2 Answers
@Controller and @Service are special form of @Component. Spring allows you to use it interchangeably, but is it NOT recommended to do it that way. As for instance the @Controller is used on classes to that serves are the Controller on the MVC. Also, spring dispatcher servlet will scan for @RequestMapping on classes which are annotated using @Controller.

- 1,018
- 1
- 14
- 33
@Component
------@Controller
------@Service
------@Repository
@Controller
and @Service
are ultimately part of @Component
. You may interchange them but it is not recommended or follow best practices. The purpose of using 3 different annotation is to we can have separate the layers based on it use more appropriate annotation.
@Controller:
Annotated class indicates that it is a controller component, and mainly used at the presentation layer.
@Controller is used to mark classes as Spring MVC Controller. This annotation is just a specialized version of @Component and it allows the controller classes to be auto-detected based on classpath scanning.
@Service: It indicates annotated class is a Service component in the business layer.

- 12,879
- 7
- 47
- 76