아장아장 개발 일기

@Component VS @Service VS @Repository 어노테이션 차이 본문

개발/Spring

@Component VS @Service VS @Repository 어노테이션 차이

빨간머리 마녀 🍒 2022. 8. 18. 16:53

스프링 클래스를 빈등록 할때면 component, service, repository 어노테이션 중 어떤 걸 써야할지 헷갈렸습니다.

 

repository는 비교적 다른 두개와 구분하기 쉬웠는데, component와 service 어노테이션이 특히 헷갈리더라구요.

 

이번에 baeldung에서 셋의 차이를 간략하게 다룬 글을 찾아서 한번 정리해보았습니다.

 


@Component

1. 스프링이 관리하는 컴포넌트로 등록됨
2. 스프링은 @Component로 등록된 bean만 인식함3. @Service와 @Repository도 @Component 어노테이션을 포함하기때문에 ApplicationContext로 등록됨.

@Repository

1. 특정 persistence에 맞는 예외를 찾고 스프링의 일원화된 체크 되지 않은(unified & unchecked) 예외로 던지기 위함
2. 이를 위해 스프링은 PersistenceExceptionTranslationPostProcessor를 제공
<bean cass= "org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
3. 스프링 부트를 사용한다면 이미 application context에 포함되어 있음

@Service

1. 비즈니스 로직을 담고있는 빈에 표시함
2. 서비스 레이어에서 사용된다는 것을 제외하면 다른 사용 이유는 없음.

🔦 참고한 사이트

https://www.baeldung.com/spring-component-repository-service

 

@Component vs @Repository and @Service in Spring | Baeldung

Learn about the differences between the @Component, @Repository and @Service annotations and when to use them.

www.baeldung.com

 

Comments