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

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

    @Component acts as @Bean annotation in configuration class , register bean in spring context. Also it is parent for @Service, @Repository and @Controller annotation.

    @Service, extends @Component annotation and has only naming difference.

    @Repository - extends @Component annotation and translate all database exceptions into DataAccessException.

    @Controller - acts as controller in MVC pattern. The dispatcher will scan such annotated classes for mapped methods, detecting @RequestMapping annotations.

提交回复
热议问题