Dependency Injection
μ°μ Dependency Injection μ΄λΌκ³ νλ κ²μ DIP(Dependecy Inversion Principle) λ₯Ό κΈ°λ°μΌλ‘ νλ€.

μμμ λ³Έ μμμ²λΌ, Spring Container μμ μμ±λκ³ κ΄λ¦¬λλ object λ€μ κ°κ°μ νμμ λλ€λ₯Έ dependency κ΄κ³κ° μ‘΄μ¬ν κ²μ΄λ€. μλ₯Ό λ€μ΄μ, FootballCoach λ μμ μ½μΉλ₯Ό, μμ μ½μΉλ λ§λ΄ μ½μΉλ₯Ό νμλ‘ νκΈ° λλ¬Έμ΄λ€.
μ΄μ λνμ¬ Member class λ Spring Container λ‘λΆν° FootballCoach object λ₯Ό μꡬν λ, FootballCoach μ νμμ μ‘΄μ¬νλ λͺ¨λ dependency λ₯Ό νλ²μ μ»κΈ°λ₯Ό μν κ²μ΄λ€.
λ°λΌμ Dependecy Injection μ΄λ, μ΄λ κ² κ°μ²΄λ₯Ό μνλ Member μ μμ μΌλ‘ 보μμ λ, object κ°μ dependecy λ₯Ό μΈλΆμμ κ²°μ νκ³ μμ μκ² μ£Όμ
ν΄μ£Όλ μ€κ³ ν¨ν΄μ μλ―Ένλ κ²μ΄λ€. (μ€μ λ‘λ μμ μκ² μ€μ€λ‘ μ£Όμ
νλ κ²μ΄ μλλΌ Spring Container κ° μ΄λ₯Ό μννλ€.)
Injection Types
Spring μμλ μ¬λ¬κ°μ§ λ°©λ²μΌλ‘ Injection μ μ 곡νκ³ μλλ°, λνμ μΌλ‘ λ€μ λ κ°μ§μ λ°©λ²μ΄ μλ€.
- Constructor Injection
- Settter Injection
Constructor Injection μ constructor λ₯Ό μ΄μ©νλ λ°©μμΌλ‘, object μμ± μμ μ λ°λμ μ€μ λλ―λ‘ νμμ μΈ dependency μ μ²λ¦¬ν λ μ ν©νλ€.
Setter Injection μ dependency λ₯Ό μ νμ μΌλ‘ μ€μ ν μ μμΌλ―λ‘ optional dependency λ₯Ό μ²λ¦¬ν λ μ ν©νλ€.
Spring Autowiring
dependency injection μ μν΄μ Spring μ Autowiring μ΄λΌλ κ²μ μ¬μ©ν μ μλ€.
Autowiring μ Dependency Injection μ μλμΌλ‘ μνν΄μ£Όλ©°, @Autowired annotation μ μ¬μ©νμ¬ Spring Container μ λ±λ‘λ Bean μ€μμ type μ΄ μΌμΉνλ object λ₯Ό μλμΌλ‘ μ°Ύμμ inject ν΄μ€λ€.
μ¬κΈ°μ Bean μ Spring Container μ μν΄ κ΄λ¦¬λλ object λ‘, κ°λ°μκ° κ΅³μ΄ XML νμΌμ΄λ Java Source Code μμ μ€μ νμ§ μλλΌλ class μ @Component annotation λ§ μΆκ°ν¨μΌλ‘μ¨ Spring Container μ Bean μΌλ‘ λ±λ‘ν μ μλ€.