Inflearn Community Q&A
Autowired 빨간줄 이유
Resolved
Written on
·
525
·
Edited
0
Autowired 빨간줄 뜨는 이유가 DiscountService를 Component로 등록하지 않아서 그런건가요? 의존관계를 주입할 때는 항상 Component로 등록되어야하는지도 궁금합니다
spring객체지향
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 1
3
안녕하세요. kong님, 공식 서포터즈 y2gcoder입니다.
말씀하신 것이 맞습니다. 기본적으로 @Autowired는 사용하는 대상 클래스가 빈으로 등록되어야 적용되기 때문입니다. 다만 해당 빨간 줄은 IDE에서 런타임 전에 코드만 분석하여 판단하고 표시해주는 일종의 Warning입니다. 해당 테스트 코드에서는 ApplicationContext 를 통해 스프링 컨테이너를 임의로 만들고, 컨테이너 내에 DiscountService를 직접 주입하는 방식으로 만들고 있습니다. 이를 IDE에서 감지하지 못해 저렇게 빨간 줄을 띄우고 있는 것입니다!
감사합니다.





