IoC 와 DI

λ°•μš°ν˜„ (Joshua)Β·2023λ…„ 4μ›” 24일

ν•­ν•΄99_TIL

λͺ©λ‘ 보기
16/20

πŸ“ μ„œλ‘  (객체 κ°„μ˜ 의쑴 관계)

μ„œλ‘œ μƒν˜Έμž‘μš©ν•˜λŠ” κ°μ²΄μ—λŠ” 두가지 μ’…λ₯˜μ˜ 의쑴 관계가 μžˆλ‹€. 이 두가지 의쑴 κ΄€κ³„λŠ” κ°•ν•œ κ²°ν•© 와 λŠμŠ¨ν•œ κ²°ν•©/ μ•½ν•œ 결합이닀.

  • κ°•ν•œ κ²°ν•©
    = μ–΄λ–€ 객체가 μ‚¬μš©ν•˜λŠ” 의쑴 객체λ₯Ό 직접 λ§Œλ“€μ–΄ μ‚¬μš©ν•œλ‹€.
public class Controller1 {
	private final Service1 service1;

	public Controller1() {
		this.service1 = new Service1();
	}
}

λ³΄μ΄λŠ” 것과 같이 Controller1 κ°μ²΄λŠ” 생성될 λ•Œ Service1 객체λ₯Ό μΈμŠ€ν„΄μŠ€ν™”ν•΄μ„œ μ‚¬μš©ν•œλ‹€. μ—¬κΈ°μ„œ λ§Œμ•½ Controller1 객체의 μ—¬λŸ¬κ°€μ§€ λ©”μ„œλ“œλ“€μ΄ Servicee1 객체λ₯Ό μΈμŠ€ν„΄μŠ€ν™” ν•΄μ„œ μ‚¬μš©ν•œλ‹€λ©΄ μ½”λ“œμ˜ 쀄은 κΈΈμ–΄μ§€κ³ , Service1 객체의 변경사항이 생기면 고쳐야할 뢀뢄도 λ§Žμ„ 것이닀. 또 μ•Œ 수 μžˆλŠ” 점은 μ½”λ“œμ˜ ꡬ쑰둜 인해 Controller1 κ°μ²΄λŠ” Service1 객체에 μ˜μ‘΄μ„ ν•œλ‹€λŠ” 것이닀.
μœ„μ˜ 상황을 κ·ΉλŒ€ν™” μ‹œμΌœμ„œ κ·Έλ¦° λ‹€μ΄μ–΄κ·Έλž¨μ„ 보여주겠닀.

  • λŠμŠ¨ν•œ κ²°ν•©/ μ•½ν•œ κ²°ν•©
    = μœ„μ˜ κ°•ν•œ κ²°ν•©κ³ΌλŠ” λ°˜λŒ€λ‘œ 의쑴 객체λ₯Ό μ£Όμž… λ°›μ•„ μ‚¬μš©ν•œλ‹€.
Class Controller1 {
	private final Service1 service1;

	// service1 객체 μ‚¬μš©
	public Controller1(Service1 service1) {
		this.service1 = service1;
	}
}

λ³΄μ΄λŠ” 것과 같이 μœ„μ—μ™€λŠ” λ‹€λ₯΄κ²Œ Controller1 κ°μ²΄λŠ” 생성될 λ•Œ Servicee1 객체λ₯Ό μ£Όμž…λ°›μ•„ μ‚¬μš©ν•œλ‹€. μ΄λ ‡κ²Œ 되면, κ°•ν•œ 결합에 λΉ„ν•΄ μ½”λ“œμ˜ κΈΈμ΄λŠ” 쀄고, Service1 객체에 변경사항이 생기더라도, Controller1 κ°μ²΄μ—μ„œλŠ” 변경사항이 μ—†λ‹€λŠ” 것이닀. λ˜ν•œ, μœ„μ™€ λ°˜λŒ€λ‘œ Controller1 κ°μ²΄λŠ” Service1 객체에 μ˜μ‘΄μ„ ν•˜μ§€ μ•ŠλŠ”λ‹€λŠ” 것이닀.
이 상황을 μ •λ¦¬ν•œ λ‹€μ΄μ–΄κ·Έλž¨μ„ 보여주겠닀.

μ΄λ ‡κ²Œ 상황이 뒀바뀐것을 μ˜μ‘΄μ„ ν•΄μ•Όν•˜λŠ” 객체λ₯Ό μ£Όμž… λ°›λŠ” 것을 μ˜μ‘΄μ„± μ£Όμž… λ˜λŠ” DI라 ν•˜κ³ , ν”„λ‘œκ·Έλž¨μ˜ μ œμ–΄μ˜ 흐름이 바뀐 것을 μ œμ–΄μ˜ μ—­μ „ λ˜λŠ” IoC라 ν•œλ‹€.

πŸ“‹ DIλž€?

