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

后端 未结 29 2195
时光说笑
时光说笑 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:51

    Difference between @Component, @Repository, @Controller & @Service annotations

    @Component – generic and can be used across application.
    @Service – annotate classes at service layer level.
    @Controller – annotate classes at presentation layers level, mainly used in Spring MVC.
    @Repository – annotate classes at persistence layer, which will act as database repository.

    @Controller = @Component ( Internal Annotation ) + Presentation layer Features
    @Service = @Component ( Internal Annotation ) + Service layer Features
    @Component = Actual Components ( Beans )
    @Repository = @Component ( Internal Annotation ) + Data Layer Features ( use for handling the Domain Beans )

提交回复
热议问题