๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’Š Java & Kotlin & Spring/- spring framework +

[Spring Framework] DI Application์„ ๋งŒ๋“ค์–ด๋ณด์ž.

by Wonit 2020. 9. 5.
์ด๋ฒˆ ๊ธ€์˜ ์†Œ์Šค ์ฝ”๋“œ๋Š” SKplanet Tacademy - Spring Framework Basic ๋™์˜์ƒ ๊ฐ•์˜ ์ž๋ฃŒ์— ์žˆ๋Š” ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ๋‹ค๊ฐ€ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ์ง€๋‚œ ์‹œ๊ฐ„ IoC ์ปจํ…Œ์ด๋„ˆ์™€ DI์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋…๊ณผ ์šฉ์–ด๋“ค์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋‹ค.


์ด์ œ ์˜ค๋Š˜์€ Ioc Container(DI Container)๋ฅผ ์–ด๋–ป๊ฒŒ Spring์—์„œ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์•Œ์•„๋ณด์ž.

  • POJO ํด๋ž˜์Šค ์ž‘์„ฑ
  • ์„ค์ • ๋ฉ”ํƒ€์ •๋ณด XML ์ž‘์„ฑ
  • DI ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค ์ž‘์„ฑ

POJO ํด๋ž˜์Šค์˜ ๋‹ค์ด์–ด๊ทธ๋žจ

์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ œ๋กœ ๊ตฌ์„ฑํ•  POJOํด๋ž˜์Šค์˜ ๋‹ค์ด์–ด๊ทธ๋žจ์€ ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

  • Hello.class
  • Printer.interface
  • StringPrinter.class
  • ConsolePrinter.class
  • beans.xml :: XML ์„ค์ • ํŒŒ์ผ๋กœ

Hello ํด๋ž˜์Šค๋Š” StringPrinter๋‚˜ ConsolePrinter๋ฅผ ์ง์ ‘ ์˜์กดํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, Printer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์˜์กดํ•˜๋Š” ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•œ๋‹ค.


๊ทธ๋ฆฌ๊ณ  XML ์„ค์ • ํŒŒ์ผ์—์„œ Hello ํด๋ž˜์Šค๊ฐ€ ์–ด๋–ค Printer๋ฅผ ์˜์กดํ•˜๋Š”์ง€๋ฅผ ๋ช…์‹œํ•ด ์ค„ ๊ฒƒ์ด๋‹ค.

 

POJO ํด๋ž˜์Šค ์ž‘์„ฑ

Hello.class

public class Hello {

    private String name; // value type
    private Printer printer; // ref type

    public Hello(){}

    /* ์œ„์— ์กด์žฌํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ setter ๋ฉ”์„œ๋“œ */
    public void setName(String name) {
        this.name = name;
    }

    /* ์œ„์— ์กด์žฌํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ setter ๋ฉ”์„œ๋“œ */
    public void setPrinter(Printer printer) {
        this.printer = printer;
    }


    String sayHello() {
        return "Hello " + name;
    }

    public void print() {
        this.printer.print(sayHello());
    }
}

Printer.interface

public interface Printer {
    void print(String message);
}

StringPrinter

public class StringPrinter implements Printer{
    private StringBuffer sb = new StringBuffer();

    @Override
    public void print(String message) {
        this.sb.append(message);
    }

    public String toString() {
        return this.sb.toString();
    }
}

ConsolePrinter

public class ConsolePrinter implements Printer{

    @Override
    public void print(String message) {
        System.out.println(message);
    }
}

beans.xml

<?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="hello" class="Hello">
        <property name="name" value="Spring"/>
        <property name="printer" ref="printer"/>
    </bean>

    <bean id="printer" class="StringPrinter" />
    <bean id="consolePrinter" class="ConsolePrinter" />
</beans>

ํ˜„์ œ DI๋ฅผ setter Injection์œผ๋กœ ์ง€์ •์ด ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— <property> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

HelloBeanTest.class

HelloBeanTest ๋Š” Hello Bean์˜ ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค์ด๋‹ค.

ํ…Œ์ŠคํŠธ์˜ ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. Create IoC Container
  2. Get Hello Bean
  3. Get StringPrinter Bean

์ด์ œ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.

public class HelloBeanTest {  
public static void main(String\[\] args) {
    /*IOC Container*/
    ApplicationContext context = new GenericXmlApplicationContext("resource/beans.xml");

    Hello hello = (Hello) context.getBean("hello"); // XML ์˜ Hello
    System.out.println(hello.sayHello());
    hello.print();

    Printer printer = (Printer)context.getBean("printer"); // XML ์˜ StringPrinter
    System.out.println(printer.toString());

    Hello hello2 = context.getBean("hello", Hello.class); // XML ์˜ Hello
    hello2.print();

    System.out.println(hello == hello2);
}
}  

๊ทธ๋Ÿผ ๊ฒฐ๊ณผ๋กœ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋œ๋‹ค.

 

์—ฌ๊ธฐ์„œ ๋ง ํ•˜๋Š” true๋Š” Spring์ด SingleTon์ด๋ผ๋Š” ๊ฒƒ์„ ์ฆ๋ช…ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋œ๋‹ค.

 


๋‹ค์Œ ์‹œ๊ฐ„์—๋Š” SingleTon์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

๋Œ“๊ธ€