๐Ÿ’ก ์ด ํฌ์ŠคํŒ…์€ ์Šคํ”„๋ง ์Šคํ„ฐ๋””์— ๋งž๊ฒŒ ๊ตฌ์„ฑ๋œ ๊ฒƒ์ด๋ฉฐ, ์ถ”ํ›„ ๊ธ€์˜ ๊ตฌ์กฐ์™€ ๋‹ค๋ฃจ๋Š” ๋ฒ”์œ„๊ฐ€ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ  ๋ฐ”๋ž€๋‹ค.

IoC(Inversion of Control, ์ œ์–ด์˜ ์—ญ์ „)

์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑ, ๊ด€๊ณ„์„ค์ •, ์‚ฌ์šฉ, ์ œ๊ฑฐ ๋“ฑ ์˜ค๋ธŒ์ ํŠธ ์ „๋ฐ˜์— ๊ฑธ์นœ ๋ชจ๋“  ์ œ์–ด๊ถŒ์„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๊ฐ–๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ํ”„๋ ˆ์ž„์›Œํฌ์˜ ์ปจํ…Œ์ด๋„ˆ์—๊ฒŒ ๋„˜๊ธฐ๋Š” ๊ฐœ๋…์„ ๋งํ•œ๋‹ค. ์ฐธ๊ณ ๋กœ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ ๋งŒ์˜ ๊ฐœ๋…์ด ์•„๋‹ˆ๋‹ค.

์Šคํ”„๋ง์—์„œ๋Š” ์ € '์˜ค๋ธŒ์ ํŠธ'๋ฅผ '๋นˆ(Bean)'์ด๋ผ๊ณ  ์นญํ•œ๋‹ค.

 

๐Ÿ’ก ๋นˆ(Bean)์— ๋Œ€ํ•ด์„œ๋Š” ์ถ”ํ›„ ์—ฌ๋Ÿฌ๋ฒˆ์— ๊ฑธ์ณ ๋ณ„๋„๋กœ ํฌ์ŠคํŒ…ํ• ํ…Œ๋‹ˆ ์ฐธ๊ณ  ๋ฐ”๋ž€๋‹ค.

 

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด BestStar ์ƒ์„ฑ์ž์—์„œ Star ํด๋ž˜์Šค์™€์˜ ์˜์กด๊ด€๊ณ„๋ฅผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‹จ์—์„œ ์ง์ ‘ ์„ค์ •ํ•˜๊ณ  ์žˆ๋‹ค.

 

๐Ÿ’ก ์˜์กด๊ด€๊ณ„
์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด๊ฐ„์— ๋ ˆํผ๋Ÿฐ์Šค ์ฐธ์กฐ๊ฐ€ ๋˜์–ด ์žˆ๋‹ค๋Š” ๋ง์ด๋‹ค. 

public class BestStar {

    private Star star;

    public BestStar()
        star = new Star();
        // BestStar ์ƒ์„ฑ์ž์—์„œ Star ํด๋ž˜์Šค์™€์˜ ์˜์กด๊ด€๊ณ„๋ฅผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‹จ์—์„œ ์ง์ ‘ ์„ค์ •ํ•˜๊ณ  ์žˆ๋‹ค.
    }
}

๊ทธ๋Ÿฐ๋ฐ ์Šคํ”„๋ง์ด ์ œ๊ณตํ•˜๋Š” @Autowired ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์˜์กด๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•ด์ฃผ๋Š” ์ฝ”๋“œ๊ฐ€ ์‚ฌ๋ผ์ง€๊ณ  ํ”„๋ ˆ์ž„์›Œํฌ์— BestStar, Star ์˜ค๋ธŒ์ ํŠธ ์˜์กด๊ด€๊ณ„์˜ ์ œ์–ด๊ถŒ์„ ๋งก๊ธธ ์ˆ˜ ์žˆ๊ฒŒ ๋œ ๊ฒƒ์ด๋‹ค.

public class BestStar {

    @Autowired
    private Star star;

}

 

๐Ÿ’ก ์ถ”ํ›„ @Autowired ์–ด๋…ธํ…Œ์ด์…˜์ด ์Šคํ”„๋ง์—์„œ ์–ด๋–ป๊ฒŒ ์ถ”์ƒํ™”๋ผ์žˆ๊ณ  ๊ทธ ๋‚ด๋ถ€์—์„œ ์–ด๋–ค ๋™์ž‘์›๋ฆฌ๋ฅผ ๊ฐ–๋Š”์ง€ ๋””๋ฒ„๊น… ์Šคํƒ ํŠธ๋ ˆ์ด์‹ฑ์„ ํ•ด๋ณด๋ฉฐ ๋ถ„์„ํ•œ ๊ธ€์„ ํฌ์ŠคํŒ… ํ• ํ…Œ๋‹ˆ ์ฐธ๊ณ ๋ฐ”๋ž€๋‹ค.

