Inflearn Community Q&A
클래스 필드에서도 Map @Autowired를 사용할 수 있나요?
Written on
·
432
0
이번 예제에서 내부 클래스인 static class DiscountSerive에 Map 타입과 List 타입의 멤버변수를 만들고 @Autowired를 설정해 보았습니다. 저는 출력결과가 수업강의에 있었던 예제 처럼 타입에 맞는 빈이 모두 출력될 것이라 기대했는데 결과물은 null이네요. 클래스 필드에서는 @Autowired를 사용할 수 없는것일까요?
<코드>

<결과물>

oopspring
Quiz
What are the main reasons why Field Injection is generally not recommended?
Because it is difficult to guarantee immutability
Because changing dependencies is easy
Because it's difficult to test with pure Java code
Because writing configuration code is complex
Answer 2
0
안녕하세요, Jihwan Jang 님. 공식 서포터즈 codesweaver 입니다.
.
질문하신 경우는 저도 생각치 못했던 경우네요. :)
검색해보니 @Autowired는 우선순위가 있습니다.
아래 링크의 테스트를 확인해보시면 생성자 주입과 필드 주입이 겹칠 경우, 생성자 주입이 우선시 됨을 알 수 있습니다.
https://dololak.tistory.com/565
.
감사합니다.
0






아무래도 그래서 그런 거 같습니다
생성자에서는
policyMap과policies만 주입 받다보니 저 시점에는null이 나오는 것이 당연해보이고그 이후에 필드 부분에 대한 주입이 이루어지는 거 같네요