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

后端 未结 29 2168
时光说笑
时光说笑 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:57
    @Component
    @Controller
    @Repository
    @Service
    @RestController
    

    These are all StereoType annotations.this are usefull for the making our classes as spring beans in ioc container,

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 2020-11-22 01:01

    @Component, @ Repository, @ Service, @Controller:

    @Component is a generic stereotype for the components managed by Spring @Repository, @Service, and @Controller are @Component specializations for more specific uses:

    • @Repository for persistence
    • @Service for services and transactions
    • @Controller for MVC controllers

    Why use @Repository, @Service, @Controller over @Component? We can mark our component classes with @Component, but if instead we use the alternative that adapts to the expected functionality. Our classes are better suited to the functionality expected in each particular case.

    A class annotated with @Repository has a better translation and readable error handling with org.springframework.dao.DataAccessException. Ideal for implementing components that access data (DataAccessObject or DAO).

    An annotated class with @Controller plays a controller role in a Spring Web MVC application

    An annotated class with @Service plays a role in business logic services, example Facade pattern for DAO Manager (Facade) and transaction handling

    0 讨论(0)
  • 2020-11-22 01:03

    They are almost the same - all of them mean that the class is a Spring bean. @Service, @Repository and @Controller are specialized @Components. You can choose to perform specific actions with them. For example:

    • @Controller beans are used by spring-mvc
    • @Repository beans are eligible for persistence exception translation

    Another thing is that you designate the components semantically to different layers.

    One thing that @Component offers is that you can annotate other annotations with it, and then use them the same way as @Service.

    For example recently I made:

    @Component
    @Scope("prototype")
    public @interface ScheduledJob {..}
    

    So all classes annotated with @ScheduledJob are spring beans and in addition to that are registered as quartz jobs. You just have to provide code that handles the specific annotation.

    0 讨论(0)
  • 2020-11-22 01:03

    We can answer this according to java standard

    Referring to JSR-330, which is now supported by spring, you can only use @Named to define a bean (Somehow @Named=@Component). So according to this standard, there seems that there is no use to define stereotypes (like @Repository, @Service, @Controller) to categories beans.

    But spring user these different annotations in different for the specific use, for example:

    1. Help developers define a better category for the competent. This categorizing may become helpful in some cases. (For example when you are using aspect-oriented, these can be a good candidate for pointcuts)
    2. @Repository annotation will add some functionality to your bean (some automatic exception translation to your bean persistence layer).
    3. If you are using spring MVC, the @RequestMapping can only be added to classes which are annotated by @Controller.
    0 讨论(0)
  • 2020-11-22 01:03

    Good enough answers are here to explain the whats-the-difference-between-component-repository-service-annotations. I would like to share the difference between @Controller & @RestController

    @Controller vs RestController

    @RestController:

    • This annotation is a specialized version of @Controller which adds @Controller and @ResponseBody annotation automatically. so we do not have to add @ResponseBody to our mapping methods. That means @ResponseBody is default active.
    • If you use @RestController you cannot return a view (By using Viewresolver in Spring/Spring-Boot)
    • @RestController also converts the response to JSON/XML automatically as @ResponseBody makes the returned objects to something that could be in the body, e.g. JSON or XML

    @Controller

    • @Controller is used to mark classes as Spring MVC Controller. This annotation is just a specialized version of @Component and it allows the controller classes to be auto-detected based on classpath scanning.
    • @Controller you can return a view in Spring web MVC.

    More Detailed View

    0 讨论(0)
提交回复
热议问题