Is Spring annotation @Controller same as @Service?

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

    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?

提交回复
热议问题