@Component, @Service

Kyojun Jin·2024년 8월 21일

Spring

목록 보기
12/12

https://docs.spring.io/spring-framework/reference/core/beans/classpath-scanning.html#beans-stereotype-annotations

@Component and Further Stereotype Annotations

The @Repository annotation is a marker for any class that fulfills the role or stereotype of a repository (also known as Data Access Object or DAO). Among the uses of this marker is the automatic translation of exceptions, as described in Exception Translation.

Spring provides further stereotype annotations: @Component, @Service, and @Controller. @Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases (in the persistence, service, and presentation layers, respectively). Therefore, you can annotate your component classes with @Component, but, by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts. @Repository, @Service, and @Controller may also carry additional semantics in future releases of the Spring Framework. Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated earlier, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

@Component 및 추가 고정관념 주석

@Repository 주석은 저장소(Data Access Object 또는 DAO라고도 함)의 역할 또는 고정관념을 충족하는 모든 클래스에 대한 마커입니다. 이 마커의 용도 중에는 예외 번역에서 설명한 예외 자동 번역이 있습니다.

Spring은 @Component, @Service, @Controller와 같은 스테레오 타입 어노테이션을 추가로 제공합니다. @Component는 Spring이 관리하는 모든 컴포넌트의 일반적인 스테레오 타입입니다. @Repository, @Service, @Controller는 보다 구체적인 사용 사례(각각 지속성, 서비스, 프레젠테이션 계층)를 위해 @Component를 전문화한 것입니다. 따라서 컴포넌트 클래스에 @Component로 어노테이션을 달 수 있지만, 대신에 @Repository, @Service, @Controller와 함께 어노테이션을 달면 도구에 의한 처리나 측면과의 연관성에 더 적합합니다. 예를 들어, 이러한 스테레오 타입 어노테이션은 포인트컷의 이상적인 대상이 됩니다. @Repository, @Service, @Controller는 이후 Spring Framework의 릴리스에서 추가적인 메소드를 수행할 수도 있습니다. 따라서 서비스 계층에 @Component@Service 중 하나를 고르고 있다면 @Service가 분명히 더 나은 선택입니다. 마찬가지로, 앞서 설명했듯이 @Repository는 영속성 계층에서 자동 예외 변환을 위한 마커로 이미 지원됩니다.

앵간하면 Service 쓰는 게 나을 것 같다. (기능 상 차이는 없는데 의미의 차이)

0개의 댓글