Constructor Injection 은 μœ„μ—μ„œ μ„€λͺ…ν•œλŒ€λ‘œ, constructor λ₯Ό μ΄μš©ν•˜λŠ” Injection 방식이닀.

μš°μ„  μœ„μ˜ μ‹œλ‚˜λ¦¬μ˜€λ₯Ό μ μš©ν•΄λ³΄κΈ°λ‘œ ν•˜μž. μš°μ„  /dailyworkout endpoint 에 μ ‘κ·Όν•˜λ©΄, DemoController λΌλŠ” controller λ₯Ό ν†΅ν•˜μ—¬ Coach object 의 getDailyWorkout method λ₯Ό μ‹€ν–‰ν•˜λ„λ‘ ν•˜λŠ” 방식이닀.

Coach object λŠ” Dependency Injection 을 μœ„ν•˜μ—¬ Interface 둜 λ§Œλ“€κ³ , 이λ₯Ό 각각의 class κ°€ implements ν•˜λŠ” μ‹μœΌλ‘œ κ΅¬μ„±ν•˜λ©΄ DIP(Dependency Inversion Principle) 도 λ§Œμ‘±ν•  것이닀.

Constructor Injection - Code
@RestController  
public class DemoController {  
  
    // define a private field for the dependency  
    private Coach myCoach;  
  
    // define a constructor for dependency injection  
    @Autowired  
    public DemoController(Coach theCoach) {  
        myCoach = theCoach;  
    }  
  
    @GetMapping("/dailyworkout")  
    public String getDailyWorkout() {  
        return myCoach.getDailyWorkout();  
    }  
}

μš°μ„  FootballCoach class λ§Œμ„ @Component annotation 을 ν†΅ν•˜μ—¬ Spring Bean 으둜 μ„€μ •ν•˜μ—¬ Spring Container 에 μ €μž₯ν•œλ‹€. 이후 Controller μ—μ„œλŠ” Autowiring 을 ν†΅ν•˜μ—¬ λ“€μ–΄μ˜¨ FootballCoach κ°€ Controller 의 constructor 에 injection λœλ‹€.

즉, Constructor Injection 이 μˆ˜ν–‰λœ 것이닀.

Constructor Injection - Behind the Scenes

DI(Dependency Injection) 을 ν†΅ν•˜μ—¬ object κ°€ injection 된 것은 μ•Œκ² λ‹€. ν•˜μ§€λ§Œ μ•„μ£Ό 잘 μ•Œκ³  μžˆλ“―μ΄, Java μ—μ„œλŠ” λ‹¨μˆœνžˆ class type 의 field λ₯Ό μ„ μ–Έν•œλ‹€κ³  ν•΄μ„œ ν•΄λ‹Ή class 의 object κ°€ μƒκΈ°λŠ” 것은 μ•„λ‹ˆλ‹€. μ½”λ“œμƒμ—μ„œλŠ” theCoach λ‚˜ DemoController λ‹¨μˆœνžˆ μ„ μ–Έλ§Œ ν–ˆμ„ 뿐 μ‹€μ œ object λ₯Ό μƒμ„±ν•˜λŠ” 뢀뢄은 보이지 μ•ŠλŠ”λ‹€.

이런 역할을 λ‚΄λΆ€μ μœΌλ‘œ μˆ˜ν–‰ν•˜λŠ” 것이 λ°”λ‘œ Spring IoC Container, μœ„μ—μ„œ μ–ΈκΈ‰ν•œ Spring Container 이닀. Spring Container λŠ” Spring Container λΆ€λΆ„μ—μ„œ μ–ΈκΈ‰ν•œ κ²ƒμ²˜λŸΌ object λ₯Ό λ§Œλ“€κ³ , dependency λ₯Ό injection ν•˜λŠ” 역할을 μˆ˜ν–‰ν•œλ‹€. λ”°λΌμ„œ Spring IoC Container λŠ” λ‚΄λΆ€μ μœΌλ‘œ μ•„λž˜μ˜ μ½”λ“œλ₯Ό μˆ˜ν–‰ν•˜λŠ” 것이닀.

Coach theCoach = new FootballCoach();
DemoController demoController = new DemoController(theCoach);

λ‹€μ‹œ ν•œλ²ˆ μ„€λͺ…ν•˜λ©΄, 첫 번째 codeline μ—μ„œλŠ” Spring Bean 으둜 λ“±λ‘λœ FootballCoach class 에 λŒ€ν•œ μ‹€μ œ theCoach λΌλŠ” Coach type 의 object λ₯Ό μ„ μ–Έν•œ 것이고, 두 번째 codeline μ—μ„œλŠ” theCoach object λ₯Ό injection ν•˜μ—¬ Constructor λ₯Ό ν†΅ν•œ initialization, 즉 Constructor Injection 을 μˆ˜ν–‰ν•˜μ˜€λ‹€.

ν•˜μ§€λ§Œ, 이런 생각을 λ– μ˜¬λ¦΄ 수 μžˆλ‹€.

μ•„λ‹ˆ, κ·Έλƒ₯ β€œnew” keyword λ₯Ό μ‚¬μš©ν•΄μ„œ object λ₯Ό μƒμ„±ν•˜λ©΄ λ˜λŠ”κ±° μ•„λ‹ˆμ•Ό? μ™œ λ³΅μž‘ν•˜κ²Œ Spring Container μ—κ²Œ 이 μž‘μ—…μ„ μˆ˜ν–‰ν•˜κ²Œ ν•˜λŠ”κ±°μ•Ό.

Why using Spring IoC Container

λ¬Όλ‘ , μž‘μ€ application 에 λŒ€ν•˜μ—¬ Spring Container λ₯Ό ν†΅ν•˜μ—¬ IoC λ‚˜ DI 의 이점을 잘 λŠλΌμ§€ λͺ»ν•  것이닀. ν•˜μ§€λ§Œ Spring 은 λ³΅μž‘ν•œ application 은 λ¬Όλ‘ , κΈ°μ—…, real-time / real-world application 을 λŒ€μƒμœΌλ‘œ ν•œλ‹€. λ˜ν•œ Database 에 λŒ€ν•œ μ ‘κ·Ό, Transacations, REST APIs, Web MVC, Spring Security λ“±μ—μ„œ μ•„μ£Ό 효율적으둜 μ‚¬μš©λœλ‹€.

λΌˆμ™€ μ‚΄λ‘œ μ–»λŠ” 것은 μ•žμœΌλ‘œ 더 λŠκ»΄λ³΄λ„λ‘ ν•˜μž.