Is Spring annotation @Controller same as @Service?

后端 未结 9 1882
夕颜
夕颜 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: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.

提交回复
热议问题