본문 바로가기
IT/Java

@Resource 관련

by 성준하이 2023. 9. 22.
반응형

이전 포스팅에서 Autowired 관련해서 다룬 글이 있다.

자세한 내용은 아래 참고 포스팅 참고 바란다.

 

포스팅에서 다룰 Resource 랑 공통점은 의존성을 주입 받기 위한 annotation이다.

 

그럼 둘의 차이에 대해서 알아보면

  Autowired Resource
출처 Spring Framework 표준자바
import org.springframework.beans.factory.annotation.Autowired  javax.annotation.Resource 
bean 검색 시 타입 우선 검색 이름 우선 검색
이름 지정 @Autowired
@Qualifier("test")
@Resource(name = "test")

 

의 차이들이 있다.

 

여기서 주요하게 봐야하는것은

 

bean 검색시 타입 우선과 이름 우선에 대해서 봐야한다.

 

Autowired로 주입을 받게 될 경우에는 주입 받을 클래스나 interface의 타입을 먼저 검색하고 동일한게 있을경우엔 이름까지 동일한 bean을 주입 받는다.

즉 아래코드에서 보면

 

@Autowired

private TestService service;

이런 코드가 있다면

1. 스프링프레임워크에 등록되어있는 bean들 중에 TestService 형이 있는지를 확인하고

2. 있는데 여러개가 있다면 이름이 service인 bean을 찾는다.

 

만약

@Resource

private TestService service;

로 되어있다면

1. 스프링프레임워크에 등록되어있는 bean들 중에 service의 이름이 가진 bean을 확인하고

2. 있는데 여러개가 있다면 TestService형을 찾는다.


참고 포스팅

https://thenicesj.tistory.com/278

 

@Autowired 대신 @RequiredArgsConstructor

Spring 에서는 다양한 장점이 존재하는데 그중 하나는 의존성 주입(DI) 에 대한 개념이다. 이 개념이 어떤 의미인지 모른다면 아래 참고포스팅에서 확인하고 오면 도움이 될것이다. 여기서 나오는

thenicesj.tistory.com

https://thenicesj.tistory.com/634

 

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify t

spring 개발 할 시 제목과 같은 이 에러가 날 경우 해결법이다. Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed 그대로 번

thenicesj.tistory.com

 

반응형

'IT > Java' 카테고리의 다른 글

implements, extends 실 사용 예제  (48) 2023.09.27
Inner Class(이너클래스, 내부클래스)  (33) 2023.09.24
[Java] Iterator  (62) 2023.09.21
@ExceptionHandler 와 @ControllerAdvice 예외 처리하기  (39) 2023.09.19
[JPA] @OneToMany , @ManyToOne  (61) 2023.09.18

댓글