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

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

    In Spring @Component, @Service, @Controller, and @Repository are Stereotype annotations which are used for:

    @Controller: where your request mapping from presentation page done i.e. Presentation layer won't go to any other file it goes directly to @Controller class and checks for requested path in @RequestMapping annotation which written before method calls if necessary.

    @Service: All business logic is here i.e. Data related calculations and all.This annotation of business layer in which our user not directly call persistence method so it will call this method using this annotation. It will request @Repository as per user request

    @Repository: This is Persistence layer(Data Access Layer) of application which used to get data from the database. i.e. all the Database related operations are done by the repository.

    @Component - Annotate your other components (for example REST resource classes) with a component stereotype.

    Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

    Other class-level annotations may be considered as identifying a component as well, typically a special kind of component: e.g. the @Repository annotation or AspectJ's @Aspect annotation.

    0 讨论(0)
  • 2020-11-22 00:41

    Annotate other components with @Component, for example REST Resource classes.

    @Component
    public class AdressComp{
        .......
        ...//some code here    
    }
    

    @Component is a generic stereotype for any Spring managed component.

    @Controller, @Service and @Repository are Specializations of @Component for specific use cases.

    @Component in Spring

    0 讨论(0)
  • 2020-11-22 00:44

    A @Service to quote spring documentation,

    Indicates that an annotated class is a "Service", originally defined by Domain-Driven Design (Evans, 2003) as "an operation offered as an interface that stands alone in the model, with no encapsulated state." May also indicate that a class is a "Business Service Facade" (in the Core J2EE patterns sense), or something similar. This annotation is a general-purpose stereotype and individual teams may narrow their semantics and use as appropriate.

    If you look at domain driven design by eric evans,

    A SERVICE is an operation offered as an interface that stands alone in the model, without encapsulating state, as ENTITIES and VALUE OBJECTS do. SERVICES are a common pattern in technical frameworks, but they can also apply in the domain layer. The name service emphasizes the relationship with other objects. Unlike ENTITIES and VALUE OBJECTS, it is defined purely in terms of what it can do for a client. A SERVICE tends to be named for an activity, rather than an entity—a verb rather than a noun. A SERVICE can still have an abstract, intentional definition; it just has a different flavor than the definition of an object. A SERVICE should still have a defined responsibility, and that responsibility and the interface fulfilling it should be defined as part of the domain model. Operation names should come from the UBIQUITOUS LANGUAGE or be introduced into it. Parameters and results should be domain objects. SERVICES should be used judiciously and not allowed to strip the ENTITIES and VALUE OBJECTS of all their behavior. But when an operation is actually an important domain concept, a SERVICE forms a natural part of a MODEL-DRIVEN DESIGN. Declared in the model as a SERVICE, rather than as a phony object that doesn't actually represent anything, the standalone operation will not mislead anyone.

    and a Repository as per Eric Evans,

    A REPOSITORY represents all objects of a certain type as a conceptual set (usually emulated). It acts like a collection, except with more elaborate querying capability. Objects of the appropriate type are added and removed, and the machinery behind the REPOSITORY inserts them or deletes them from the database. This definition gathers a cohesive set of responsibilities for providing access to the roots of AGGREGATES from early life cycle through the end.

    0 讨论(0)
  • 2020-11-22 00:45

    all these annotations are type of stereo type type of annotation,the difference between these three annotations are

    • If we add the @Component then it tells the role of class is a component class it means it is a class consisting some logic,but it does not tell whether a class containing a specifically business or persistence or controller logic so we don't use directly this @Component annotation
    • If we add @Service annotation then it tells that a role of class consisting business logic
    • If we add @Repository on top of class then it tells that a class consisting persistence logic
    • Here @Component is a base annotation for @Service,@Repository and @Controller annotations

    for example

    package com.spring.anno;
    @Service
    public class TestBean
    {
        public void m1()
        {
           //business code
        }
    }
    
    package com.spring.anno;
    @Repository
    public class TestBean
    {
        public void update()
        {
           //persistence code
        }
    }
    
    • whenever we adds the @Service or @Repositroy or @Controller annotation by default @Component annotation is going to existence on top of the class
    0 讨论(0)
  • 2020-11-22 00:45

    In spring framework provides some special type of annotations,called stereotype annotations. These are following:-

    @RestController- Declare at controller level.
    @Controller – Declare at controller level.
    @Component – Declare at Bean/entity level.
    @Repository – Declare at DAO level.
    @Service – Declare at BO level.
    

    above declared annotations are special because when we add <context:component-scan> into xxx-servlet.xml file ,spring will automatically create the object of those classes which are annotated with above annotation during context creation/loading phase.

    0 讨论(0)
  • 2020-11-22 00:46

    Use of @Service and @Repository annotations are important from database connection perspective.

    1. Use @Service for all your web service type of DB connections
    2. Use @Repository for all your stored proc DB connections

    If you do not use the proper annotations, you may face commit exceptions overridden by rollback transactions. You will see exceptions during stress load test that is related to roll back JDBC transactions.

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