일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 빈 충돌
- jwt
- TiL
- 개발자부트캠프추천
- DesignPattern
- 취업리부트코스
- 디자인패턴
- 항해99
- 전략패턴 #StrategyPattern #디자인패턴
- infcon 2024
- KPT회고
- 1주일회고
- 코딩테스트 준비
- 프로그래머스
- 디자인 패턴
- 인프콘 2024
- @FeignClient
- 프로그래머스 이중우선순위큐
- Python
- 커스텀 헤더
- 단기개발자코스
- Spring multimodule
- 99클럽
- 파이썬
- 구글 OAuth login
- 개발자 취업
- JavaScript
- 빈 조회 2개 이상
- jwttoken
- spring batch 5.0
- Today
- Total
m1ndy5's coding blog
UnsatisfiedDependencyException (feat.영한님 강의) 본문
... 증말 이 문제로 몇시간은 헤맨 것같다,,,ㅎ
문제의 시작부터 차근 차근 따라가보자,,,,
먼저 프로젝트에는 2가지의 @Configuration 파일이 있었다.
package hello.core;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDisscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.MemberRepository;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;
import hello.core.member.MemoryMemberRepository;
import hello.core.order.OrderService;
import hello.core.order.OrderServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MemberService memberService(){
return new MemberServiceImpl(memberRepository());
}
@Bean
public OrderService orderService(){
return new OrderServiceImpl(memberRepository(), discountPolicy());
}
@Bean
public MemberRepository memberRepository(){
return new MemoryMemberRepository();
}
@Bean
public DiscountPolicy discountPolicy(){
return new RateDiscountPolicy();
}
}
수동으로 등록시킨 AppConfig파일과
package hello.core;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan(
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class)
)
public class AutoAppConfig {
}
컴포넌트 스캔을 사용해 빈을 등록하는 AutoAppConfig 파일이다.
이 때 AppConfig와 AutoAppConfig 파일의 중복을 막기 위해서 AutoAppConfig에서 @Configuration이 달린 애는 안읽히게 해놨다
즉 AppConfig에 있는 정보들은 빈으로 등록이 안될 것이다!
그리고 나서 모든 테스트를 돌렸는데 이게 웬일 ApplicationTest가 안돌아갔다.
에러메세지는
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'memberServiceImpl' defined in file [/Users/minsung/Desktop/study/core/out/production/classes/hello/core/member/MemberServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'hello.core.member.MemberRepository' available: expected single matching bean but found 2: memoryMemberRepository,memberRepository
이거였고 즉 memoryMemberRepository와 memberRepository 둘 다 빈으로 등록되어 있어서 뭘 의존성 주입해야될지 모르겠어요~ 이런뜻이었다.
일단 @Autowired는 type으로 빈을 조회하기 때문에 MemberRepository 타입을 조회했을 것이다.
근데 내가 여기서 아주 큰 착각을 했다.
MemberRepository 인터페이스가 빈으로 만들어졌다고 착각한 것이다.....
여기서 왜 인터페이스가 빈으로 만들어지는지에 대해서만 미친듯이 삽질하면서 검색한듯...ㅋ
그런데 이유는 다름아닌.......
@SpringBootApplication 에 있었다.........ㅋㅋㅋㅋㅋㅋ (일단 내 뇌피셜)
@SpringBootApplication을 까보면
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
이런 어노테이션들을 포함하고 있는데 @ComponentScan도 있다.
생각해보니까 AutoAppConfig에서만 막는다고 AppConfig가 안읽히는게 아니었다;;;
이렇게 문제의 결론은 났지만(아직 안남ㅎ) 덕분에 알게 됐던 지식들도 있다.
여러개의 Component가 등록됐을 때 그중 사용하고 싶은 한개에 @Primary를 붙여주면 @Autowired가 걔를 읽어준다는 것과
@Autowired 대신 @RequiredArgsConstructor을 사용하는 것을 권장하는 것!!
이건 아마 앞으로의 강의에서 설명해주실 것같아서 일단은 그렇구나 정도로 넘어가려고 한다!
추가
@SpringbootApplication 어노테이션 때문이 아닌가 싶기도 한데...
아시는 분 헬프미요ㅠㅜㅠㅜㅠ
일단 https://stackoverflow.com/questions/71399411/how-can-i-exclude-a-specific-configuration-class-from-my-spring-boot-applicatio 이 사람은 나랑 똑같이 생각하는 것 같긴하다..
인프런 질문
답이 달리는 것 보고 업로드해야겠다.
'백엔드 with java > spring' 카테고리의 다른 글
About Setter (feat. Builder) (0) | 2024.01.17 |
---|---|
Entity 대신 DTO를 반환해야하는 이유 (0) | 2024.01.17 |
Spring Security UserDetails, UserDetailsService (1) | 2023.10.11 |
Jackson & Constructor (0) | 2023.07.11 |
정적 컨텐츠 VS MVC와 템플릿엔진 VS API (0) | 2023.03.07 |