h. Dependency Injection - kimxavi/spring_tutorial GitHub Wiki
๋ชจ๋ ์๋ฐ ์ดํ๋ฆฌ์ผ์ด์ ์ ํจ๊ป ์๋ํ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ์ง๋ค. ๋ณต์กํ ์๋ฐ ์ดํ๋ฆฌ์ผ์ด์ ์ ์์ฑํ ๋, ์ฌ์ฌ์ฉ์ฑ์ ์ฆ๋ํ๊ณ ๋จ์ ํ ์คํธ ์์ ๋ค๋ฅธ ํด๋์ค๋ค์ ๋ ๋ฆฝ์ ์ด๊ฒ ํ๊ธฐ ์ํด์ ์ดํ๋ฆฌ์ผ์ด์ ํด๋์ค๋ ๋ค๋ฅธ ์๋ฐ ํด๋์ค๋ค์ ๋ํด ๊ฐ๋ฅํ๋ฉด ๋ ๋ฆฝ์ ์ด์ฌ์ผ ํ๋ค. Dependency Injection(wiring) ์ ๊ทธ ํด๋์ค๋ค์ด ์๋ก ๊ฒฐํฉ๋๋ ๊ฒ์ ๋๋๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ์ ์๊ฐ ๋์ ๊ทธ๋ค์ด ๋ ๋ฆฝ์ด๋๋ก ๋๋๋ค.
์๋ฅผ ๋ค์ด, ํ ์คํธ ์๋ํฐ ์ปดํผ๋ํธ๋ฅผ ๊ฐ์ง๋ ์ดํ๋ฆฌ์ผ์ด์ ์ด ์๋ค๊ณ ํ์. ๊ทธ๋ฆฌ๊ณ ๋ง์ถค๋ฒ ๊ฒ์ฌ๋ฅผ ์ ๊ณตํ๋ค๊ณ ํ์.
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor() {
spellChecker = new SpellChecker();
}
}
TextEditor ์ SpellChecker ๊ฐ์ ์์กด์ฑ์ด ์๊ฒผ๋ค. inversion of control(IoC) ์์๋ ์ด๋ ๊ฒ ํ ์ ์๋ค.
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
}
TextEditor๊ฐ SpellChecker์ ๊ตฌํ์ ๋ํด ๊ฑฑ์ ํ ํ์๊ฐ ์์ด์ก๋ค. SpellChecker๋ ๋ ๋ฆฝ์ ์ผ๋ก ๊ตฌํ๋๊ณ TextEditor๊ฐ ์ธ์คํด์คํํ ๋ ์ ๊ณต๋ ๊ฒ์ด๋ค. ์ด ๊ฒ๋ค์ ์คํ๋ง์ด ์ ์ดํ ๊ฒ์ด๋ค. TextEditor๋ก ๋ถํฐ ๋ชจ๋ ์ ์ด๋ฅผ ์ ๊ฑฐํ๊ณ ๊ทธ ๊ฒ์ XML configuration file์ ๊ฐ์ ์ด๋ค ๋ค๋ฅธ ๊ฒ์ด ๊ฐ์ง๋ค ๊ทธ๋ฆฌ๊ณ ๊ทธ ์์กด์ฑ์ ์์ฑ์์ ์ํด ์ฃผ์ ๋๋ค. ์ด๋ฌํ ์ ์ด์ ํ๋ฆ์์ Dependency Injection (DI)์ ์ดํด ๋ฐ์ ๋๋ค. ํจ๊ณผ์ ์ผ๋ก ์ผ๋ถ ์ธ๋ถ ์์คํ ์ ์์กด์ฑ์ ์์ ํ ๋๋ฌธ์ด๋ค.
๋ ๋ฒ์งธ ๋ฐฉ๋ฒ์ TextEditor ์ Setter Methods๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
Constructor-based DI ๋ ์ปจํ ์ด๋๊ฐ ๋ค๋ฅธ ํด๋์ค์ ์์กด์ฑ์ด ์๋ ์ธ์๋ฅผ ๊ฐ์ง ์์ฑ์๋ฅผ ํธ์ถํ ๋ ๋ฐ์ํ๋ค.
TextEditor.java
package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker) {
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
SpellChecker.java
package com.tutorialspoint;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
}
MainApp.java
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
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-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<constructor-arg ref="spellChecker"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.
ํ๋ ์ด์์ ํ๋ผ๋ฏธํฐ๊ฐ ์ ๋ฌ ๋ ๋ ์ ๋งค๋ชจํธํ ๋๊ฐ ์๋ค. ์ด๋ฌํ ์ ๋งค๋ชจํธํจ์ ์์ ๊ธฐ ์ํด์ ์์ฑ์์ ์ธ์ ์์๋ฅผ bean ์ ์์ ์ค์ ํ๋ค.
package x.y;
public class Foo {
public Foo(Bar bar, Baz baz) {
// ...
}
}
<beans>
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
</bean>
<bean id="bar" class="x.y.Bar"/>
<bean id="baz" class="x.y.Baz"/>
</beans>
์์๋๋ก ์ ๋ฌ
package x.y;
public class Foo {
public Foo(int year, String name) {
// ...
}
}
<beans>
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="2001"/>
<constructor-arg type="java.lang.String" value="Zara"/>
</bean>
</beans>
ํ์ ์ ๋งค์นํด์ค๋ค.
<beans>
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="2001"/>
<constructor-arg index="1" value="Zara"/>
</bean>
</beans>
๋ง์ง๋ง์ผ๋ก ์ ์ผ ์ข์ ๋ฐฉ๋ฒ์ index๋ฅผ ์ฃผ๋ ๊ฒ์ด๋ค. value๋ฅผ ํตํด ๊ฐ์ ์ง์ ๋ฃ์ด์ค ์๊ฐ ์๋ค.
Setter-based DI๋ no-argument ์์ฑ์๋ static ํฉํ ๋ฆฌ ๋ฉ์๋๊ฐ bean์ ์ธ์คํด์คํํ ํ์, ์ปจํ ์ด๋๊ฐ bean์ setter๋ฉ์๋๋ฅผ ํธ์ถํจ์ผ๋ก์จ ์ฑ์ทจ๋๋ค. TextEditor.java
package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
// a setter method to inject the dependency.
public void setSpellChecker(SpellChecker spellChecker) {
System.out.println("Inside setSpellChecker." );
this.spellChecker = spellChecker;
}
// a getter method to return spellChecker
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
spellChecker ๋ณ์์ผ ๋ setSpellChecker() ์ ๊ฐ์ด ์ธํ ํด์ผ๋๋ค. ๋ค์ด๋ฐ์ ์ฃผ์ํ์.
SpellChecker.java
package com.tutorialspoint;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
}
MainApp.java
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
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-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<property name="spellChecker" ref="spellChecker"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
ํ๊ทธ ์์ ๋ฅผ ์ฐ๋ ๊ฒ ์์ฑ์ ๋ฐฉ์๊ณผ ๋ค๋ฅด๋ค. ref ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์จ์ ๊ฐ์ฒด์ ๋ ํผ๋ฐ์ค๋ฅผ ์ ๋ฌํ๋ค. ๋ง์ฝ ๊ฐ์ ๋ฐ๋ก ์ ๋ฌํ๋ ค๋ฉด value ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์ฌ์ฉํ๋ค.
๋ง์ฝ ๋ง์ setter๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ p-namespace๋ฅผ ์ฐ๋ ๊ฒ ํธ๋ฆฌํ๋ค.
<?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-3.0.xsd">
<bean id="john-classic" class="com.example.Person">
<property name="name" value="John Doe"/>
<property name="spouse" ref="jane"/>
</bean>
<bean name="jane" class="com.example.Person">
<property name="name" value="John Doe"/>
</bean>
</beans>
์์ ์์๋ฅผ
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="john-classic" class="com.example.Person"
p:name="John Doe"
p:spouse-ref="jane"/>
</bean>
<bean name="jane" class="com.example.Person"
p:name="John Doe"/>
</bean>
</beans>
p-namespace ๋ฅผ ์ฌ์ฉํด์ ๋ณํํ๋ค. -ref๋ฅผ ํตํด ์ง์ ๊ฐ์ ์ฃผ๋ ๋ฐฉ์์ ์๋์ง๋ง ๋ค๋ฅธ ๋ ํผ๋ฐ์ค๋ฅผ ์ฐธ๊ณ ํ๋ค.
์์ฑ์ ๊ธฐ๋ฐ๊ณผ setter ๊ธฐ๋ฐ์ ํจ๊ป ์ฌ์ฉํ ์๋ ์๋ค. ๊ทธ๋ด ๋ ์์ฑ์๋ ํ์ ์์๋ค์ ์ธํ ํ๊ณ setter๋ optionalํ ์์๋ค์ ์ธํ ํ๋ ๊ท์น์ ์ฐ๋ ๊ฒ์ด ์ข๋ค.
์๋ฐ์ innner class์ ์ ์ฌํ๊ฒ inner beans ๋ ํด๋์ค์ ๋ฒ์๋ด์์ ์ ์๋๋ค.
TextEditor.java
package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
// a setter method to inject the dependency.
public void setSpellChecker(SpellChecker spellChecker) {
System.out.println("Inside setSpellChecker." );
this.spellChecker = spellChecker;
}
// a getter method to return spellChecker
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
SpellChecker.java
package com.tutorialspoint;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling(){
System.out.println("Inside checkSpelling." );
}
}
MainApp.java
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
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-3.0.xsd">
<!-- Definition for textEditor bean using inner bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<property name="spellChecker">
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker"/>
</property>
</bean>
</beans>
Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.
- list : ๊ฐ์ ๋ฆฌ์คํธ๋ฅผ ์ฃผ์ ํ๋ค. ์ค๋ณต ํ์ฉ.
- set : set ๊ฐ ๋ค์ ์ฃผ์ ํ๋ค. ์ค๋ณต ๋ถ๊ฐ.
- map : name-value ์ ์ปฌ๋ ์ . ์ด๋ค ํ์ ์ด๋ ๋ฃ์ ์ ์์.
- props : name-value ์ ์ปฌ๋ ์ . name, value ๋ชจ๋ string.
์ปฌ๋ ์ ๊ฐ์ ์ง์ ๋ฃ์ด์ฃผ๋๊ฐ. ์ปฌ๋ ์ ์ ๋ ํผ๋ฐ์ค bean์ ref ๋ก ์ ๋ฌํ ์ ์๋ค. JavaCollection.java
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
List addressList;
Set addressSet;
Map addressMap;
Properties addressProp;
// a setter method to set List
public void setAddressList(List addressList) {
this.addressList = addressList;
}
// prints and returns all the elements of the list.
public List getAddressList() {
System.out.println("List Elements :" + addressList);
return addressList;
}
// a setter method to set Set
public void setAddressSet(Set addressSet) {
this.addressSet = addressSet;
}
// prints and returns all the elements of the Set.
public Set getAddressSet() {
System.out.println("Set Elements :" + addressSet);
return addressSet;
}
// a setter method to set Map
public void setAddressMap(Map addressMap) {
this.addressMap = addressMap;
}
// prints and returns all the elements of the Map.
public Map getAddressMap() {
System.out.println("Map Elements :" + addressMap);
return addressMap;
}
// a setter method to set Property
public void setAddressProp(Properties addressProp) {
this.addressProp = addressProp;
}
// prints and returns all the elements of the Property.
public Properties getAddressProp() {
System.out.println("Property Elements :" + addressProp);
return addressProp;
}
}
MainApp.java
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressList();
jc.getAddressSet();
jc.getAddressMap();
jc.getAddressProp();
}
}
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-3.0.xsd">
<!-- Definition for javaCollection -->
<bean id="javaCollection" class="com.tutorialspoint.JavaCollection">
<!-- results in a setAddressList(java.util.List) call -->
<property name="addressList">
<list>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</list>
</property>
<!-- results in a setAddressSet(java.util.Set) call -->
<property name="addressSet">
<set>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</set>
</property>
<!-- results in a setAddressMap(java.util.Map) call -->
<property name="addressMap">
<map>
<entry key="1" value="INDIA"/>
<entry key="2" value="Pakistan"/>
<entry key="3" value="USA"/>
<entry key="4" value="USA"/>
</map>
</property>
<!-- results in a setAddressProp(java.util.Properties) call -->
<property name="addressProp">
<props>
<prop key="one">INDIA</prop>
<prop key="two">Pakistan</prop>
<prop key="three">USA</prop>
<prop key="four">USA</prop>
</props>
</property>
</bean>
</beans>
List Elements :[INDIA, Pakistan, USA, USA]
Set Elements :[INDIA, Pakistan, USA]
ap Elements :{1=INDIA, 2=Pakistan, 3=USA, 4=USA}
Property Elements :{two=Pakistan, one=INDIA, three=USA, four=USA}
์๋๋ ์ด๋ป๊ฒ bean ๋ ํผ๋ฐ์ค๋ฅผ ํ๋์ ์ปฌ๋ ์ ์์๋ก ์ฃผ์ ํ๋์ง ๋ํ๋ด๋ ์์์ด๋ค. ๋ ํผ๋ฐ์ค์ value๋ฅผ ํผ์ฉํด์ ์ธ ์ ์๋ค. setter ๋ฉ์๋๋ฅผ ๋ ํผ๋ฐ์ค๋ฅผ ๋ค๋ฃฐ ์ ์๋๋ก ๊ตฌํํด์ค์ผ ํ๋ค.
<?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-3.0.xsd">
<!-- Bean Definition to handle references and values -->
<bean id="..." class="...">
<!-- Passing bean reference for java.util.List -->
<property name="addressList">
<list>
<ref bean="address1"/>
<ref bean="address2"/>
<value>Pakistan</value>
</list>
</property>
<!-- Passing bean reference for java.util.Set -->
<property name="addressSet">
<set>
<ref bean="address1"/>
<ref bean="address2"/>
<value>Pakistan</value>
</set>
</property>
<!-- Passing bean reference for java.util.Map -->
<property name="addressMap">
<map>
<entry key="one" value="INDIA"/>
<entry key ="two" value-ref="address1"/>
<entry key ="three" value-ref="address2"/>
</map>
</property>
</bean>
</beans>
<bean id="..." class="exampleBean">
<property name="email" value=""/>
</bean>
empty("") ๊ฐ์ ์ ๋ฌํ๋ค.
<bean id="..." class="exampleBean">
<property name="email"><null/></property>
</bean>
null ๊ฐ์ ์ ๋ฌํ๋ค.