λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Basics/Concepts

Dependency Injection: Concept of Spring Framework

by IworldT 2020. 10. 25.
λ°˜μ‘ν˜•

과제λ₯Ό ν•˜λ©΄μ„œ, μ•„μ£Ό 쒋은 μ‚¬μ΄νŠΈλ₯Ό ν•˜λ‚˜ μ•Œκ²Œ λ˜μ—ˆλ‹€. κ΅μˆ˜λ‹˜κ»˜μ„œ μ†Œκ°œν•˜μ‹  λ―Έλ””μ—„! λ‹€μ–‘ν•œ λΆ„μ•Όμ˜ 전문가듀이 μ“΄ 글을 λ³Ό 수 μžˆλŠ” μΌμ’…μ˜ λΈ”λ‘œκ·Έ, λ―Έλ””μ—„(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을 'ν˜ΈμΆœν•˜λ‹€'라고 ν•΄μ„ν•˜μ˜€λ‹€. μ΄λŠ”, λΆ€λͺ¨ 클래슀(μƒμœ„ λͺ¨λ“ˆ)은 μžμ‹ 클래슀(ν•˜μœ„ λͺ¨λ“ˆ)을 ν˜ΈμΆœν•  수 μžˆμ§€λ§Œ, κ·Έ λ°˜λŒ€μ˜ ν˜ΈμΆœμ€ λΆˆκ°€ν•˜λ‹€λŠ” 것이닀. μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬μ— μ μš©ν•˜μ—¬ λ³Έλ‹€λ©΄, "μ‘μš©ν”„λ‘œκ·Έλž¨ ν΄λž˜μŠ€κ°€ νŠΉμ • 라이브러리λ₯Ό ν˜ΈμΆœν•˜μ§€ λ§ˆμ„Έμš”, 우리(μŠ€ν”„λ§ν”„λ ˆμž„μ›Œν¬)κ°€ 당신을 ν˜ΈμΆœν• κ²Œμš”" 라고 해석될 수 μžˆκ² λ‹€.

Inversion of Control Principle

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.

μ˜ˆμ‹œλ₯Ό μ΄μš©ν•œ μ„€λͺ…

당신이 μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ„ μ‹œμž‘ν•œλ‹€λ©΄, 그것은 ν—¬νΌλ‚˜ μœ ν‹Έλ¦¬ν‹° 클래슀 같은 λ‹€λ₯Έ ν΄λž˜μŠ€λ“€ λ˜ν•œ μš”κ΅¬ν•  것이닀. κ·Έλž˜μ„œ κΈ°λ³Έ μžλ°” μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ ν΄λž˜μŠ€λŠ” 헬퍼와 μœ ν‹Έλ¦¬ν‹° 클래슀λ₯Ό μΈμŠ€ν„΄μŠ€ν™” ν•˜κ³ λ‚˜μ„œ 그것듀을 μ‚¬μš©ν•˜κΈ° μ‹œμž‘ν•˜λŠ”λ°, 이것은 ν΄λž˜μŠ€λ“€ κ°„μ˜ 결합도λ₯Ό μ¦κ°€μ‹œν‚¬ 것이닀. 이제 ν΄λž˜μŠ€λ“€ 간에 λ‹¨λ‹¨ν•œ 결합이 있고, 그만큼 μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ ν΄λž˜μŠ€λ“€μ€ ν—¬νΌλ‚˜ μœ ν‹Έλ¦¬ν‹° ν΄λž˜μŠ€λ“€μ΄ ν™•μ‹€νžˆ μˆ˜ν–‰λ˜λŠ”μ§€μ— λŒ€ν•΄μ„œ μ˜μ‘΄ν•˜κ²Œ λ˜μ—ˆμœΌλ©°, λ˜ν•œ ν΄λž˜μŠ€λ“€ μ‚¬μ΄μ˜ 좔상화 κ°œλ…μ΄ μ—†κ²Œ λœλ‹€. 이것은 μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ ν΄λž˜μŠ€λ“€μ΄ 항상 λ‹€λ₯Έ 두 ν΄λž˜μŠ€λ“€μ„ μΈμŠ€ν„΄μŠ€ν™” ν•˜λ©΄μ„œ 이 ν΄λž˜μŠ€λ“€μ„ 각각 ν…ŒμŠ€νŠΈν•˜κΈ° μ–΄λ ΅κ²Œ λ§Œλ“ λ‹€.

μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šμ„ λ•Œ, μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜(=μ‘μš©ν”„λ‘œκ·Έλž¨)이 직접 ν—¬νΌλ‚˜ μœ ν‹Έλ¦¬ν‹° 클래슀λ₯Ό μ§μ ‘μ μœΌλ‘œ μ‚¬μš©ν•˜λŠ”λ°, μ΄λŠ” 높은 결합도λ₯Ό 보이며 μ„œλ‘œμ—κ²Œ 큰 영ν–₯을 끼치게 λ˜λŠ” 문제λ₯Ό μ œμ‹œν•˜κ³  μžˆλ‹€.

An example of spring DI container (μŠ€ν”„λ§ μ˜μ‘΄μ„± μ£Όμž… μ»¨ν…Œμ΄λ„ˆμ˜ ν•œ μ˜ˆμ‹œ)

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 μ»¨ν…Œμ΄λ„ˆκ°€ μ‹€μ œ μˆ˜ν–‰ μ‹œμ— 이것듀을 묢을 것이닀.

728x90
λ°˜μ‘ν˜•

λŒ“κΈ€