1. 수동넣기


2. xml 설정파일 property(속성) 사용 ( 변동사항이 생기면 xml 파일만 수정하면된다)

※ xml 설정파일의 property

   +

setter 와 기본생성자 를 이용

     
   xml 설정파일의 property 태그
+
setter 와 기본생성자 를 이용
 xml 설정파일의 constructor-arg 태그
 +
생성자를 이용
 


 


3. @Component + Setter 이용


4.  @Component + @Autowired ( Setter 대신사용 )  

@Autowired : bean(객체)들간의 관계를 맺어주는 것


by Type 사용시 주의사항 : 같은타입이 여러개이면,  by Name을 사용해야한다.


@Autowired 도 by Type 방식으로 검색으로 똑같이 engine이 여러개인데  이건 왜 에러가 안날까?

Autowired의 특성 때문에 가능.
Autowired는 ① 먼저 타입으로 먼저검색 ( 
                     ② 찾는 타입 이 여러개라면 이름으로 일치하는 것 검색 ( engine이 일치하니 반환 )
                      ※ 현재 engine, superEngine, turboEngine (타입의 첫글자를 소문자로 한것 중)
그래서 에러없이 결과잘나온 것


에러메세지
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.fastcampus.ch3.Engine' available: expected single matching bean but found 2: superEngine,turboEngine

Engine 클래스를  @Component 빈 등록하지 않음.
그러니 @autowired에서 engine은 찾지 못하고, superEngine, turboEngine 찾았으나 뭘 선택해야할지 몰라에러.

이럴땐 @Qualifier("superEngine") 이런식으로 결정해줘야함.


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


참고 :

남궁성, 스프링의 정석

+ Recent posts