μœ„μ—μ„œ μ–ΈκΈ‰ν•œ Di ( Dependency Injection ) ν˜Ήμ€ μ˜μ‘΄μ„± μ£Όμž…μ€ μ‰½κ²Œ λ§ν•˜μžλ©΄ μš©λ„μ— 맞게 ν•„μš”ν•œ 객체λ₯Ό κ·Έλƒ₯ κ°€μ Έλ‹€ μ‚¬μš©ν•˜λŠ” 것이닀. 이 객체듀이 μ–΄λ–»κ²Œ λ§Œλ“€μ–΄μ‘ŒλŠ”μ§€λŠ” μ•Œ ν•„μš”κ°€ μ—†λŠ” 것이닀. 예λ₯Ό λ“€μ–΄ μš°λ¦¬κ°€ μ—¬λŸ¬κ°€μ§€ μ’…λ₯˜μ˜ μ˜·μ„ μž…λŠ” 것과 같이, ν•„μš”ν•  λ•Œ κ°€μ Έλ‹€κ°€ μ‚¬μš©ν•˜λŠ” 것이닀.
그럼 μ—¬κΈ°μ„œ μƒκ°λ‚˜λŠ” μ§ˆλ¬Έμ€ 객체λ₯Ό μ‚¬μš©ν•˜λ €λ©΄ 객체 생성이 μš°μ„  λ˜μ–΄μ•Ό ν•˜λŠ”λ°, μ–΄λ–»κ²Œ 객체 생성 없이 객체λ₯Ό μ‚¬μš©ν•  수 μžˆλŠ” 것이닀. 그것은 λ°”λ‘œ μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬κ°€ ν•„μš”ν•œ 객체λ₯Ό μƒμ„±ν•˜μ—¬ κ΄€λ¦¬ν•˜λŠ” 역할을 λŒ€μ‹ ν•΄μ€€λ‹€.μ—¬κΈ°μ„œ Bean κ³Ό IoC μ»¨ν…Œμ΄λ„ˆλΌλŠ” κ°œλ…μ΄ λ“±μž₯ν•œλ‹€.

πŸ“‹ Bean & IoC μ»¨ν…Œμ΄λ„ˆ

Bean = μŠ€ν”„λ§μ΄ κ΄€λ¦¬ν•˜λŠ” 객체이닀.
μŠ€ν”„λ§ IoC μ»¨ν…Œμ΄λ„ˆ = Bean 을 λͺ¨μ•„λ‘” 톡이닀.

  • Bean 등둝 방식
  1. 클래슀 μœ„μ— @Component μ–΄λ…Έν…Œμ΄μ…˜ 적어주기!
@Component
public class ProductService { ... }

μ €λ ‡κ²Œ 적어주면 μŠ€ν”„λ§ μ„œλ²„κ°€ 돌 λ•Œ μŠ€ν”„λ§ IoC μ»¨ν…Œμ΄λ„ˆμ— Bean을 생성해쀀닀

// 1. ProductService 객체 생성
ProductService productService = new ProductService();
// 2. μŠ€ν”„λ§ IoC μ»¨ν…Œμ΄λ„ˆμ— 빈 (productService) μ €μž₯
// productService -> μŠ€ν”„λ§ IoC μ»¨ν…Œμ΄λ„ˆ

μ΄λ•Œ μ €μž₯λ˜λŠ” Bean의 이름은 클래슀 μ΄λ¦„μ˜ μ²«κΈ€μžλ₯Ό μ†Œλ¬Έμžλ‘œ λ³€κ²½ν•˜μ—¬ μ €μž₯ν•œλ‹€.
밑에 μ‚¬μ§„μ²˜λŸΌ 클래슀 μ˜†μ— 컀피 콩 μ•„μ΄μ½˜μ΄ 있으면 κ·Έ ν΄λž˜μŠ€λŠ” 'bean' ν΄λž˜μŠ€λΌλŠ” λœ»μ΄λ‹€.

이 @Component μ–΄λ…Έν…Œμ΄μ…˜μ˜ 적용 λ²”μœ„λŠ” @ComponentScan 에 μ„€μ •ν•΄ μ€€ packages μœ„μΉ˜μ™€ ν•˜μœ„ packages 듀이닀.
2. 직접 객체λ₯Ό μƒμ„±ν•œ λ’€ μœ„μ— @Bean μ–΄λ…Έν…Œμ΄μ…˜ 적어주기!

...
@Configuration
public class BeanConfiguration {
    @Bean
    public ProductRepository productRepository() {
        String dbUrl = "jdbc:h2:mem:db";
        String username = "sa";
        String password = "";
        return new ProductRepository(dbUrl, username, password);
    }
}

이것도 μ €μž₯ 방법은 μœ„μ— 적힌 방식과 κ°™λ‹€.

  • μŠ€ν”„λ§ 'Bean' μ‚¬μš© 방법
  1. λ©€λ²„λ³€μˆ˜ μ„ μ–Έ μœ„μ— @Autowired 적어주기!
@Component
public class MyController {
    private final MyService myService;
    @Autowired
    public MyController(MyService myService) {
        this.myService = myService;
    }	
		// ...
}

μ΄λ ‡κ²Œ 적으면 μŠ€ν”„λ§μ— μ˜ν•΄ μžλ™μ μœΌλ‘œ DI λœλ‹€. ν•˜μ§€λ§Œ Spring 4.3 버젼 λΆ€ν„° @Autowired μƒλž΅ κ°€λŠ₯ν•˜λ‹€! 단, μƒμ„±μž 선언이 1개 일 λ•Œλ§Œμ΄λ‹€. λ˜ν•œ, Lombok의 @RequiredArgsConstructor κ°€ final 둜 μ„ μ–Έλœ 멀버 λ³€μˆ˜λ₯Ό μžλ™μœΌλ‘œ 생성해주기 λ•Œλ¬Έμ— μ΄λ ‡κ²Œ μƒλž΅μ΄ κ°€λŠ₯ν•˜λ‹€.

@RestController
@RequiredArgsConstructor // final둜 μ„ μ–Έλœ 멀버 λ³€μˆ˜λ₯Ό μžλ™μœΌλ‘œ 생성
public class MyController {
    private final MyService myService;
	// @Autowired
	// public ProductController(ProductService productService) {
	//     this.productService = productService;
	// }
}

좜처:
DI,IoC
κ²°ν•©

"Coffee Bean" β˜•
'23.04.17 - '23.04.22

profile
맀일 맀일 μ„±μž₯ν•˜μž

0개의 λŒ“κΈ€