κ³Όμ λ₯Ό νλ©΄μ, μμ£Ό μ’μ μ¬μ΄νΈλ₯Ό νλ μκ² λμλ€. κ΅μλκ»μ μκ°νμ λ―Έλμ! λ€μν λΆμΌμ μ λ¬Έκ°λ€μ΄ μ΄ κΈμ λ³Ό μ μλ μΌμ’ μ λΈλ‘κ·Έ, λ―Έλμ(meduim.com)μ΄λ€. λ¬Όλ‘ ! λλΆλΆμ΄ μμ΄λ‘ μμ±λμ΄ μλ€.
λλ¦ ν΄μν΄λ³΄μκ³ , κ·Έ λ΄μ©μ΄ μ μ΄μ μμ μ μ΄ν΄νκ³ μ€νλ§νλ μμν¬κ° μ 곡νλ μμ‘΄μ± μ£Όμ μ λνμ¬ μ λ°μ μΈ μ΄ν΄λ₯Ό λκΈ° μ’μ κ² κ°μ λ²μμ 곡μ νλ € νλ€.
Dependency Injection: Concept of Spring Framework (μμ‘΄μ± μ£Όμ : μ€νλ§ νλ μ μν¬μ κ°λ )μ μ λͺ©μΌλ‘ μ΄ λ―Έλμμ κΈ,
(medium.com/analytics-vidhya/dependency-injection-concept-of-spring-framework-d9c3688005f8)
μ΄ μλ³Έ κΈμ΄λ€. λμ μ£Όκ΄μ μΈ ν΄μμ΄ ν¬ν¨λμ΄ μμΌλ―λ‘, λμ ν΄μμ μ°Έκ³ νμ¬ μ§μ κ²μμ ν΄λ³΄λ©΄μ κ°λ μ μμκ°λ©΄ μ’κ² λ€. λν, νλ¦° λΆλΆμ΄ 보μΈλ€λ©΄ μλ €μ£Όμλ©΄ κ°μ¬νκ² λ€!
An overview of Dependency Injection
Spring framework serves as a dependency injection, which is a form of inversion control ( Inversion of Control is a programming principle which inverts the flow of control as compared to traditional control flow). IoC is a kind of Hollywood principle, don’t call us, we’ll call you, which is applied to programming.
μμ‘΄μ± μ£Όμ κ°μ
μ€νλ§ νλ μμν¬λ μ μ΄μ μμ (IoCλ μ μ΄μ νλ¦μ μ ν΅μ μΈ νλ¦κ³Ό λμΉ(λ°λμΈ)μν¨ νλ‘κ·Έλλ° μμΉμ)μ ν νμμΌλ‘ μμ‘΄μ± μ£Όμ μ μ 곡νλ€. IoCλ μΌμ’ μ ν리μ°λ μμΉμΈλ°, νλ‘κ·Έλλ°μ μ μ©λλ “μ°λ¦¬λ₯Ό νΈμΆνμ§ λ§μΈμ, μ°λ¦¬κ° νΈμΆν κ²μ”μ΄λ€.
"Don't call us, we'll call you"λ ν리μ°λ μμΉμ΄λ€. "μ°λ¦¬λ₯Ό λΆλ₯΄μ§λ§μΈμ, μ°λ¦¬κ° λΉμ μ λΆλ₯Όκ²μ." μ΄κ²μ νλ‘κ·Έλλ° μμΉμ μ μ©μμΌμ, callμ 'νΈμΆνλ€'λΌκ³ ν΄μνμλ€. μ΄λ, λΆλͺ¨ ν΄λμ€(μμ λͺ¨λ)μ μμ ν΄λμ€(νμ λͺ¨λ)μ νΈμΆν μ μμ§λ§, κ·Έ λ°λμ νΈμΆμ λΆκ°νλ€λ κ²μ΄λ€. μ€νλ§ νλ μμν¬μ μ μ©νμ¬ λ³Έλ€λ©΄, "μμ©νλ‘κ·Έλ¨ ν΄λμ€κ° νΉμ λΌμ΄λΈλ¬λ¦¬λ₯Ό νΈμΆνμ§ λ§μΈμ, μ°λ¦¬(μ€νλ§νλ μμν¬)κ° λΉμ μ νΈμΆν κ²μ" λΌκ³ ν΄μλ μ μκ² λ€.
The spring framework serving as a dependency injection binds together the classes of your application with the help of the instructions provided by you, which can be provided as annotations in the code or by explicitly binding the configuration code or by using an XML configuration.
μμ‘΄μ± μ£Όμ μΌλ‘μμ μ€νλ§νλ μμν¬μ μ 곡μ λΉμ μ μ΄ν리μΌμ΄μ μ ν΄λμ€λ€κ³Ό λΉμ μ΄ μ 곡ν μ€λͺ μ λ¬Άλλ°, κ·Έ μ€λͺ λ€μ μ½λμ μ΄λ Έν μ΄μ λ€μ΄λ λ°μΈλ©λ ꡬμ±(configuration)μ½λ λλ XML ꡬμ±μ μ¬μ©ν¨μΌλ‘μ¨ μ 곡λ μ μλ€
Explanation using an example
If you start with an application, it might require other classes as well, like helper and utility classes. So, in basic Java the application class would instantiate the helper and utility classes and then start using them, which will increase the coupling between the classes. Now there’s a tight coupling between the classes, as the application class now depends upon the correct implementation of helper and utility classes and also there is no concept of abstraction among the classes. This makes it difficult to test the application class alone, as it will always instantiate the other two classes.
μμλ₯Ό μ΄μ©ν μ€λͺ
λΉμ μ΄ μ΄ν리μΌμ΄μ μ μμνλ€λ©΄, κ·Έκ²μ ν¬νΌλ μ νΈλ¦¬ν° ν΄λμ€ κ°μ λ€λ₯Έ ν΄λμ€λ€ λν μꡬν κ²μ΄λ€. κ·Έλμ κΈ°λ³Έ μλ° μ΄ν리μΌμ΄μ ν΄λμ€λ ν¬νΌμ μ νΈλ¦¬ν° ν΄λμ€λ₯Ό μΈμ€ν΄μ€ν νκ³ λμ κ·Έκ²λ€μ μ¬μ©νκΈ° μμνλλ°, μ΄κ²μ ν΄λμ€λ€ κ°μ κ²°ν©λλ₯Ό μ¦κ°μν¬ κ²μ΄λ€. μ΄μ ν΄λμ€λ€ κ°μ λ¨λ¨ν κ²°ν©μ΄ μκ³ , κ·Έλ§νΌ μ΄ν리μΌμ΄μ ν΄λμ€λ€μ ν¬νΌλ μ νΈλ¦¬ν° ν΄λμ€λ€μ΄ νμ€ν μνλλμ§μ λν΄μ μμ‘΄νκ² λμμΌλ©°, λν ν΄λμ€λ€ μ¬μ΄μ μΆμν κ°λ μ΄ μκ² λλ€. μ΄κ²μ μ΄ν리μΌμ΄μ ν΄λμ€λ€μ΄ νμ λ€λ₯Έ λ ν΄λμ€λ€μ μΈμ€ν΄μ€ν νλ©΄μ μ΄ ν΄λμ€λ€μ κ°κ° ν μ€νΈνκΈ° μ΄λ ΅κ² λ§λ λ€.
μ€νλ§ νλ μμν¬λ₯Ό μ¬μ©νμ§ μμ λ, μ΄ν리μΌμ΄μ (=μμ©νλ‘κ·Έλ¨)μ΄ μ§μ ν¬νΌλ μ νΈλ¦¬ν° ν΄λμ€λ₯Ό μ§μ μ μΌλ‘ μ¬μ©νλλ°, μ΄λ λμ κ²°ν©λλ₯Ό 보μ΄λ©° μλ‘μκ² ν° μν₯μ λΌμΉκ² λλ λ¬Έμ λ₯Ό μ μνκ³ μλ€.
This is now when the spring dependency injection container comes into picture. With the help of spring dependency injection you will have the same classes to run your application but rather than instantiating and binding the classes, you will have to instruct the spring DI container to do the same for you. this can be done using annotations in the code or using configuration code or with an XML configuration code.
μ΄κ²μ μ€νλ§μ μμ‘΄μ± μ£Όμ 컨ν μ΄λλ₯Ό κ·Έλ¦ΌμΌλ‘ ννν κ²μ΄λ€. μ€νλ§μ μμ‘΄μ± μ£Όμ μ λμμΌλ‘, λΉμ μ μ΄ν리μΌμ΄μ μ μ€ννλ λ°μ λκ°μ ν΄λμ€λ€μ κ°μ§λ§, κ·Έ ν΄λμ€λ€μ μ§μ μΈμ€ν΄μ€ννκ³ λ°μΈλ©νλ λμ , λΉμ μ μ€νλ§ DI 컨ν μ΄λμκ² λμ μνν΄ μ€ κ²μ μ§μλ§ νλ©΄ λλ€. μ΄κ²μ μ½λμ μ΄λ Έν μ΄μ λ€μ μ¬μ©νκ±°λ, κ΅¬μ± μ½λλ XMLμ κ΅¬μ± μ½λλ‘ μνλ μ μλ€.
λΉμ¦λμ€ λ‘μ§μ΄ λ°μ΄ν°λ² μ΄μ€λ₯Ό μ§μ μ°Έμ‘°νμ¬ λμ κ²°ν©λλ₯Ό 보μ΄λ λͺ¨μ΅(μΌμͺ½)κ³Ό λ¬λ¦¬, IRepositoryλΌλ μΆμν λ μΈν°νμ΄μ€λ₯Ό μ°Έμ‘°νκ² νκ³ , μ΄ μΈν°νμ΄μ€λ λ°μ΄ν°λ² μ΄μ€μμ μμλ°μμΌλ‘μ¨ λΉμ¦λμ€ ν΄λμ€μ λ°μ΄ν° ν΄λμ€κ° μ§μ μ μΌλ‘ μ°κ΄λμ§ μκ² λ§λ λ€. λν μ΄λ μ€νλ§ DI(μμ‘΄μ±μ£Όμ ) 컨ν μ΄λκ° μ€ννλ€.
In the spring framework we have to tell the spring about the classes that are being used in an application code and the inter dependencies of the classes on each other. The spring DI controller will instantiate the helper, utility and application classes, these instances will be called as spring beans.
μ€νλ§νλ μμν¬μμ μ°λ¦¬λ μ΄ν리μΌμ΄μ μ½λμμ μ¬μ©λλ ν΄λμ€λ€κ³Ό ν΄λμ€λ€κ°μ μνΈμμ‘΄μ±(the inter dependencies)μ μ€νλ§μκ² μλ €μ£Όμ΄μΌ νλ€. μ€νλ§ DI 컨νΈλ‘€λ¬λ ν¬νΌμ μ νΈλ¦¬ν°, κ·Έλ¦¬κ³ μ΄ν리μΌμ΄μ ν΄λμ€λ€μ μΈμ€ν΄μ€νν κ²μ΄κ³ , μ΄λ¬ν μΈμ€ν΄μ€λ€μ μ€νλ§ λΉμ¦(spring beans)λΌκ³ λΆλ¦¬κ² λλ€.
Now that the DI container has been configured, it knows the dependencies among the classes of your application. But, without instantiating the helper and the utility classes from the application class the spring DI container will inject the helper and utility classes into the application class, by this the helper and the utility classes will be decoupled from the application class. Also the configuration of application is now more flexible because it is now instructed by the configuration class.
μ΄μ DI 컨ν μ΄λκ° κ΅¬μ±λμκ³ , λΉμ μ μ΄ν리μΌμ΄μ ν΄λμ€λ€ μ¬μ΄μ μμ‘΄μ±λ€μ μκ³ μλ€. κ·Έλ¬λ μ΄ν리μΌμ΄μ μ ν¬νΌμ μ νΈλ¦¬ν° ν΄λμ€λ€μ μΈμ€ν΄μ€ννμ§ μκ³ μ€νλ§ DI 컨ν μ΄λκ° ν¬νΌμ μ νΈλ¦¬ν° ν΄λμ€λ€μ μ΄ν리μΌμ΄μ ν΄λμ€μ μ£Όμ ν κ²μΈλ°, μ΄λ‘μ¨ ν¬νΌμ μ νΈλ¦¬ν° ν΄λμ€λ€μ μ΄ν리μΌμ΄μ ν΄λμ€λ‘λΆν° λΆλ¦¬λλ€. λν μ΄μ κ΅¬μ± ν΄λμ€λ€μ μν΄ μ§μλ°μκΈ° λλ¬Έμ μ΄ν리μΌμ΄μ μ ꡬμ±μ μ΄μ λ μ μ°ν΄μ‘λ€.
Summary
Hence spring dependency injection helps you in the way that, you have to declare the components and the dependencies between them and the DI container binds them together at the run-time.
μ€νλ§μ μμ‘΄μ± μ£Όμ μ μ΄μ κ°μ λ°©μμΌλ‘ λΉμ μ λκ³ , μ€μ§ μ»΄ν¬λνΈμ κ·Έ μ¬μ΄μ μμ‘΄μ±λ€μ μ μνκΈ°λ§ νλ©΄ λλ©°, DI 컨ν μ΄λκ° μ€μ μν μμ μ΄κ²λ€μ λ¬Άμ κ²μ΄λ€.
λκΈ