html
Spring Framework์์ ์์กด์ฑ ์ฃผ์ ๋ง์คํฐํ๊ธฐ: ์ข ํฉ ๊ฐ์ด๋
๋ชฉ์ฐจ
- ์๊ฐ ....................................................................................................... 1
- ์์กด์ฑ ์ฃผ์ ์ดํดํ๊ธฐ ........................................ 3
- Spring ํ๋ก์ ํธ ์ค์ .................................................... 6
- ์์กด์ฑ ์ฃผ์ ๊ตฌํ ........................................... 10
- ์ฝ๋ ๋ถ์ ...................................................................................... 15
- ์ ํ๋ฆฌ์ผ์ด์ ์คํ .................................................................... 20
- ๊ฒฐ๋ก ......................................................................................................... 25
- ์ถ๊ฐ ์๋ฃ ................................................................................ 26
์๊ฐ
"Spring Framework์์ ์์กด์ฑ ์ฃผ์ ๋ง์คํฐํ๊ธฐ: ์ข ํฉ ๊ฐ์ด๋"์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค. ์ํํธ์จ์ด ๊ฐ๋ฐ ๋ถ์ผ, ํนํ Java ์ํ๊ณ ๋ด์์ Dependency Injection (DI)์ ์ดํดํ๋ ๊ฒ์ ํ์ฅ ๊ฐ๋ฅํ๊ณ , ์ ์ง๋ณด์๊ฐ ์ฉ์ดํ๋ฉฐ, ํ ์คํธ ๊ฐ๋ฅํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ถํ๋ ๋ฐ ์ค์ํฉ๋๋ค. ์ด eBook์ ์์กด์ฑ ์ฃผ์ ์ ๊ฐ๋ ์ ๊น์ด ์๊ฒ ๋ค๋ฃจ๋ฉฐ, ์ค์ฉ์ ์ธ ํ๋ก์ ํธ ์์ ๋ฅผ ํตํด Spring Framework๋ฅผ ์ฌ์ฉํ ๊ตฌํ ๋ฐฉ์์ ์ค๋ช ํฉ๋๋ค.
๋ฐฐ์ธ ๋ด์ฉ
- ์์กด์ฑ ์ฃผ์ ์ ๊ธฐ๋ณธ ๊ฐ๋ ๊ณผ Spring Framework์์์ ์ญํ .
- DI๋ฅผ ์ํ Spring ํ๋ก์ ํธ ์ค์ ๋จ๊ณ๋ณ ๊ฐ์ด๋.
- ์ฝ๋ ์์ ๋ฅผ ํตํ DI์ ์ค์ฉ์ ๊ตฌํ.
- DI ์ฌ์ฉ ์์ ๋ชจ๋ฒ ์ฌ๋ก์ ์ผ๋ฐ์ ์ธ ์ค์.
์ด ๊ฐ์ด๋๋ฅผ ๋๊น์ง ์ฝ์ผ๋ฉด ์์กด์ฑ ์ฃผ์ ์ ๋ํ ํ์คํ ์ดํด์ ํจ๊ป Spring์ ํ์ฉํ์ฌ Java ์ ํ๋ฆฌ์ผ์ด์ ์ ํฅ์์ํค๋ ๋ฐฉ๋ฒ์ ์๊ฒ ๋ ๊ฒ์ ๋๋ค.
์์กด์ฑ ์ฃผ์ ์ดํดํ๊ธฐ
์์กด์ฑ ์ฃผ์ ์ด๋?
Dependency Injection (DI)์ ํ๋์ฝ๋ฉ๋ ์์กด์ฑ์ ์ ๊ฑฐํ์ฌ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ณด๋ค ๋ชจ๋ํํ๊ณ ํ ์คํธ ๊ฐ๋ฅํ๊ฒ ๋ง๋๋ ๋์์ธ ํจํด์ ๋๋ค. ๊ฐ๋จํ ๋งํด, DI๋ ๊ฐ์ฒด๊ฐ ์ค์ค๋ก ์์กด์ฑ์ ์์ฑํ์ง ์๊ณ ์ธ๋ถ ์์ค๋ก๋ถํฐ ์์กด์ฑ์ ๋ฐ๋๋ก ํฉ๋๋ค.
ํต์ฌ ๊ฐ๋ :
- ์ ์ด์ ์ญ์ (IoC): DI๋ IoC์ ํ ํํ๋ก, ์์กด์ฑ ์์ฑ ๋ฐ ๊ด๋ฆฌ๋ฅผ ๊ฐ์ฒด์์ ์ธ๋ถ ์ํฐํฐ(์: Spring)๋ก ์ญ์ ์ํต๋๋ค.
- ์์กด์ฑ: ํด๋์ค๊ฐ ๊ธฐ๋ฅ์ ์ํํ๊ธฐ ์ํด ํ์ํ ๊ฐ์ฒด๋ค์
๋๋ค. ์๋ฅผ ๋ค์ด,
Car
ํด๋์ค๋Engine
ํด๋์ค์ ์์กดํ ์ ์์ต๋๋ค.
์์กด์ฑ ์ฃผ์ ์ ์ฅ์
- ํฅ์๋ ํ ์คํธ ๊ฐ๋ฅ์ฑ: ์์กด์ฑ์ ๋ชจํนํ์ฌ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋ฆฝ์ ์ผ๋ก ํ ์คํธํ ์ ์์ต๋๋ค.
- ๋์จํ ๊ฒฐํฉ: ํด๋์ค ๊ฐ์ ์ํธ ์์กด์ฑ์ ์ค์ฌ ์์คํ ์ ๋์ฑ ์ ์ฐํ๊ฒ ๋ง๋ญ๋๋ค.
- ์ ์ง๋ณด์์ฑ: ๋ค๋ฅธ ์ปดํฌ๋ํธ์ ์ํฅ์ ์ฃผ์ง ์๊ณ ์ฝ๊ฒ ๊ด๋ฆฌ ๋ฐ ์ ๋ฐ์ดํธํ ์ ์์ต๋๋ค.
- ์ฌ์ฌ์ฉ์ฑ: ์ปดํฌ๋ํธ๋ฅผ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ค์ํ ๋ถ๋ถ์์ ์ฌ์ฌ์ฉํ ์ ์์ต๋๋ค.
์์กด์ฑ ์ฃผ์ ์ ์ฌ์ฉํ ๋์ ์ฅ์
DI๋ ์์กด์ฑ์ ์๋์ผ๋ก ๊ด๋ฆฌํ๋ ๊ฒ์ด ๋ฒ๊ฑฐ๋ก์ด ํฌ๊ณ ๋ณต์กํ ์ ํ๋ฆฌ์ผ์ด์ ์์ ํนํ ์ ์ฉํฉ๋๋ค. ์ ์ฉ ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
- ๋์ ๋ชจ๋ํ์ ํ์ฅ์ฑ์ด ์๊ตฌ๋๋ ์ ํ๋ฆฌ์ผ์ด์ .
- ๋ค์ํ ๊ตฌ์ฑ ์ต์ ์ ๊ฐ์ง ์์คํ .
- ํ ์คํธ ์ฃผ๋ ๊ฐ๋ฐ(TDD)์ ๊ฐ์กฐํ๋ ํ๋ก์ ํธ.
๋น๊ต ํ: ์๋ ์์กด์ฑ ๊ด๋ฆฌ vs. ์์กด์ฑ ์ฃผ์
์ธก๋ฉด | ์๋ ์์กด์ฑ ๊ด๋ฆฌ | ์์กด์ฑ ์ฃผ์ |
---|---|---|
๊ฒฐํฉ๋ | ๋์ ๊ฒฐํฉ๋ | ๋์จํ ๊ฒฐํฉ๋ |
ํ ์คํธ ๊ฐ๋ฅ์ฑ | ๋ ๋ฆฝ์ ์ผ๋ก ํ ์คํธํ๊ธฐ ์ด๋ ค์ | ์์กด์ฑ์ ์ฝ๊ฒ ๋ชจํน ๊ฐ๋ฅ |
์ ์ฐ์ฑ | ๋ฎ์ | ๋์ |
์ฝ๋ ์ ์ง๋ณด์์ฑ | ๋ฎ์ | ๋์ |
๊ตฌ์ฑ | ํ๋์ฝ๋ฉ | ์ธ๋ถ ๊ตฌ์ฑ(XML, ์ ๋ ธํ ์ด์ ๋ฑ) |
Spring ํ๋ก์ ํธ ์ค์
ํ๋ก์ ํธ ๊ตฌ์กฐ ์์ฑ
๋จผ์ ๊ธฐ๋ณธ์ ์ธ Spring ํ๋ก์ ํธ ๊ตฌ์กฐ๋ฅผ ์ค์ ํฉ๋๋ค. ๋ค์ ํ์ผ๊ณผ ๋๋ ํ ๋ฆฌ๊ฐ ์๋์ง ํ์ธํ์ธ์:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
S02L01-Dependency Injection/ โโโ src/ โ โโโ main/ โ โ โโโ java/ โ โ โ โโโ App.java โ โ โ โโโ AppConfig.java โ โ โ โโโ org/studyeasy/ โ โ โ โโโ car/ โ โ โ โ โโโ Corolla.java โ โ โ โ โโโ Swift.java โ โ โ โโโ interfaces/ โ โ โ โโโ Car.java โ โ โโโ webapp/ โ โ โโโ index.jsp โโโ pom.xml |
AppConfig.java๋ก Spring ๊ตฌ์ฑ
AppConfig.java ํด๋์ค๋ Spring์ ๊ตฌ์ฑ ์ผํฐ ์ญํ ์ ํ๋ฉฐ, ์ปดํฌ๋ํธ ์ค์บ์ ํ์ฑํํ๊ณ ๋น์ ์ ์ํฉ๋๋ค.
1 2 3 4 5 6 7 8 |
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan("org.studyeasy") public class AppConfig { // ์ปดํฌ๋ํธ ์ค์บ์ ํ์ฑํํ๋ ๊ตฌ์ฑ ํด๋์ค } |
์ค๋ช :
- @Configuration: ํด๋์ค์ @Bean ์ ์ ๋๋ import๊ฐ ์์์ ๋ํ๋ ๋๋ค.
- @ComponentScan: Spring์ด ์ปดํฌ๋ํธ, ๊ตฌ์ฑ ๋ฐ ์๋น์ค๋ฅผ ์ฐพ์ ์์น๋ฅผ ์ง์ ํฉ๋๋ค.
์์กด์ฑ ์ฃผ์ ๊ตฌํ
Car ์ธํฐํ์ด์ค ์ ์
Car ์ธํฐํ์ด์ค๋ ๊ตฌ์ฒด์ ์ธ ์๋์ฐจ ํด๋์ค์์ ๊ตฌํ๋ specs ๋ฉ์๋๋ฅผ ์ ์ธํฉ๋๋ค.
1 2 3 4 5 |
package org.studyeasy.interfaces; public interface Car { void specs(); } |
Car ๊ตฌํ์ฒด ์์ฑ: Swift์ Corolla
Swift.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.studyeasy.car; import org.springframework.stereotype.Component; import org.studyeasy.interfaces.Car; @Component public class Swift implements Car { @Override public void specs() { System.out.println("Suzuki์ ํด์น๋ฐฑ"); } } |
Corolla.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.studyeasy.car; import org.springframework.stereotype.Component; import org.studyeasy.interfaces.Car; @Component public class Corolla implements Car { @Override public void specs() { System.out.println("Toyota์ ์ธ๋จ"); } } |
์ค๋ช :
- @Component: ํด๋์ค๋ฅผ Spring ๊ด๋ฆฌ ์ปดํฌ๋ํธ๋ก ํ์ํ์ฌ ์ปดํฌ๋ํธ ์ค์บ ๋ฐ DI์ ๋์์ด ๋ฉ๋๋ค.
์์กด์ฑ ์ฃผ์ ์ ์ํ ์ ๋ ธํ ์ด์ ๊ตฌ์ฑ ์ฌ์ฉ
App.java์์๋ Spring์ ApplicationContext๋ฅผ ํ์ฉํ์ฌ ์์กด์ฑ์ ๊ด๋ฆฌํ๊ณ ์ฃผ์ ํฉ๋๋ค.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.studyeasy.interfaces.Car; public class App { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Car myCar = context.getBean(Car.class); myCar.specs(); ((AnnotationConfigApplicationContext) context).close(); } } |
์ค๋ช :
- ApplicationContext: ๋น๊ณผ ์์กด์ฑ์ ๊ด๋ฆฌํ๋ Spring ์ปจํ ์ด๋.
- getBean(): Spring ์ปจํ ์คํธ์์ ์ง์ ๋ ํ์ ์ ๋น์ ๊ฐ์ ธ์ต๋๋ค.
- ์ปจํ ์คํธ ๋ซ๊ธฐ: ๋ชจ๋ ๋ฆฌ์์ค๋ฅผ ์ ๋๋ก ํด์ ํ๋๋ก ์ปจํ ์คํธ๋ฅผ ์ข ๋ฃํฉ๋๋ค.
์ฝ๋ ๋ถ์
App.java: ๋ฉ์ธ ํด๋์ค
App.java๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ง์ ์ ์ญํ ์ ํฉ๋๋ค. Spring ์ปจํ ์คํธ๋ฅผ ์ด๊ธฐํํ๊ณ , Car ๋น์ ๊ฐ์ ธ์ specs ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค.
์ฝ๋ ๋ถ์:
- Spring ์ปจํ
์คํธ ์ด๊ธฐํ:
1ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
AppConfig ๊ตฌ์ฑ์ ๊ธฐ๋ฐ์ผ๋ก ์๋ก์ด ์ปจํ ์คํธ๋ฅผ ์์ฑํฉ๋๋ค. - ๋น ๊ฐ์ ธ์ค๊ธฐ ๋ฐ ์ฌ์ฉ:
12Car myCar = context.getBean(Car.class);myCar.specs();
Car ๋น(Swift ๋๋ Corolla)์ ๊ฐ์ ธ์ specs ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค. - ์ปจํ
์คํธ ๋ซ๊ธฐ:
1((AnnotationConfigApplicationContext) context).close();
Spring ์ปจํ ์คํธ๋ฅผ ์ข ๋ฃํ์ฌ ๋ฆฌ์์ค๋ฅผ ํด์ ํฉ๋๋ค.
AppConfig.java: ๊ตฌ์ฑ ํด๋์ค
AppConfig.java๋ ์ง์ ๋ ํจํค์ง๋ฅผ ์ค์บํ์ฌ ์ปดํฌ๋ํธ๋ฅผ ํ์งํ๊ณ ์๋์ผ๋ก ์์กด์ฑ์ ์ฃผ์ ํ ์ ์๋๋ก ํฉ๋๋ค.
ํต์ฌ ์ ๋ ธํ ์ด์ :
- @Configuration: ํด๋์ค๋ฅผ ๋น ์ ์์ ์์ค๋ก ํ์ํฉ๋๋ค.
- @ComponentScan: Spring ์ปดํฌ๋ํธ๋ฅผ ํ์งํ ํจํค์ง๋ฅผ ์ง์ ํฉ๋๋ค.
Swift.java์ Corolla.java: ๊ตฌ์ฒด์ ๊ตฌํ์ฒด
Swift.java์ Corolla.java๋ ๋ชจ๋ Car ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ฉฐ @Component๋ก ์ ๋ ธํ ์ด์ ๋์ด ์์ด Spring์ ์ปดํฌ๋ํธ ์ค์บ ๋์์ด ๋ฉ๋๋ค.
์ฝ๋ ํ์ด๋ผ์ดํธ:
- specs ๋ฉ์๋๋ฅผ ๊ตฌํํ์ฌ ํน์ ์๋์ฐจ ์ธ๋ถ ์ ๋ณด๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
- @Component๋ฅผ ์ฌ์ฉํ์ฌ Spring ์ ํ๋ฆฌ์ผ์ด์ ์ปจํ ์คํธ์ ๋ฑ๋กํฉ๋๋ค.
์ ํ๋ฆฌ์ผ์ด์ ์คํ
์์กด์ฑ ์ฃผ์ ์์ด ์คํ
์ด๊ธฐ์๋ DI ์์ด ๊ฐ์ฒด๋ฅผ App.java ํด๋์ค ๋ด์์ ์๋์ผ๋ก ์์ฑํฉ๋๋ค.
์ฝ๋ ์ค๋ํซ:
1 2 3 4 5 6 7 8 9 |
public class App { public static void main(String[] args) { Car swift = new Swift(); swift.specs(); // ์ถ๋ ฅ: Suzuki์ ํด์น๋ฐฑ Car corolla = new Corolla(); corolla.specs(); // ์ถ๋ ฅ: Toyota์ ์ธ๋จ } } |
์ฅ์ :
- ์์ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋จํจ.
๋จ์ :
- ํด๋์ค ๊ฐ์ ๋์ ๊ฒฐํฉ๋.
- ๋๊ท๋ชจ ํ๋ก์ ํธ์์ ์์กด์ฑ ๊ด๋ฆฌ๊ฐ ์ด๋ ค์.
- ์์ ์ ์ฝ๋ ๋ณ๊ฒฝ ๋ฐ ์ฌ๋ฐฐํฌ๊ฐ ํ์ํจ.
์์กด์ฑ ์ฃผ์ ๊ณผ ํจ๊ป ์คํ
Spring์ DI๋ฅผ ํ์ฉํ๋ฉด ๊ฐ์ฒด ์์ฑ๊ณผ ์์กด์ฑ ๊ด๋ฆฌ๋ฅผ ํ๋ ์์ํฌ์ ๋งก๊ธธ ์ ์์ต๋๋ค.
์์ ๋ ์ฝ๋ ์ค๋ํซ:
1 2 3 4 5 6 7 8 9 10 |
public class App { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Car myCar = context.getBean(Car.class); myCar.specs(); // ์ถ๋ ฅ์ ๋น ๊ตฌ์ฑ์ ๋ฐ๋ผ ๋ค๋ฆ ((AnnotationConfigApplicationContext) context).close(); } } |
์ฅ์ :
- ๋์จํ ๊ฒฐํฉ๋: ํด๋์ค๊ฐ ์์ ์ ์์กด์ฑ์ ์์ฑํ ์ฑ ์์ด ์์ต๋๋ค.
- ์ ์ฐ์ฑ: ํด๋ผ์ด์ธํธ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ์ง ์๊ณ ๋ ๋ค์ํ ๊ตฌํ์ฒด ๊ฐ ์ฝ๊ฒ ์ ํํ ์ ์์ต๋๋ค.
- ํ์ฅ์ฑ: ํฌ๊ณ ๋ณต์กํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ ์ ํฉํฉ๋๋ค.
์ถ๋ ฅ ์ค๋ช :
์ ํ๋ฆฌ์ผ์ด์ ์ด ์คํ๋๋ฉด Spring์ ์ฌ์ฉ ๊ฐ๋ฅํ ๋น์ ๋ฐ๋ผ ์ ์ ํ Car ๊ตฌํ์ฒด(Swift ๋๋ Corolla)๋ฅผ ์ฃผ์ ํฉ๋๋ค. specs ๋ฉ์๋๋ ํด๋น ์๋์ฐจ์ ์ธ๋ถ ์ ๋ณด๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
์ํ ์ถ๋ ฅ:
1 |
Suzuki์ ํด์น๋ฐฑ |
๊ฒฐ๋ก
Dependency Injection์ Spring Framework์ ํต์ฌ์ผ๋ก, Java ์ ํ๋ฆฌ์ผ์ด์ ์์ ๋ชจ๋ํ, ํ์ฅ์ฑ, ์ ์ง๋ณด์์ฑ์ ์ด์งํฉ๋๋ค. ์์กด์ฑ์ ์์ฑ ๋ฐ ๊ด๋ฆฌ๋ฅผ ์ธ๋ถํํจ์ผ๋ก์จ DI๋ ๋ ๊น๋ํ ์ฝ๋ ์ํคํ ์ฒ๋ฅผ ๊ฐ๋ฅํ๊ฒ ํ๊ณ ํ ์คํธ ๊ฐ๋ฅ์ฑ์ ํฅ์์ํต๋๋ค.
์ด ๊ฐ์ด๋์์๋ ์์กด์ฑ ์ฃผ์ ์ ๊ธฐ๋ณธ ๊ฐ๋ ์ ํ๊ตฌํ๊ณ , Spring ํ๋ก์ ํธ๋ฅผ ์ค์ ํ๋ฉฐ, ์ค์ฉ์ ์ธ ์ฝ๋ ์์ ๋ฅผ ํตํด DI๋ฅผ ๊ตฌํํ๊ณ , ์ ํต์ ์ธ ์์กด์ฑ ๊ด๋ฆฌ ๋ฐฉ์์ ๋นํด DI์ ์ฅ์ ์ ๋ถ์ํ์ต๋๋ค. DI๋ฅผ ๋์ ํ๋ฉด ๊ฐ๋ฐ ํ๋ก์ธ์ค๊ฐ ๊ฐ์ํ๋ ๋ฟ๋ง ์๋๋ผ ๊ฒฌ๊ณ ํ๊ณ ์ ์ฐํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ถํ ์ ์๋ ๊ธธ์ ์ด์ด์ค๋๋ค.
SEO ํค์๋: Dependency Injection, Spring Framework, Java Development, Spring DI Tutorial, Inversion of Control, Spring Components, Spring ApplicationContext, Spring Beans, Car Interface Example, Spring Configuration, Modular Java Applications, Testable Code, Spring ComponentScan, Spring @Component, Spring @Configuration, Loose Coupling Java, Spring Dependency Management, Spring Bean Lifecycle, Spring IoC Container, Spring Annotations
์ถ๊ฐ ์๋ฃ
- Spring Framework ๋ฌธ์
- ์ ์ด์ ์ญ์ (IoC)๊ณผ ์์กด์ฑ ์ฃผ์ (DI) ์ดํดํ๊ธฐ
- ์ด๋ณด์๋ฅผ ์ํ Spring ํํ ๋ฆฌ์ผ
- Joshua Bloch์ Effective Java
๋ ธํธ: ์ด ๊ธฐ์ฌ๋ AI์ ์ํด ์์ฑ๋์์ต๋๋ค.