Component, @Service 차이 - Component, @Service chai

카카오 면접을 준비하면서, 공부했던 내용을 정리해놓고 다시 기억하기 위한 포스팅
Component, @Service 차이 - Component, @Service chai
Component, @Service 차이 - Component, @Service chai

@Component

Spring에서 관리되는 객체임을 표시하기 위해 사용하는 가장 기본적인 annotation이다. 즉, scan-auto-detection과 dependency injection을 사용하기 위해서 사용되는 가장 기본 어노테이션이다.

@Controller

Web MVC 코드에 사용되는 어노테이션이다. @RequestMapping 어노테이션을 해당 어노테이션 밑에서만 사용할 수 있다. 

@Repository

이것은 Annotation based Configuration, @Repository의 작업은 플랫폼 별 예외를 잡아서 Spring의 통합 검사되지 않은 예외 중 하나로 다시 던지는 것입니다. 이를 위해 PersistenceExceptionTranslationPostProcessor이 제공되며 다음과 같이 Spring의 애플리케이션 컨텍스트에 추가해야합니다.

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

이 빈 포스트 프로세서는 @Repository로 주석이 달린 모든 빈에 권고자를 추가하여 플랫폼 별 예외를 포착 한 다음 Spring의 확인되지 않은 데이터 액세스 예외 중 하나로 다시 발생시킵니다.

@Service

비즈니스 로직이나 respository layer 호출하는 함수에 사용된다. 다른 어노테이션과 다르게 @Component에 추가된 기능은 없다. 하지만 나중에 Spring 측에서 추가적인 exception handling을 해줄 수도 있으니 비즈니스 로직에는 해당 어노테이션을 사용하자.


일반적으로 컴포넌트 클래스들에 @Component를 붙일 수 있지만, @Repository, @Service, @Controller를 붙인다면  도구들이 클래스들을 처리하는데 더 적합하도록 할 수 있고 관점(aspects)에 더 연관성을 부여할 수 있다. - AOP 를 통한 처리가 쉽게 가능하다



IT기업 취업을 준비하는데 막막하신 분들을 위해 네이버 공채 합격자도 배출하고, 수강생들이 추천하는 원데이클래스를 소개해드릴게요.

취업준비가 막막하시고 어떻게 공부를 해야하고 시작해야할지 답답하신 분들에게 추천드려요!

전체적인 취업 프로세스에 대한 설명, 준비 방법, 공부 방향성을 제시해드려요. 수강생 모두 평점 만점을 준 만족도 높은 강의입니다 :)

네 & 카 개발자가 알려주는 IT기업/대기업 취업의 모든것! #서류 #코테 #면접 | 탈잉

# 진행방식 모든 수업은 온라인 라이브로 진행됩니다. 튜터들의 현재 경력 및 이력 사항에 대한 내용을 확인하고 수업을 진행합니다. 코로나로 인해 오프라인으로 진행하던 수업을 "20% 할인된

taling.me

Component, @Service 차이 - Component, @Service chai
Component, @Service 차이 - Component, @Service chai

스택오버플로우에서 유명한(?) 글을 번역합니다. 원문 

의역, 오역 있음. 번역 못하면 그냥 english 로 적습니다..

Question

@Component, @Repository와 @Service가 상호 교환이 가능하게 쓰여질 수 있나요? 아니면 notation device 외에 부분적으로 기능들이 주어지는 건가요??

그니까, 서비스 클래스에서 @Service 어노테이션을 @Component로 바꿔도 클래스의 역할이 달라지지 않나요?

아니면 클래스에 기능, 행동적으로 영향을 미치게 될까요??

Answer1

@Repository 어노테이션은 repository, DAO의 역할을 하는 모든 클래스에 marker로 쓰이게 됩니다. 이 marker는 예외를 자동으로 번역할 수 있습니다. Exception Translation 참고

스프링은 @Component, @Service 와 @Controller 같은 어노테이션을 기본적으로(stereotype) 제공합니다. @Component는 스프링이 관리하는 모든 구성요소에 대한 기본 타입입니다. @Repository, @Service와 @Controller는 @Componenet의 더 구체적인 쓰임새를 위한 specialization 입니다. 

그러므로 구성되는 클래스에 @Component 어노테이션을 사용할 수 있겠으나 @Repository, @Service나 @Controller 같은 어노테이션을 붙임으로 인해 클래스가 더 적절하게 처리하려는 작업에 더 들어맞게 됩니다.

Annotation Meaning
@Component generic streotype for any Spring-managed component
@Repository stereotype for persistence layer
@Service stereotype for service layer
@Controller stereotype for presentation layer (spring-mvc)

Answer2

@Component

클래스가 스프링의 구성요소임을 말해주는 기본적인 목적의 어노테이션

@Component의 특벌한 점이 있다면 <contect:component-scan>이 오직 @Component만을 스캔하고 @Controller와 @Service, @Repository는 일반적으로 찾지 않음. 이들은 이미 스스로를 @Component로 어노테이션 하기 때문에 알아서 스캔된다고,,????(they are scanned because they themselves are annotated with @component)

@Controller, @Service, @Repository 어노테이션의 정의를 보면

@Component
public @interface Service{ }

@Component
public @interface Repository{ }

@Component
public @interface Controller{ }

이렇게 되어 있음. 그러니까 @Controller, @Service, @Repository가 @Component어노테이션의 특별한 타입이라고 말할 수 있겠다. <context:component-scan>은 이들을 골라 이들의 클래스를 @Component가 붙은 것 처럼 빈으로 등록하게 된다.

특별한 타입의 어노테이션들은 결국 얘네도 @Components이므로 스캔이 된다. 어쩌구,,

@Repository

data repository를 정의하기 위해서 사용됨

@Repository의 특별한 점은?

@Repository는 annotation을 기반으로 한 configuration이다.