@Component, @Controller, @Service, @Repository의 차이점

김명수·2025년 9월 1일

매일메일

목록 보기
1/89
post-thumbnail

@Component, @Controller, @Service, @Repository의 차이점

●특징

  • @Component : 공용 컴포넌트(일반 빈)
  • @Controller : 웹 계층(HTTP 요청 처리)
  • @Service : 서비스 계층(비즈니스 로직)
  • @Repository : 영속성 계층(데이터 접근 및 예외변환)

●@Controller, @Repository 대신 @Component 사용하면 안되는 이유

  • Spring 6(Spring Boot 3) 이전 버전에서는 @Component + @RequestMapping으로도 Bean 및 핸들러로 등록되었지만, Spring 6 이후 부터 @Controller 외에는 핸들러로 등록하지 않아 웹 요청을 정상적으로 수행할 수 없음

  • @Repository를 @Component로 대체할 경우, PersistenceExceptionTranslationPostProcessor에 의해 예외가 DataAccessException으로 변환되지 않으며, 이 경우 데이터 액세스 계층에서 발생하는 예외 처리에 영향을 미칠 수 있음

  • @Service, @Controller, @Repository는 각각 특정 계층을 나타내므로, AOP의 포인트컷을 정의할 때 유용하게 사용될 수 있으며, @Component를 사용하면 이러한 계층 구분이 불분명해져 AOP 적용이 어려울 수 있음

profile
신입개발자

0개의 댓글