[Spring Framework] DI Applicationμ λ§λ€μ΄λ³΄μ.
μ΄λ² κΈμ μμ€ μ½λλ SKplanet Tacademy - Spring Framework Basic λμμ κ°μ μλ£μ μλ μμ€μ½λλ₯Ό κ°μ Έλ€κ° μ¬μ©νμ΅λλ€.
μ°λ¦¬λ μ§λ μκ° IoC 컨ν μ΄λμ DIμ λν κΈ°λ³Έμ μΈ κ°λ κ³Ό μ©μ΄λ€μ λν΄μ μμ보μλ€.
μ΄μ μ€λμ Ioc Container(DI Container)λ₯Ό μ΄λ»κ² Springμμ μ¬μ©νλμ§ μμ보μ.
- POJO ν΄λμ€ μμ±
- μ€μ λ©νμ 보 XML μμ±
- DI ν μ€νΈ ν΄λμ€ μμ±
POJO ν΄λμ€μ λ€μ΄μ΄κ·Έλ¨
μ°λ¦¬κ° μμ λ‘ κ΅¬μ±ν POJOν΄λμ€μ λ€μ΄μ΄κ·Έλ¨μ μλμ κ°μ ννλ‘ λνλΌ μ μλ€.
Hello
.classPrinter
.interfaceStringPrinter
.classConsolePrinter
.classbeans.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μ ν
μ€νΈ ν΄λμ€μ΄λ€.
ν μ€νΈμ μμλ λ€μκ³Ό κ°λ€.
- Create IoC Container
- Get Hello Bean
- 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μ λν΄μ μμ보μ.