i. Beans Auto Wiring - kimxavi/spring_tutorial GitHub Wiki

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ์™€ ์—†์ด ๊ด€๊ณ„๋ฅผ autowireํ•  ์ˆ˜ ์žˆ๋‹ค.

Autowiring Modes

bean ์— autowire mode๋ฅผ ๋ช…์‹œํ•ด์ค€๋‹ค.

  • no : default. ์•„๋ฌด autowire ๊ฐ€ ์—†๋‹ค.
  • autodetect : constructor๋กœ ์ฒซ ์‹œ๋„ ํ›„ ์•ˆ ๋˜๋ฉด byType ์‹œ๋„ํ•จ.
  • byName
  • byType
  • constructor

byType ๋‚˜ constructor๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๋ฐฐ์—ด์ด๋‚˜ ๋‹ค๋ฅธ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

byName

property name์„ ๊ฐ€์ง€๊ณ  Autowiring. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ XML ์„ค์ • ํŒŒ์ผ์— byName๋กœ auto-wire ๊ฐ’์ด ์„ค์ •๋œ bean ๋“ค์„ ์ฐพ๋Š”๋‹ค. ๊ทธ ๋‹ค์Œ์— ์„ค์ • ํŒŒ์ผ์— ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ bean์„ ์ฃผ์ž…ํ•ด์ค€๋‹ค. ๋งŒ์•ฝ ์—†๋‹ค๋ฉด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, spellChecker์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์งˆ ๋•Œ, spellChecker์˜ ์ด๋ฆ„์„ ๊ฐ€์ง„ bean์„ ์ฐพ๊ณ  ์„ธํŒ…ํ•ด์ค€๋‹ค.

<?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" />
      <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

์œ„์˜ XML์„ autowire๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

<?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" 
      autowire="byName">
      <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

byType

property type์„ ๊ฐ€์ง€๊ณ  autowiring ํ•ด์ค€๋‹ค. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” byType๋กœ autowire ๊ฐ’์ด ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ์ฐพ๊ณ  ๊ทธ ๋‹ค์Œ์— type ๋งค์น˜๋ฅผ ํ†ตํ•ด์„œ ๊ฐ’์„ ์„ค์ •ํ•ด์ค€๋‹ค. ์—†์„ ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ฐœ์ƒ. ์˜ˆ๋ฅผ ๋“ค์–ด, spellChecker์˜ ํƒ€์ž…์ด SpellChecker ์ผ ๊ฒฝ์šฐ์— bean์˜ ์ •์˜๊ฐ€ SpellChecker์ธ ๊ฒƒ์„ ์ฐพ๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๊ฒƒ์„ ์ฃผ์ž…ํ•ด์ค€๋‹ค.

<?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" />
      <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</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" 
      autowire="byType">
      <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

์ด๋ ‡๊ฒŒ ๋ณ€ํ™˜ ์‹œํ‚ค๋ฉด ๋œ๋‹ค. bean์˜ id ๊ฐ’์ด spellChecker ์—์„œ ํƒ€์ž…์„ ๋ช…์‹œํ•˜์—ฌ SpellChecker๋กœ ๋ฐ”๋€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

constructor

์ด๊ฒƒ์€ byType๊ณผ ์œ ์‚ฌํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๊ฒƒ์€ ์ƒ์„ฑ์ž์— ๊ณต๊ธ‰ํ•ด์ค€๋‹ค.

TextEditor.java

package com.tutorialspoint;

public class TextEditor {
   private SpellChecker spellChecker;
   private String name;

   public TextEditor( SpellChecker spellChecker, String name ) {
      this.spellChecker = spellChecker;
      this.name = name;
   }
   public SpellChecker getSpellChecker() {
      return spellChecker;
   }
   public String getName() {
      return name;
   }

   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}

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" 
      autowire="constructor">
      <constructor-arg value="Generic Text Editor"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

Limitations with autowiring

autowiring์€ ํ”„๋กœ์ ํŠธ์—์„œ ์ผ๊ด€๋˜๊ฒŒ ์‚ฌ์šฉ๋  ๋•Œ ๊ฐ€์žฅ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ autowiring์„ ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, ๊ฐœ๋ฐœ์ž์—๊ฒŒ ํ˜ผ๋™์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ œํ•œ์ ๊ณผ ๋‹จ์ ์„ ์•Œ๊ณ  ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • Overriding possibility : ์™€ ๋ฅผ ๋ช…์‹œํ•จ์œผ๋กœ์จ autowiring์„ ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ๋‹ค.
  • Primitive data types : primitives, Strings, and Classes ์™€ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ ํ”„๋กœํผํ‹ฐ์—๋Š” ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  • Confusing nature : ๋ช…์‹œ์ ์œผ๋กœ wiring ํ•ด์ฃผ๋Š” ๊ฒƒ๋ณด๋‹ค ๋ถ€์ •ํ™•ํ•˜๋‹ค.
โš ๏ธ **GitHub.com Fallback** โš ๏ธ