Is Spring annotation @Controller same as @Service?

后端 未结 9 1846
夕颜
夕颜 2020-12-12 10:40

Is Spring annotation @Controller same as @Service?

I have idea about @Controller which can be used for URL mappin

相关标签:
9条回答
  • 2020-12-12 11:24

    No you can't they are different. When the app was deployed your controller mappings would be borked for example.

    Why do you want to anyway, a controller is not a service, and vice versa.

    0 讨论(0)
  • 2020-12-12 11:25

    From Spring In Action

    As you can see, this class is annotated with @Controller. On its own, @Controller doesn’t do much. Its primary purpose is to identify this class as a component for component scanning. Because HomeController is annotated with @Controller, Spring’s component scanning automatically discovers it and creates an instance of HomeController as a bean in the Spring application context.

    In fact, a handful of other annotations (including @Component, @Service, and @Repository) serve a purpose similar to @Controller. You could have just as effectively annotated HomeController with any of those other annotations, and it would have still worked the same. The choice of @Controller is, however, more descriptive of this component’s role in the application.

    0 讨论(0)
  • 2020-12-12 11:26

    You can declare a @service as @Controller.

    You can NOT declare an @Controller as @Service

    @Service

    It is regular. You are just declaring class as a Component.

    @Controller

    It is a little more special than Component. The dispatcher will search for @RequestMapping here. So a class annotated with @Controller, will be additionally empowered with declaring URLs through which APIs are called

    0 讨论(0)
提交回复
热议问题