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 Dependency Injection

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>

PRINT

Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.

Constructor arguments resolution

ํ•˜๋‚˜ ์ด์ƒ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์ „๋‹ฌ ๋  ๋•Œ ์• ๋งค๋ชจํ˜ธํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ์• ๋งค๋ชจํ˜ธํ•จ์„ ์—†์• ๊ธฐ ์œ„ํ•ด์„œ ์ƒ์„ฑ์ž์˜ ์ธ์ž ์ˆœ์„œ๋ฅผ 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 Dependency Injection

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 ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

XML Configuration using p-namespace

๋งŒ์•ฝ ๋งŽ์€ 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ํ•œ ์š”์†Œ๋“ค์„ ์„ธํŒ…ํ•˜๋Š” ๊ทœ์น™์„ ์“ฐ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

Injecting Inner Beans

์ž๋ฐ”์˜ 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>

PRINT

Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.

Injecting Collection

  • 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>

PRINT

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}

Injecting Bean References

์•„๋ž˜๋Š” ์–ด๋–ป๊ฒŒ 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>

Injecting null and empty string values

<bean id="..." class="exampleBean">
   <property name="email" value=""/>
</bean>

empty("") ๊ฐ’์„ ์ „๋‹ฌํ•œ๋‹ค.

<bean id="..." class="exampleBean">
   <property name="email"><null/></property>
</bean>

null ๊ฐ’์„ ์ „๋‹ฌํ•œ๋‹ค.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