[Spring] Inversion of Control

Inversion of Control

์ œ์–ด์˜ ์—ญ์ „(Inversion of Control) -> IoC์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

์ œ์–ด

์ œ์–ด์˜ ์˜๋ฏธ๋ฅผ ์•Œ์•„๋ณด์ž.
ํ•œ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ์—์„œ ๋‹ค๋ฅธ ๊ตฌํ˜„์ฒด์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ ๋‹ค๊ณ  ํ•ด๋ณด์ž.

public class InstanceClass {
    public void run(){
        System.out.println("run!");
    }
}
public class Test {

    public static void setTest() {
        InstanceClass instanceClass = new InstanceClass();
        instanceClass.run();
    }

    public static void main(String[] args){
        setTest();
    }
}

Test๋ผ๋Š” ํด๋ž˜์Šค์—์„œ new ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.
์ด๋Ÿฐ ์ƒํ™ฉ์ด Test ํด๋ž˜์Šค์˜ setTest() ๋ฉ”์„œ๋“œ๊ฐ€ InstanceClass๋ฅผ ์ œ์–ดํ•˜๊ณ  ์žˆ๋Š” ์ƒํ™ฉ์ด๋‹ค.

public class Test {

    public static void setTest() {
        InstanceClass instanceClass = new InstanceClass();
        instanceClass.run();
    }

    public static void setTest1() {
        InstanceClass instanceClass = new InstanceClass();
        instanceClass.run();
    }

    public static void setTest2() {
        InstanceClass instanceClass = new InstanceClass();
        instanceClass.run();
    }

    public static void setTest3() {
        InstanceClass instanceClass = new InstanceClass();
        instanceClass.run();
    }

    public static void main(String[] args){
        setTest();
        setTest1();
        setTest2();
        setTest3();
    }
}

๋งŒ์•ฝ์— ์ด๋Ÿฐ ์‹์œผ๋กœ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ์—์„œ new ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด ์„œ๋กœ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค์ผ๊นŒ?
๋‹ต์€ ์„œ๋กœ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค์ด๋‹ค.
๊ฐ ์Šคํƒ ์˜์—ญ์—์„œ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  ๊ทธ ์•ˆ์—์„œ ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด heap ์˜์—ญ์—์„œ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.
์ด heap ์˜์—ญ์—์„œ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋Š” ์Šคํƒ ์˜์—ญ์ด ์†Œ๋ฉธํ•  ๋•Œ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ์— ์˜ํ•ด ๊ฐ™์ด ์†Œ๋ฉธ๋˜๋ฏ€๋กœ, setTest,1,2,3์˜ ์ธ์Šคํ„ด์Šค๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค์ด๋‹ค.

Spring IoC

DB์— ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š” ํด๋ž˜์Šค๋“ค, ์ปค๋„ฅ์…˜ ํ’€, ์Šค๋ ˆ๋“œ ํ’€๊ณผ ๊ฐ™์€ ์ž์›์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ํด๋ž˜์Šค๋“ค์ด ์ธ์Šคํ„ด์Šค๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ๋งŒ๋“ค๋ฉด, ์ž์›์˜ ๋‚ญ๋น„๊ฐ€ ๋˜๊ณ  ์˜ˆ์ƒํ•  ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
์ด์™ธ์—๋„ ์„œ๋ฒ„์—์„œ ์‚ฌ์šฉ๋˜๋Š” Service, Repository ๊ฐ์ฒด๋„ ์ค‘๋ณต์œผ๋กœ ์—ฌ๋Ÿฌ ๋ฒˆ ๋งŒ๋“œ๋Š” ๊ฒƒ๋ณด๋‹ค ์ธ์Šคํ„ด์Šค๋ฅผ ํ•œ ๋ฒˆ๋งŒ ์ƒ์„ฑํ•ด์„œ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์•ˆ์ •์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค.

Spring์—์„œ๋Š” Spring IoC(Inversion of Control)์˜ ๋ฐฉ์‹์„ ํ†ตํ•ด ์ด๋Ÿฌํ•œ ๊ด€๋ฆฌ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.
์ฆ‰, ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•ด ์ฃผ๋Š” ์ œ์–ด๋ฅผ Spring ์ชฝ์œผ๋กœ ์—ญ์ „์‹œ์ผœ ์Šคํ”„๋ง์ด ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
์Šคํ”„๋ง์ด ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜(@Component, @Repository, @Service ๋“ฑ๋“ฑ..)์„ ์ž…๋ ฅํ•ด ์ฃผ๋ฉด ์Šคํ”„๋ง์ด IoC๋กœ ๊ด€๋ฆฌํ•ด ์ค€๋‹ค.
์ด๋•Œ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ์‹ฑ๊ธ€ํ„ด์œผ๋กœ ๊ด€๋ฆฌํ•ด ์ฃผ๋ฏ€๋กœ ํด๋ž˜์Šค๊ฐ€ ๋งŒ๋“œ๋Š” ์ธ์Šคํ„ด์Šค๋“ค์ด ์ค‘๋ณต๋˜์ง€ ์•Š๊ณ , ํ•œ ๊ตฌํ˜„์ฒด์— ํ•œ์ธ ์Šคํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์ค˜์„œ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ Spirng์ด ์ธ์‹ํ•œ ๊ตฌํ˜„์ฒด๋“ค์ด ํ•˜๋‚˜์”ฉ heap ์˜์—ญ์— ๋งŒ๋“ค์–ด์ ธ์„œ ๊ด€๋ฆฌ๋œ๋‹ค.

์˜ˆ์ œ

import org.springframework.stereotype.Component;

@Component
public class InstanceClass {
    public void run(){
        System.out.println("run!");
    }
}
import org.springframework.beans.factory.annotation.Autowired;

public class Test {
    @Autowired
    private InstanceClass instanceClass;

    public void test(){
        instanceClass.run();
    }
}

IoC๊ฐ€ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๊ฒŒ @Component์™€ ๊ฐ™์€ ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ์ค€๋‹ค.
์ด๋•Œ ํด๋ž˜์Šค์˜ ์œ„์น˜๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ ๋ฉ”์ธ ํด๋ž˜์Šค์˜ ํ•˜์œ„์—ฌ์•ผ ์Šคํ”„๋ง IoC๊ฐ€ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋‹ค.
IoC์— ์ œ์–ด๊ฐ€ ์—ญ์ „๋œ ๊ตฌํ˜„์ฒด๋Š” ์œ„์™€ ๊ฐ™์€ @Autowired์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
๋˜ํ•œ ์ด ์ธ์Šคํ„ด์Šค๋Š” ์‹ฑ๊ธ€ํ„ด์œผ๋กœ ๊ด€๋ฆฌ๋˜๋Š” ๋‹จ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋‹ค.