What's the difference between @Component, @Repository & @Service annotations in Spring?

后端 未结 29 2176
时光说笑
时光说笑 2020-11-22 00:33

Can @Component, @Repository and @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?

29条回答
  •  不知归路
    2020-11-22 00:59

    There is no difference between @Component, @Service, @Controller, @Repository. @Component is the Generic annotation to represent the component of our MVC. But there will be several components as part of our MVC application like service layer components, persistence layer components and presentation layer components. So to differentiate them Spring people have given the other three annotations also.

    • To represent persistence layer components: @Repository
    • To represent service layer components: @Service
    • To represent presentation layer components: @Controller
    • or else you can use @Component for all of them.

提交回复
热议问题