ERROR RESOLVING

오류 UnsatisfiedDependencyException

Adev 2023. 2. 11. 00:01


상황 

스프링  Autowired 의존성 자동주입을 사용하던 중 아래와 같이 동일한 타입의 빈이 2개 발견된다는 에러가 발생했다.

 

ERROR: org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@bd4dc25] to prepare test instance [com.toy.mapper.BoardServiceTests@35d3ab60]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.toy.mapper.BoardServiceTests': Unsatisfied dependency expressed through method 'setService' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.toy.board.BoardService' available: expected single matching bean but found 2: boardServiceImpl,boardService
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:667)

 

 


해결

오류코드에 BoardService타입이 boardServiceImpl, boardService 2개 발견된다고 적혀있기때문에

"found 2: boardServiceImpl,boardService"

이 중에 하나를 필드명으로 명시한다.

 

//오류

@Setter(onMethod_= {@Autowired})
private BoardService service;

 

//해결

@Setter(onMethod_= {@Autowired})
private BoardService boardService;