1. ๊ฐ๋ ๐ฆ
DI(Dependency Injection = ์์กด์ฑ ์ฃผ์ = ์์กด๊ด๊ณ ์ฃผ์ )๋ฅผ ์๊ธฐ ์ํด์๋ ์ฐ์ IoC(Inversion of Control = ์ ์ด์ ์ญ์ )๋ฅผ ์์์ผ ํ๋ค.
IoC๋ ๋ฉ์๋๋ ๊ฐ์ฒด์ ํธ์ถ ์์ ์ ๊ฐ๋ฐ์๊ฐ ๊ฒฐ์ ํ๋ ๊ฒ์ด ์๋ ์ธ๋ถ์์ ๊ฒฐ์ ํ๋ ๊ฒ์ด๋ค. ์ฌ๊ธฐ์ ์ธ๋ถ๋ Spring Framework๋ฅผ ๋ปํ๋ค.
// ๊ธฐ์กด
public class DictionaryService {
KoreanDictionary koreanDictionary = new KoreanDictionary();
}
// IoC ์ ์ฉ
public class DictionaryService {
Dictionary dictionary;
}
๊ธฐ์กด์๋ ๊ฐ์ฒด๋ฅผ ํด๋์ค ๋ด๋ถ์์ ์์ฑํ๊ณ ์ฌ์ฉํ์ง๋ง IoC๋ฅผ ์ ์ฉํ๋ฉด ๋ฏธ๋ฆฌ ์์ฑํด ๋์ ๊ฐ์ฒด๋ฅผ ์ฃผ์ ๋ฐ์ ์ฌ์ฉํ๊ธฐ๋ง ํ๋ฉด ๋๋ค. ํด๋์ค ๋ ๋ฒจ์์ ์์กด๊ด๊ณ๊ฐ ์ค์ ๋๋ ๊ฒ ์๋๋ผ ๋ฐํ์ ์์ ๊ด๊ณ๊ฐ ๋์ ์ผ๋ก ์ฃผ์ ๋๋ ๊ฒ์ด๋ค. ์ด๋ฅผ ํตํด ๋ชจ๋ ๊ฐ์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ ์ ์ฐ์ฑ์ ๋์ผ ์ ์๋ค.
๊ทธ๋ฌ๋ฉด DI๋ ๋ฌด์์ผ๊น? DI๋ IoC์ ๊ตฌํ ๋ฐฉ๋ฒ ์ค ํ๋์ด๋ค. (IoC์ ์ ํ์๋ DI๋ง ์๋ ๊ฒ์ด ์๋๋ผ DL(Dependency Lookup)๋ ์กด์ฌํ๋ค.)
- DI - ๊ฐ ํด๋์ค ๊ฐ์ ์์กด์ฑ์ ์์ ์ด ์๋ ์ธ๋ถ(์ปจํ ์ด๋)์์ ์ฃผ์ ํ๋ ๊ฒ
- DL - Bean์ ์ ๊ทผํ๊ธฐ ์ํด ์ปจํ ์ด๋๊ฐ ์ ๊ณตํ๋ API๋ฅผ ์ด์ฉํ์ฌ Bean์ Lookup ํ๋ ๊ฒ
2. ์ปจํ ์ด๋(Container) ๐
์์์ DI๋ ๊ฐ ํด๋์ค ๊ฐ์ ์์กด์ฑ์ ์์ ์ด ์๋ ์ธ๋ถ(์ปจํ ์ด๋)์์ ์ฃผ์ ํ๋ ๊ฒ์ด๋ผ๊ณ ํ์๋ค. ๊ทธ๋ฌ๋ฉด ์ปจํ ์ด๋๊ฐ ๊ฐ์ฒด๋ค์ ๊ด๋ฆฌํ๊ณ ์ฃผ์ ํด์ค์ผ ํ๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
์ด ๋ถ๋ถ์ ์คํ๋ง ์ปจํ ์ด๋('IoC ์ปจํ ์ด๋' ๋๋ 'DI ์ปจํ ์ด๋'๋ผ๊ณ ๋ ๋ถ๋ฆผ)๊ฐ ๋ด๋นํด ์ค๋ค. ์คํ๋ง ์ปจํ ์ด๋๊ฐ ๊ด๋ฆฌํ๋ ๊ฐ์ฒด๋ฅผ ๋น(Bean)์ด๋ผ๊ณ ํ๊ณ , ๋น๋ค์ ์๋ช ์ฃผ๊ธฐ(Life-Cycle)๋ฅผ ๊ด๋ฆฌํ๋ ๋นํฉํ ๋ฆฌ(BeanFactory)๊ฐ ์๋ค.
๊ฐ์ฅ ์ค์ํ 2๊ฐ์ง ์ธํฐํ์ด์ค๋ BeanFactory์ ApplicationContext์ด๋ค. ApplicationContext๋ BeanFactory๋ฅผ ํ์ฅํ์ฌ ์ฌ๋ฌ ๊ฐ์ง ๊ธฐ๋ฅ์ ์ถ๊ฐํ ๊ฒ์ด๋ค.
BeanFactory๋ฅผ ์ง์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๋ ๊ฑฐ์ ์์ผ๋ฏ๋ก ์ผ๋ฐ์ ์ผ๋ก ApplicationContext๋ฅผ ์คํ๋ง ์ปจํ
์ด๋๋ผ๊ณ ํ๋ค.
3. DI(IoC) ๐
DI๋ฅผ ์ ์ฉ๋ ๋ชจ์ต์ ๊ธฐ์กด ์ฝ๋์์๋ถํฐ์ ๊ฐ์ ๊ณผ์ ์ ํตํด ์์๋ณด์. ์์ ๋ DictionaryService๋ผ๋ ๊ฒ์ด ์กด์ฌํ๊ณ showInformation()์ด๋ผ๋ ๋ฉ์๋๋ฅผ ํตํด ์์ ์ด ๊ฐ์ง๊ณ ์๋ dictionary์ ์ ๋ณด(KoreanDictionary ๋๋ EnglishDictionary)๋ฅผ ๊ฐ์ ธ์จ๋ค๊ณ ํ์.
1. ๊ฐ์ฒด ๊ฐ์ ๊ฐํ ๊ฒฐํฉ
public class DictionaryMain {
public static void main(String[] args) {
DictionaryService dictionaryService = new DictionaryService();
dictionaryService.showInformation();
}
}
----------
public class DictionaryService {
KoreanDictionary koreanDictionary = new KoreanDictionary();
// EnglishDictionary englishDictionary = new EnglishDictionary();
public void showInformation(){
System.out.println(koreanDictionary.info());
// System.out.println(englishDictionary.info());
}
}
----------
public class KoreanDictionary {
public String info(){
return "ํ๊ตญ์ด ์ฌ์ ์
๋๋ค.";
}
}
----------
public class EnglishDictionary {
public String info(){
return "This is an English dictionary.";
}
}
์ฝ๋๋ฅผ ํตํด ์ ์ ์๋ฏ์ด ๋ง์ฝ ํ๊ตญ์ด ์ฌ์ (KoreanDictionary)์์ ์์ด ์ฌ์ (EnglishDictionary)๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ํ์ ๊ณผ ์์ฑ์๋ฅผ ๋ฐ๊ฟ์ ์ฌ์ฉํด์ผ ํ๋ค.
2. ์ธํฐํ์ด์ค ์ด์ฉ
public class DictionaryMain {
public static void main(String[] args) {
DictionaryService dictionaryService = new DictionaryService();
dictionaryService.showInformation();
}
}
----------
public class DictionaryService {
Dictionary dictionary = new KoreanDictionary();
// Dictionary dictionary = new EnglishDictionary();
public void showInformation(){
System.out.println(dictionary.info());
// System.out.println(dictionary.info());
}
}
----------
public interface Dictionary {
public String info();
}
----------
public class KoreanDictionary implements Dictionary{
@Override
public String info(){
return "ํ๊ตญ์ด ์ฌ์ ์
๋๋ค.";
}
}
----------
public class EnglishDictionary implements Dictionary {
@Override
public String info(){
return "This is an English dictionary.";
}
}
์ธํฐํ์ด์ค๋ฅผ ์ด์ฉํ๋ค๋ฉด ํ์ ์ ๋์ผํ๊ฒ ํด์ ์ฌ์ฉํ ์ ์๋ค.
ํ์ง๋ง ์ฌ์ ํ ๋ฌธ์ ๊ฐ ๋ง๋ค. ๊ทธ๋ฆผ์ ํตํ์ฌ ์ ์ ์๋ฏ์ด ๊ธฐ๋ํ๋ ์์กด๊ด๊ณ์ ๋ฌ๋ฆฌ ์ค์ ์์กด๊ด๊ณ๋ ๊ตฌ์ฒด์ ์ธ ํด๋์ค์ ์์กดํ๋ ๊ฒ์ ์ ์ ์๋ค. ๊ทธ๋์ ์์ง ์๋์ ๊ฐ์ ๋ฌธ์ ์ ์ด ๋จ์์๋ค.
- DIP ์๋ฐ
DictionaryService๊ฐ Dictionary ์ธํฐํ์ด์ค๋ฟ๋ง ์๋๋ผ KoreanDictionary์ธ ๊ตฌ์ฒด ํด๋์ค์๋ ํจ๊ป ์์กดํ๊ณ ์๋ค - OCP ์๋ฐ
KoreadnDictionary๊ฐ ์๋ EnglishDictionary๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด DictionaryMain์ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํด์ผ ํ๋ค.
3. AppConfig ์ด์ฉ (์ต์ข ์์ฑ)
@Configuration
public class AppConfig {
@Bean
public DictionaryService dictionaryService() {
return new DictionaryService(dictionary());
}
@Bean
public Dictionary dictionary() {
return new KoreanDictionary();
}
}
----------
public class DictionaryMain {
public static void main(String[] args) {
// ์คํ๋ง ์ปจํ
์ด๋ ์์ฑ
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
DictionaryService dictionaryService = applicationContext.getBean("dictionaryService", DictionaryService.class);
dictionaryService.showInformation();
}
}
----------
public class DictionaryService {
Dictionary dictionary;
public DictionaryService(Dictionary dictionary) {
this.dictionary = dictionary;
}
public void showInformation(){
System.out.println(dictionary.info());
}
}
----------
public interface Dictionary {
public String info();
}
----------
public class KoreanDictionary implements Dictionary {
@Override
public String info(){
return "ํ๊ตญ์ด ์ฌ์ ์
๋๋ค.";
}
}
----------
public class EnglishDictionary implements Dictionary {
public String info(){
return "This is an English dictionary.";
}
}
์คํ๋ง ์ปจํ ์ด๋์ ๊ฐ์ฒด๋ฅผ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋กํ๊ณ , ์คํ๋ง ์ปจํ ์ด๋์์ ์คํ๋ง ๋น์ ์ฐพ์์ ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝํ์๋ค.
4. ์ปดํฌ๋ํธ ์ค์บ & ์์กด๊ด๊ณ ์ฃผ์ ๐
์์ ์์ ์์๋ ๊ฐ์ฒด๊ฐ ๋ช ๊ฐ ์ ๋์ด์ ๋ฑ๋กํ๋๋ฐ ๋ฌธ์ ๊ฐ ์์ด ๋ณด์ธ๋ค. ํ์ง๋ง ์คํ๋ง ๋น์ด ์์ญ, ์๋ฐฑ ๊ฐ๊ฐ ๋๋ฉด ์ผ์ผ์ด ๋ฑ๋กํ๊ธฐ๋ ์ด๋ ต๊ณ , ์ค์ ์ ๋ณด๋ ์ปค์ง๊ณ , ๋๋ฝํ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค. ๊ทธ๋์ ์คํ๋ง์ ์ค์ ์ ๋ณด๊ฐ ์์ด๋ ์๋์ผ๋ก ์คํ๋ง ๋น์ ๋ฑ๋กํ๋ ์ปดํฌ๋ํธ ์ค์บ์ด๋ผ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
AppConfig์ @ComponentScan์ ๋ถ์ด๊ณ , ๋น์ผ๋ก ๋ฑ๋กํ ๊ฐ์ฒด์ @Component๋ผ๊ณ ๋ถ์ฌ์ฃผ๋ฉด ๋๋ค. ๊ทธ๋ฆฌ๊ณ KoreanDictionary์ EnglishDictionary ๋ ๊ตฐ๋ฐ์ ๋ชจ๋ @Component๋ฅผ ๋ถ์ด๋ฉด ๋น์ฐํ ์ถฉ๋์ด ์ผ์ด๋๋๋ฐ ์ฐ์ ์์๋ก ์ฌ์ฉํ ๊ณณ์ @Primary๋ฅผ ๋ถ์ด๋ฉด ๋๋ค. (@Qualifier๋ฅผ ์ด์ฉํ ์๋ ์๋๋ฐ ์ถ๊ฐ์ ์ธ ์ค๋ช ์ ์๋ตํ๋ค.)
๋ํ, ์์กด๊ด๊ณ๋ @Autowired๋ฅผ ์ด์ฉํ์ฌ ์๋์ผ๋ก ์ฃผ์ ํ ์ ์๋ค (์์กด๊ด๊ณ ์ฃผ์ ๊ณผ ๊ด๋ จํด์๋ ๋ค์ ๊ธ์ ์ฐธ๊ณ ํ๊ธฐ๋ฅผ ๋ฐ๋๋ค.)
@Configuration
@ComponentScan
public class AppConfig {
}
----------
public class DictionaryMain {
public static void main(String[] args) {
// ์คํ๋ง ์ปจํ
์ด๋ ์์ฑ
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
DictionaryService dictionaryService = applicationContext.getBean(DictionaryService.class);
dictionaryService.showInformation();
}
}
----------
@Component
public class DictionaryService {
Dictionary dictionary;
@Autowired
public DictionaryService(Dictionary dictionary) {
this.dictionary = dictionary;
}
public void showInformation(){
System.out.println(dictionary.info());
}
}
public interface Dictionary {
public String info();
}
----------
@Component
@Primary
public class KoreanDictionary implements Dictionary {
@Override
public String info(){
return "ํ๊ตญ์ด ์ฌ์ ์
๋๋ค.";
}
}
----------
@Component
public class EnglishDictionary implements Dictionary {
public String info(){
return "This is an English dictionary.";
}
}
ํด๋น ๊ธ์ ๊น์ํ ๋์ '์คํ๋ง ํต์ฌ ์๋ฆฌ - ๊ธฐ๋ณธํธ'์ ์ฐธ๊ณ ํ์์ต๋๋ค.
'๐ ์คํ๋ง > ๊ธฐ๋ณธ ๊ฐ๋ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
HTTP ์์ฒญ(ํค๋, ํ๋ผ๋ฏธํฐ, ๋ฉ์์ง) ์ฒ๋ฆฌ (0) | 2023.04.20 |
---|---|
์คํ๋ง MVC (0) | 2023.04.19 |
๋ก๊น (SLF4J, Logback) (0) | 2023.04.09 |
์์ฑ์ ์ฃผ์ ์ ์ ํํ๋ผ! (0) | 2023.04.07 |
์์กด๊ด๊ณ ์ฃผ์ ๋ฐฉ๋ฒ (0) | 2023.04.06 |