본문 바로가기
백엔드/Java

Spring @Autowired 사용시 주의점

by david100gom 2019. 3. 11.
728x90

 

@Autowired 사용시 동일한 타입의 Bean 객체를 2개 이상일때  Injection of autowired dependencies failed 에러 발생하게 됩니다.

  • 원인 : expected single matching bean but found 2:
  • 해결방법 :  @Autowired 사용시에는 동일한 타입을 Bean 객체를 2개사용하지 말것. (@Autowired 로 명시해준 클래스의 동일한 클래스를 XML 설정파일에 bean 으로 설정하지 말것.) 

만약, 동일한 클래스를 2개 이상 설정해줘야 한다면,

예) bean id가 main 일 경우 @Qualifier("main") 를 함께 사용하여 이름을 지정하거나, @Resource(name = "main") 를 이용하여 이름을 지정한다. 

참고 URL  : http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-autowired-annotation

기타 : 

@Autowired wires per type and is a spring annotation 

@Resource wires per bean name and is specified by the JSR-250. 

여기까지 Spring @Autowired 사용시 주의점 이였습니다.

 

728x90

'백엔드 > Java' 카테고리의 다른 글

Effective Java 맛보기 1탄  (0) 2019.09.14
Spring Data Redis - Jedis vs Lettuce  (0) 2019.08.18
리팩토링  맛보기 1탄  (0) 2019.08.11
JUnit 4.5 에러  (0) 2019.03.13
Spring + Mybatis + Junit 단위 테스트  (0) 2017.09.06
PowerMockup  (0) 2013.10.09
인터페이스 활용  (1) 2013.03.06
jstat - JVM 통계 데이터 감시 툴  (1) 2009.08.12

댓글