티스토리 뷰

728x90

 

 

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

의역, 오역 있음. 번역 못하면 그냥 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이다. 

728x90
댓글
250x250
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함