
์๋ก
Spring์์ ๊ฐ์ฅ ์ค์ํ ๊ฐ๋ ์ค ํ๋์ธ 'DI'์ 'IoC', spring์ ์ฌ์ฉํ๋ค๋ฉด ํ๋ฒ์ฏค์ ๋ค์ด๋ดค์ ์ฉ์ด์ธ๋ฐ ๊ทธ ์๋ฏธ์ ๋ํด์๋ ์ ๋ชจ๋ฅด๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. ์ค๋์ spring์ ํต์ฌ ์ฉ์ด์ธ 'DI'์ 'IoC'์ ๋ํด์ ์์๋ณด๋๋ก ํ์!
DI, IoC ๊ฐ๋
> DI
- DI(Dependency Injection)์ด๋ '์์กด์ฑ ์ฃผ์ '์ ๋งํ๋ค.
- ์์กด์ฑ ์ฃผ์ ์ ๊ณง ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฐ์๊ฐ ์ง์ ์์ฑํ๋๊ฒ ์๋๋ผ IOC์ปจํ ์ด๋์์ ๊ฐ์ฒด๋ฅผ ์์ฑํ ํ ์ฃผ์ ์์ผ์ฃผ๋ ๋ฐฉ์์ด๋ค.
- ๊ฐ์ฒด์ ์์กด์ฑ์ ์ญ์ ์์ผ ๊ฐ์ฒด ๊ฐ์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ ์ ์ฐํ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๊ธฐ ํ์ฌ ๊ฐ๋ ์ฑ ๋ฐ ์ฝ๋ ์ค๋ณต, ์ ์ง๋ณด์๋ฅผ ์ฝ๊ฒ ํ ์ ์๊ฒ ํด์ค๋ค.
- ์์กด์ฑ์ ์ฃผ์ ํ๋ ๋ฐฉ๋ฒ์ผ๋ก๋ 3๊ฐ์ง ๋ฐฉ์์ด ์กด์ฌํ๋ค. (๊ฐ ๋ฐฉ์์ ์์ ์ฝ๋๋ ์ดํ์์ ์ค๋ช )
(์ฝ๋๋จผ์ ๋ณด๊ณ ์ถ์ผ๋ฉด ์คํฌ๋กค ๋ด๋ ค์ ํ์ธ)
ใ1. ์์ฑ์ ์ฃผ์ (Constructor Injection)
ใ2. ์์ ์ ์ฃผ์ (Setter Injection)
ใ3. ํ๋ ์ฃผ์ (Field Injection)
- 3๊ฐ์ง ๋ฐฉ์ ์ค ์คํ๋ง์์ ๊ถ์ฅํ๊ณ , ๋ง์ด ์ฐ๋ ๋ฐฉ์์ '์์ฑ์ ์ฃผ์ ' ๋ฐฉ์์ด๋ค.
> IoC
- IoC(Inversion of Control)์ '์ ์ด์ ์ญ์ '์ด๋ผ ๋ถ๋ฅธ๋ค.
- ์ ์ด์ ์ญ์ ์ด๋ ๋ง ๊ทธ๋๋ก ๋ฉ์๋๋ ๊ฐ์ฒด๋ฅผ ํธ์ถํ ๋, ๊ทธ ์์ ์ ๊ฐ๋ฐ์๊ฐ ์ง์ ํ๋ ๊ฒ์ด ์๋ spring์๊ฒ ์ ์ดํ ๊ถํ์ ๋๊ธฐ๋ ๊ฒ์ ๋งํ๋ค.
- IoC ๋ผ๋ ๊ฐ๋ ์ด ์๊ฒจ๋๊ธฐ ์ด์ ์๋ ๊ฐ๋ฐ์๊ฐ ์ง์ ๊ฐ์ฒด์ ์์ฑ์ ๊ด๋ฆฌํ๊ณ ์ ์ดํ์ง๋ง, ์คํ๋ง์ ์ฌ์ฉํ๊ฒ ๋๋ฉด์ ์คํ๋ง ์ปจํ ์ด๋์๊ฒ ์ ์ด๊ถ์ ๋๊ฒจ ์คํ๋ง ์ปจํ ์ด๋๊ฐ ํ๋ฆ์ ์ ์ดํ ์ ์๋๋กํ์ฌ ๋ณด๋ค ํธํ ๊ฐ๋ฐ์ด ๊ฐ๋ฅํด์ก๋ค.
Bean๊ณผ IoC ์ปจํ ์ด๋
- IoC ์ปจํ ์ด๋๊ฐ ๊ด๋ฆฌํ๋ ๊ฐ์ฒด๋ฅผ 'Bean(๋น)'์ด๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ, spring์ ์ด๋ฌํ Bean(๊ฐ์ฒด)๋ค์ ์์กด์ฑ์ ๊ด๋ฆฌํ๊ณ Bean์ผ๋ก ๋ฑ๋ก๋ ํด์ฃผ๊ณ ๋ง๋ค์ด์ง Bean๋ค์ ์ด๊ด์ ์ผ๋ก ๊ด๋ฆฌํ๋ค. ์ฆ ๊ฐ๋ฐ์๊ฐ ์ด๋ฌํ ๋ถ๋ถ์ ์ ๊ฒฝ์ฐ์ง ์์๋ spring์ด ์์์ ๊ด๋ฆฌํด์ฃผ๋ ๊ฒ์ด๋ค.
- ์์กด์ฑ ์ฃผ์ ์ ๋ฐ๋์ Bean์ผ๋ก ๋ฑ๋ก๋ ๊ฐ์ฒด๋ค ๋ผ๋ฆฌ๋ง ๊ฐ๋ฅํ๋ค.
- ์ฆ, IoC ์ปจํ ์ด๋๋ Bean์ผ๋ก ๋ฑ๋ก๋์ง ์์ ๊ฐ์ฒด์๊ฒ ์์กด์ฑ ์ฃผ์ ์ ํด์ฃผ์ง ์๋๋ค.
> ๐ก Bean ๋ฑ๋กํ๋ ๋ฐฉ๋ฒ
1. ๋ฑ๋กํ๋ ค๋ ๊ฐ์ฒด๋ฅผ XML ํ์ผ์ ์ค์
xml ์ค์ ํ์ผ์ ๋ ํด๋์ค๋ฅผ ๊ฐ๊ฐ Bean์ผ๋ก ๋ฑ๋กํ๊ณ , testService์ ํน์ฑ์ผ๋ก testRepository๋ฅผ ๋ฑ๋กํด ์ฐธ์กฐํ๊ฒ ํ๋ฉด ๋๋ค.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="testService"
class="me.wordbe.springgoahead.testService">
<property name="testRepository" ref="testRepository" />
</bean>
<bean id="testRepository"
class="me.wordbe.springgoahead.testRepository" />
</beans>
2. Component Scan
Spring์ด Bean์ ์์์ ์ฐพ๊ณ ๋ฑ๋กํ ์ ์๋๋ก Component๋ฅผ ์ฌ์ฉํ๋ค๊ณ ์ค์ ํด์ค๋ค.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="me.wordbe.springgoahead" />
</beans>
3. ํด๋์ค ์ ์ธ๋ถ ์์ ์ด๋ ธํ ์ด์ ์ค์
1~2๋ฒ์ ๊ณผ์ ์ ํ๊ณ , ํด๋์ค ์ ์ธ๋ถ ์์ "Component" ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํด์ฃผ๋ฉด ์ปดํฌ๋ํธ ์ด๋ ธํ ์ด์ ์ ์ค์ ํด์ค ๊ฐ์ฒด๋ฅผ Bean์ ์ ์์ ์ผ๋ก ๋ฑ๋กํ๋ค.
@Component
public class TestService{
}
ํํ ์ฌ์ฉํ๋ "@Controller", "@Service", "@Repository"๋ ๋ชจ๋ "@Component"๋ฅผ ํฌํจํ๊ณ ์์ผ๋ฏ๋ก ์ด๋๊ฒ์ ์ฌ์ฉํด๋ ๋ฌด๋ฐฉํ๋ค.(but ๊ตฌํํ๊ณ ์ ํ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์กฐ์ ๋ชฉ์ ์ ๋ฐ๋ผ ๊ตฌ์ฒด์ ์ธ ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ฌ ๊ฐ๋ ์ฑ์ ๋์ด๋ ๊ฒ์ด ์ข๋ค)
IoC ์ปจํ ์ด๋์ Bean์ ๋ฑ๋กํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํ ๋ฐฉ์ ์ธ์๋ 'Java Config'๋ฅผ ์ด์ฉํ ๋ฑ๋ก๋ฐฉ๋ฒ ๋ฑ ๋ค์ํ ๋ฐฉ์์ด ์กด์ฌํ๋ค.
DI, ์์กด์ฑ ์ฃผ์ ํ๋ ๋ฐฉ๋ฒ
- ์์กด์ฑ์ ์ฃผ์ ํ๋ ๋ฐฉ๋ฒ์ ํฌ๊ฒ 3๊ฐ์ง ๋ฐฉ์์ด ์๋ค.
- ์์ฑ์ ์ฃผ์
- setter ์ฃผ์
- ํ๋ ์ฃผ์
(Spring ๊ณต์ ๋ฌธ์์์๋ '์์ฑ์ ์ฃผ์ ' ๋ฐฉ์์ ๊ถ์ฅํ๋ค.)
- ์์ฑ์ ์ฃผ์ ๋ฐฉ์์ ๊ถ์ฅํ๊ณ ์์ง๋ง ์์กด ๊ด๊ณ๊ฐ ์ ํ์ ์ด๊ฑฐ๋ ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ ๊ฒฝ์ฐ์๋ setter ์ฃผ์ ๋ฐฉ์์ ์ฌ์ฉํ๊ธฐ๋ ํ๋ค.
>๐ก์์ฑ์ ์ฃผ์
- ์์ฑ์ ํธ์ถ ์์ ์์ ํ ๋ฒ๋ง ํธ์ถ๋๋ ๊ฒ์ ๋ณด์ฅํ๋ค.
- ๋ถํธ๊ณผ ํ์ ์์กด ๊ด๊ณ์ ์ฌ์ฉํ๋ค.
- ์์ฑ์๊ฐ ํ๋๋ง ์กด์ฌํ๋ค๋ฉด '@Autowired'๋ฅผ ์๋ตํด๋ ์๋์ผ๋ก ์ฃผ์ ๋๋ค.
- NullPointerException์ ๋ฐฉ์งํ ์ ์๋ค.
@Service
public class TestService {
private final SampleTest sample;
@Autowired
public TestService(SampleTest sample) {
this.sample = sample;
}
}
>๐กsetter(์์ ์) ์ฃผ์
- setter ํ๋์ ๊ฐ์ ๋ณ๊ฒฝํ๋ ๋ฉ์๋๋ฅผ ํตํด ์์กด ๊ด๊ณ๋ฅผ ์ฃผ์ ํ๋ ๋ฐฉ์์ด๋ค.
- ์ ํ๊ณผ ๋ณ๊ฒฝ ๊ฐ๋ฅ์ฑ์ด ์๋ ์์กด ๊ด๊ณ์ ์ฌ์ฉํ๋ค.
- '@Autowired'๋ฅผ ์จ์ฃผ์ง ์์ผ๋ฉด ์คํ๋์ง ์๋๋ค.
@Service
public class TestService {
private final SampleTest sample;
@Autowired
public void setSampleTest(SampleTest sample) {
this.sample = sample;
}
}
>๐กํ๋ ์ฃผ์
- ๊ฐ๊ฒฐํ ์ฝ๋ ์์ฑ์ด ๊ฐ๋ฅํด์ง๋ค.
- ์ธ๋ถ์์ ๋ณ๊ฒฝ์ด ๋ถ๊ฐ๋ฅํด ํ ์คํธํ๊ธฐ๊ฐ ์ด๋ ต๋ค.
- DI ํ๋ ์์ํฌ๊ฐ ์์ผ๋ฉด ์๋ฌด๊ฒ๋ ํ ์ ์๋ค.
- ์๋๋๊ฒ ํ๋ ค๋ฉด ๊ฒฐ๊ตญ setter๊ฐ ํ์ํ๋ค.
@Service
public class TestService {
@Autowired
private final SampleTest sample;
@Autowired
private final TestRepository testRepository;
}
- ํ๋ ์ฃผ์ ์ ํ๊ฒ ๋๋ฉด 'DI ์ปจํ ์ด๋' ์์์๋ง ์๋ํ๊ฒ ๋๋ฏ๋ก ์์ ์๋ฐ ์ฝ๋๋ก ํ ์คํธํ๊ธฐ๊ฐ ์ด๋ ค์ ์ ์ฌ์ฉํ์ง ์๋๋ค.
์ ๋ฆฌ
| IoC(Inversion of Control) | DI(Dependency Injection) | |
| ์๋ฏธ | ์ ์ด์ ์ญ์ | ์์กด์ฑ ์ฃผ์ |
| ๊ฐ๋ | ํ๋ก๊ทธ๋จ์์ ์ ์ด์ ํ๋ฆ์ด ์ญ์ | ๊ฐ๋ฐ์๊ฐ ๊ฐ์ฒด์์ฑX, IoC์ปจํ ์ด๋์์ ์์ฑ ํ ์ฃผ์ |
| ์ญํ | ๊ฐ์ฒด์ ์๋ช ์ฃผ๊ธฐ ๋ฐ ๊ด๋ฆฌ๋ฅผ ์ธ๋ถ์ ์์ | ๊ฐ์ฒด ๊ฐ์ ์์กด์ฑ์ ์ธ๋ถ์์ ์ฃผ์ |
| ๋ชฉ์ | ์ ์ฐ์ฑ, ํ์ฅ์ฑ, ์ฌ์ฌ์ฉ์ฑ์ ํ๋ณด | ๊ฐ์ฒด ๊ฐ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ ์ฝ๋์ ์ ์ฐ์ฑ ํ๋ณด |
| ์ฅ์ | ๊ฐ์ฒด์ ์๋ช ์ฃผ๊ธฐ์ ๊ด๋ฆฌ๋ฅผ ์ธ๋ถ์ ์์ํจ์ผ๋ก์จ ์ ์ฐ์ฑ๊ณผ ํ์ฅ์ฑ ์ ๊ณต | ์ฝ๋์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถฐ ์ ์ง๋ณด์์ ์ฉ์ด |
Reference
[Spring] ์์กด์ฑ ์ฃผ์ (Dependency Injection)์ ๋ํ์ฌ
Spring์ ์ค์ํ ์๋ฆฌ ์์กด์ฑ ์ฃผ์ (DI)์ด ๋ฌด์์ธ์ง, ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง์ ๋ํด ์์๋ณด์
velog.io
https://ittrue.tistory.com/227
[Spring] ์คํ๋ง ์์กด์ฑ ์ฃผ์ (DI : Dependency Injection) 4๊ฐ์ง ๋ฐฉ๋ฒ (์์กด ๊ด๊ณ ์๋ ์ฃผ์ )
์์กด์ฑ ์ฃผ์ (DI : Dependency Injection) ๋ฐฉ๋ฒ ์์กด์ฑ ์ฃผ์ ์๋ 4๊ฐ์ง ๋ฐฉ๋ฒ์ด ์กด์ฌํ๋ค. ์์ฑ์ ์ฃผ์ ์์ ์ ์ฃผ์ (setter ์ฃผ์ ) ํ๋ ์ฃผ์ ์ผ๋ฐ ๋ฉ์๋ ์ฃผ์ ์คํ๋ง ๊ณต์ ๋ฌธ์์์๋ ์์ฑ์ ์ฃผ์ ์ ๊ถ์ฅํ
ittrue.tistory.com
https://m42-orion.tistory.com/98
[Spring] ์คํ๋ง ์ปจํ ์ด๋ - ApplicationContext, IoC, DI
์ด ๊ธ์ ๊ณต๋ถ๋ฅผ ํ๋ฉด์ ์๊ฒ ๋ ๋ด์ฉ๋ค์ ๊ธฐ๋กํ๋ ๊ธ ์ ๋๋ค. ์ค๋ฅ๋ ๊ณ ์ณ์ผ ํ ์ฌํญ๋ค์ด ์๋ค๋ฉด ์ง์ ๋ถํ๋๋ฆฝ๋๋ค! 1. IoC๋? 2. DI๋? 3. ApplicationContext : ์คํ๋ง ์ปจํ ์ด๋๋? 4. ์คํ๋ง ์ปจํ ์ด๋
m42-orion.tistory.com
https://lucas-owner.tistory.com/39
[Spring] Spring-Container, IoC, DI, Singleton ๊ฐ๋ ์ ๋ฆฌ
๋ชฉ์ฐจ IoC ๋? Spring Container (์คํ๋ง ์ปจํ ์ด๋) DI(Dependency Injection) ๋? Singleton ํจํด์ด๋? IoC ๋? IoC ๋ Inversion Of Control์ ์ฝ์์ด๋ฉฐ, ์ ์ด์ ์ญ์ ์ด๋ผ๊ณ ํ๋ค. ํ๋ก๊ทธ๋๋จธ์ ํ๋ ์์ํฌ์ ์ฃผ์ฒด๊ฐ ๋ฐ๋
lucas-owner.tistory.com
'๐ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Spring] 05.์คํ๋ง ORM๊ณผ JPA - Hibernate (11) | 2025.07.24 |
|---|---|
| [Spring] 04.MVC ๋? - MVC ๊ตฌ์กฐ ์ดํด (6) | 2025.07.22 |
| [Spring] 02.AOP๊ฐ๋ , Aspectํน์ง ์ด์ ๋ฆฌ (1) | 2024.01.04 |
| [Spring] 01.@Autowired,@Resource,@Inject ์ฐจ์ด (6) | 2023.11.20 |