티스토리 뷰
DI란
OOP 프로그램의 방법론, 외부에서 생성한 객체를 주입하는 것
모듈화를 통해 프로그램의 재사용성을 높이고 유지보수성을 향상시킨다.
방법
-
생성자를 이용한 의존 객체를 주입
- 생성자에서 한 번 정한 후 변경이 불가능
public class Car(){ private Wheel wheel; public Car(){ wheel = new CarWheel(); } }
-
setter를 이용한 의존 객체 주입
- 값을 변경 할 수 있다
public class Car(){ private Wheel wheel; public Car(){ } public void setWheel(Wheel wheel){ this.wheel = wheel; } }
-
생성자와 setter를 이용한 의존 객체 주입
-
가장 많이 쓰는 방법
-
값을 변경 할 수 있다
-
사용자가 원할 때 객체를 주입 할 수 있다
public class Car(){ private Wheel wheel; public Car(Wheel wheel){ this.wheel = wheel; } public void setWheel(Wheel wheel){ this.wheel = wheel; } }
-
Spring에서의 적용
- 실제 사용
public class Car{
GenericApplicationContext ctx = new GenericApplicationContext("classpath:applicationContext.xml");
CarRegisterService registerService = ctx.getBean("registerService", CarRegisterService.class);
CarMaintenanceService maintenanceService = ctx.getBean("maintenanceService", CarMaintenanceService.class);
}
-
생성자에 주입
-
자바 코드
public CarRegisterService(CarDao carDao){ this.carDao = carDao; } public CarMaintenanceService(CarDao carDao){ this.carDao = carDao; }
-
위의 자바코드를 변환한 applicationContext.xml
<bean id="carDao" class="...CarDao"></bean> // 여러 객체가 carDao 공유 <bean id="registerService" class="....CarRegisterService"> <constructor-arg ref="carDao"></constructor> </bean> <bean id="maintenanceService" class="...CarMaintenanceService"> <constructor-arg ref="carDao"></constructor> </bean>
-
-
setter에 주입
-
자바 코드
public void setJdbcUrl(String jdbcUrl){ this.jdbcUrl = jdbcUrl; } public void setUserId(String userId){ this.userId = userId; } public void setUserPw(Sting userPw){ this.userPw = userPw; }
-
위의 자바코드를 변환한 applicationContext.xml
<bean id="dbConnectionInfo" class="....dbConnectionInfo"> <property name="jdbcUrl" value="jdbc:...."/> <property name="userId" value="값"/> <property name="userPw" value="값"/> </bean>
-
-
List 타입 의존객체 주입
-
자바 코드
public void setCars(List<String> cars){ this.cars = cars; }
-
위의 자바코드를 변환한 applicationContext.xml
<property name="cars"> <list> <value>truck</value> <value>suv</value> <value>sedan</value> </list> </property>
-
-
Map 타입 의존객체 주입
-
자바 코드
public void setAdmin(Map<String, String> admins){ this.admins = admins; }
-
위의 자바코드를 변환한 applicationContext.xml
<property name="admins"> <map> <entry> <key> <value>A</value> </key> <value>A@xxx.org</value> </entry> <entry> <key> <value>B</value> </key> <value>B@xxx.org</value> </entry> </map> </property>
-
[참고]
인프런 - 자바 스프링 프레임워크
'웹 프로그래밍 > Spring' 카테고리의 다른 글
[Tomcat] 콘솔 창 한글 깨짐 해결 (0) | 2020.01.07 |
---|---|
[Spring] IntelliJ서 Maven 사용하기 (0) | 2019.12.23 |
[Java] BufferedReader, BufferedWriter을 이용한 입출력 (0) | 2019.12.22 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- ESP8266
- 자바
- dovecot
- 구슬 탈출2
- hackerrank
- git
- 워드프레스
- 스티커모으기2
- 리눅스
- 라즈비안
- mysql
- dht11
- DP
- 2981
- 집배원 한상덕
- c++
- hc-06
- 블루투스
- FTP
- 프로그래머스
- 11503
- 아두이노
- the pads
- 메일서버
- 키 순서
- 백준
- 라즈베리파이
- BFS
- java
- 합승 택시 요금
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함