๋จผ์ € ๊ถ๊ธˆํ•˜์‹  ๋ถ„๋“ค์€ Github์— ํ† ์ดํ”„๋กœ์ ํŠธ๋กœ ์ •๋ฆฌํ•œ ์ด ๋งํฌ๋ฅผ ์ฐธ๊ณ ๋ฐ”๋ž€๋‹ค.

 

IoC๊ฐ€ ํ•„์š”ํ•œ ์ด์œ ? - ๊ฐ์ฒด์ง€ํ–ฅ ์›์น™์„ ์ž˜ ์ง€ํ‚ค๊ธฐ ์œ„ํ•ด

๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์™€ ๋‚ด๊ฐ€ ๊ตฌํ˜„ ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ถ€๋ถ„์œผ๋กœ ์—ญํ• ๊ณผ ๊ด€์‹ฌ์„ ๋ถ„๋ฆฌํ•ด ์‘์ง‘๋„๋ฅผ ๋†’์ด๊ณ  ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๋ฉฐ, ์ด์— ๋”ฐ๋ผ ๋ณ€๊ฒฝ์— ์œ ์—ฐํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๊ฐ€ ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ œ์–ด๋ฅผ ์—ญ์ „ํ•œ ๊ฒƒ์ด๋‹ค.

 

๐Ÿ’ก ์ถ”ํ›„ ๊ฐ์ฒด์ง€ํ–ฅ 3์š”์†Œ, 5์›์น™์— ๋Œ€ํ•ด ๋ณ„๋„๋กœ ํฌ์ŠคํŒ… ํ›„ ๋งํฌ๋ฅผ ๊ฑธ์–ด๋‘˜ํ…Œ๋‹ˆ ์ฐธ๊ณ  ๋ฐ”๋ž€๋‹ค. 


DI(Dependency Injection, ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…)

์™ธ๋ถ€๋กœ๋ถ€ํ„ฐ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ๊ฐ€์žˆ๋Š” ์ธ์Šคํ„ด์Šค์˜ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค ํƒ€์ž…์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์˜์กด๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•˜๋Š”๊ฒƒ์„ ๋งํ•œ๋‹ค. ์Šคํ”„๋ง์—์„  IoC๋ผ๋Š” ์šฉ์–ด๋งŒ ๊ฐ€์ง€๊ณ ๋Š” ๊ฐœ๋…์ด ๋„ˆ๋ฌด ์ถ”์ƒ์ ์ด๋ผ ๊ทธ ํ•ต์‹ฌ์„ ์งš๋Š” ์šฉ์–ด๊ฐ€ ํ•„์š”ํ–ˆ๋Š”๋ฐ, ์ด๋•Œ ๋ช‡๋ช‡ ์‚ฌ๋žŒ๋“ค์˜ ์ œ์•ˆ์œผ๋กœ ๋งŒ๋“  ์šฉ์–ด๊ฐ€ ๋ฐ”๋กœ DI์ธ ๊ฒƒ์ด๋‹ค.


์˜์กด๊ด€๊ณ„ ์ฃผ์ž… ๋ฐฉ๋ฒ•

ํ•„๋“œ๋ฅผ ์ด์šฉํ•œ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž… (Field Injection)

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private CourseService courseService;

    @Override
    public void studentMethod() {
        courseService.courseMethod();
    }

}

 

setter() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•œ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž… (Setter Injection)

@Service
public class StudentServiceImpl implements StudentService {

    private CourseService courseService;

    @Autowired
    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }

    @Override
    public void studentMethod() {
        courseService.courseMethod();
    }
}

 

 

์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•œ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž… (Constructor Injection)

@Service
public class StudentServiceImpl implements StudentService {

    private final CourseService courseService;

    @Autowired
    public StudentServiceImpl(CourseService courseService) {
        this.courseService = courseService;
    }

    @Override
    public void studentMethod() {
        courseService.courseMethod();
    }
}

 

๐Ÿ’ก ์ถ”ํ›„ ๊ฐ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž… ๋ฐฉ์‹์„ ๋น„๊ตํ•˜๋ฉฐ ๊ฒฐ๊ตญ์—๋Š” ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด์„œ ์ถ”๊ฐ€ ์„ค๋ช…ํ• ํ…Œ๋‹ˆ ์ฐธ๊ณ  ๋ฐ”๋ž€๋‹ค.


์ถœ์ฒ˜

์˜์กด๊ด€๊ณ„ ์ฃผ์ž…

martinfowler.com/articles/injection.html

 

์ƒ์„ฑ์ž ์ฃผ์ž…์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ 

yaboong.github.io/spring/2019/08/29/why-field-injection-is-bad/

'Spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์Šคํ”„๋ง์˜ MVC  (0) 2021.06.15
@Autowired์˜ ๋™์ž‘์›๋ฆฌ  (1) 2021.05.12
์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ(BeanFactory, ApplicationContext)  (1) 2021.05.06
POJO(Plain Old Java Object)  (0) 2021.04.28