Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
Tags
- nginx
- CI/CD
- 리액트
- Jenkins
- spring
- Spring Boot
- Docker
- 리눅스
- docker network
- MongoDB
- jenkins github
- jenkins github 연동
- Jenkins Pipeline
- jenkins 설치
- vue.js
- grafana
- Linux
- grpc
- subnetmask
- java
- REACT
- jenkins install
- jpa
- error
- IntelliJ
- JavaScript
- gradle
- jenkins jdk
- jenkins maven
- MySQL
Archives
- Today
- Total
뭐든 즐기면서 ;)
DI(Dependency Injection) 의존성 주입 본문
728x90
* DI를 설명하기 위해서는 의존성과 주입을 따로 설명해야 할 것같습니다.
일단, DI란 객체지향주의 프로그래밍을 하기 위한 구현 개념 중 하나입니다.
※ 프로그래밍에서의 의존성
● A객체가 B객체를 사용할 때 의존성이 있음을 뜻한다.
● A가 B에 의존한다.
● B가 수정되면, A에도 영향을 미친다.
※ 의존성 주입이란
● A가 B객체를 사용할 때 A객체 내에서 생성하는 것이 아닌,
B를 A외부에서 생성하여 A생성자의 파라미터로 넘겨 받아 변수에 대입하거나
setter를 통해 대입하는 것을 뜻합니다.
(=A 외부에서 생성하여 주입시킨다.)
● A와 B객체 사이에 인터페이스를 둠으로써 강한 결합이 아닌, 유연한 결합이 가능하도록 합니다.
이해를 돕기 위한 상황 가정
SSD가 등장하기 전, HDD로 만든 컴퓨터를 SSD로 교체한다고 가정합니다.
1. 부품 하나 하나를 납땜한 완제품 컴퓨터가 있습니다. 이는 부품 교체 시 뜯어내야 할 것입니다.
(의존성이 강한 프로그래밍 = 의존성 주입이 없는 프로그래밍)
2. 조립식 컴퓨터가 있다고 가정합니다. 부품을 따로 만들어놓고 끼워넣기만 하면 됩니다.
(의존성이 약한 프로그래밍 = 의존성 주입)
1. 의존성 주입이 없는 프로그래밍 (1번 상황)
- HDD로 만든 컴퓨터
// Sample 컴파일 , Computer 컴파일 , Hdd 컴파일
public class Hdd() {}
public class Computer() {
private Hdd hdd;
public Computer() {
this.hdd = new Hdd(); // 클래스 내에서 객체 생성(부품 납땜)
}
}
public class Sample() {
public static void main(String args[]) {
Computer com = new Computer();
}
}
- SSD로 교체
// Sample 컴파일 , Computer 컴파일 , Ssd 컴파일
public class Ssd() {}
public class Computer() {
private Ssd Ssd;
public Computer() {
this.Ssd = new Ssd(); // 납땜한 Hdd를 떼어내고 Ssd를 납땜
}
}
public class Sample() {
public static void main(String args[]) {
Computer com = new Computer();
}
}
2. 의존성 주입 프로그래밍 (2번 상황)
= 의존 관계를 분리하여 외부에서 주입을 받는 형식의 프로그래밍(부품만 교체)
- HDD로 만든 컴퓨터
// Sample 컴파일 , Hdd 컴파일 , Computer 컴파일
// 먼저 컴파일된 Hdd를 Computer에 주입시킵니다.
public class Hdd() {}
public class Computer() {
private Hdd hdd;
public Computer(Hdd hdd) {
this.hdd = hdd;
}
}
public class Sample() {
public static void main(String args[]) {
Hdd hdd = new Hdd(); // 클래스 외부에서 객체 생성
Computer com = new Computer(hdd); // 주입 ( = 의존성 주입)
}
}
- SSD로 교체
// Sample 컴파일 , Ssd 컴파일 , Computer 컴파일
// 먼저 컴파일된 Hdd를 Computer에 주입시킵니다.
public class Ssd() {}
public class Computer() {
private Ssd ssd;
public Computer(Ssd ssd) {
this.ssd = ssd;
}
}
public class Sample() {
public static void main(String args[]) {
Ssd ssd = new ssd(); // 클래스 외부에서 객체 생성
Computer com = new Computer(ssd); // 주입 ( = 의존성 주입)
}
}
여기까지만 해도 DI(Dependency Injection) 의존성 주입 프로그램이라고 할 수 있습니다. 말그대로 주입을 했기 때문입니다. 다만, 여기까지만 보면 결국 Hdd와 Ssd를 한 번씩 생성해야 하고, Computer내에서도 코드 수정이 이루어지기 때문에 주입하기 전과 크게 다를 게 없다고 생각될 것입니다. 그래서 interface를 필요로 하게 됩니다. interface를 두고, setter를 통하여 좀 더 유연하게(낮은 의존도=낮은 결합도) 온전한 DI적 프로그래밍을 했다고 볼 수 있습니다.
Interface 생성 후
interface AuxiliaryStorage() {}
class Hdd implements AuxiliaryStorage() {}
class Sdd implements AuxiliaryStorage() {}
public class Computer() {
private AuxiliaryStorage as;
public Computer() {
}
public void setAs(AuxiliaryStorage as) {
this.as = as;
}
public AuxiliaryStorage getAs() {
return as;
}
}
public class Sample() {
public static void main(String args[]) {
AuxiliaryStorage as = new Hdd();
// Hdd 부품 삽입
Computer com = new Computer();
com.setAs(as);
// Ssd로 교체하려면 interface의 실구현체만 바꾸어 새롭게 주입하면 된다.
as = new Ssd();
com.setAs(as);
}
}
IoC에 대한 포스팅 : https://tadaiswhatever.tistory.com/69
728x90
'BACK > Spring' 카테고리의 다른 글
Error : Caused by: java.io.FileNotFoundException: class path resource [io/] cannot be resolved to URL because it does not exist (0) | 2024.02.08 |
---|---|
Spring bean xml 파일 설정 (0) | 2023.01.18 |
IoC(Inversion of Control) (0) | 2022.05.11 |
Comments