1) DI 란?
Dependency Injection
의존 주입
개발자 -> Spring 컨테이너(Spring 프레임워크)
1. 객체를 생성 및 관리(호출시 해당 객체 메서드 수행)
2. 의존 주입
의존성이란?
어떤 메서드를 수행할때에
다른 객체를 미리 new해야하는 상황
1.생성자 주입(Constructor Injection)
private AppleWatch appleWatch;//의존 관계
public IPhone() {//의존 주입
appleWatch=new AppleWatch();
}
new를 없에야하기 때문에
외부에서 받아온다
public IPhone(AppleWatch appleWatch) {//의존 주입 생성자 주입
this.appleWatch=appleWatch;
System.out.println("생성자를 통해 의존 주입");
}
생성자에 인자가 생기면 강제성이 높아짐
applicationContext.xml
<beans>
<bean class="test.IPhone">
<constructor-args ref="aw(객체명)"/> --생성자 주입태그
</bean>
<bean class="test.AppleWatch" id="aw"/>
</beans>
Q. 의존성 주입이 애플워치만 되는데 갤럭시워치도 가능하게 해주세요
워치류면 다 들어가야함
워치타입을 정의하려함 -인터페이스
주입되는 워치에따라 다른 결과가 나오게됨
applicationContext.xml
<beans>
new 순서의 상관없이 해줌
<bean class="test.IPhone">
<constructor-args ref="gw(객체명)"/> --생성자 주입태그
</bean>
<bean class="test.AppleWatch" id="aw"/>
<bean class="test.GalaxyWatch" id="gw"/>
</beans>
<bean class="test.AppleWatch" id="aw"/>
사용여부와 무관하게 메모리에 적재되어있음
서로 교체가 가능하게
먼저 인터페이스를 만든다
무엇이든 들어갈수 있도록 인자 맞춰줍니다
<bean class="test.AppleWatch" id="aw" lazy-init="true"/>
<bean class="test.GalaxyWatch" id="gw" lazy-init="true"/>
사용안하는데 new되어있으면 매모리 낭비
그럴때 사용할 수 있는 속성 : 지연로딩 방식
lazy-init="true" 도구구나 의존관계구나
Q. 갤럭시폰에 의존관계인 워치가 있다고 합니다
워치에 갤럭시 워치를 주입해서 사용할 수 있게 해주세요
applicationContext.xml
<beans>
<bean class="test.IPhone">
<constructor-args ref="gw(객체명)"/> --생성자 주입태그
</bean>
<bean class="test.GalaxyPhone">
<constructor-args ref="gw(객체명)"/> --생성자 주입태그
</bean>
<bean class="test.AppleWatch" id="aw"/>
<bean class="test.GalaxyWatch" id="gw"/>
</beans>
gw 햇깔려할까봐 줄임 하지만 줄이면 안됨 => galaxyWatch 변경
-번외-
빈순서 스프링 컨테이너가 알아서 처리해줌 처음에 슥 읽고 처리
다른 객체를 쓰려면 new를 해야하는 데 new를 안하고 어떻게 쓸것인가
1.생성자 주입 => 반드시 생성자를 지나야하는데 그래서 "강제성"이 짙음
2번 방식보단 생성자 주입 할때에 에러가 발생하면 해당 객체는 생성x ==필수적일때에 사용하는 방식
ex) 게임 캐릭터 만듦!
무기가 없을 수도 있잖아요
==> 2번방식으로 주입
카트라이터 캐릭터
자동차없이는 캐릭터도 없다!
==> 1번 방식으로 주입
2. Setter 주입 (Setter Injection)
1번 방식보다 덜 강제적입
기본 생성자 + setter를 활용하므로 기본생성자를 반드시 필요로함
웹에서 더 많이 사용함
applicationContext.xml
<beans>
<bean class="test.IPhone">
<property name="watch" ref="gw(객체명)"/>-- setter 주입태그
<property name="userName" value="홍길동"/>
</bean>
<bean class="test.AppleWatch" id="aw"/>
<bean class="test.GalaxyWatch" id="gw"/>
</beans>
같은것
property
속성
필드
애트리뷰트
맴버변수
applicationContext.xml
<beans>
<bean class="test.IPhone">
<property name="watch" ref="gw(객체명)"/>-- setter 주입태그
<property name="userName" value="홍길동"/>
</bean>
<bean class="test.AppleWatch" id="aw"/>
<bean class="test.GalaxyWatch" id="gw"/>
<bean class="test.GalaxyPhone">
<property name="watch" ref="gw(객체명)"/>
</beans>
컬랙선을 사용할때 해당 컬랙션 태그를 사용한다
applicationContext.xml
<beans>
<bean class="test.TestBean">
<property name="testList">
<list>
<value>홍길동</value>
<value>임꺽정</value>
<value>아무무</value>
</list>
</property>
<property name="testMap">
<map>
<entry>
<key><value>홍길동</value></key>
<value>미드</value>
</entry>
<entry>
<key><value>임꺽정</value></key>
<value>원딜</value>
</entry>
<entry>
<key><value>아무무</value></key>
<value>정글</value>
</entry>
</map>
</property>
</bean>
</beans>
'Spring' 카테고리의 다른 글
스프링 프레임워크 컨테이너 (0) | 2024.03.03 |
---|---|
Spring 파일생성 (0) | 2024.03.02 |
Spring 설치 (0) | 2024.02.29 |
<bean> (1) | 2024.02.26 |
컨테이너 (0) | 2024.02.25 |