i. Beans Auto Wiring - kimxavi/spring_tutorial GitHub Wiki
์คํ๋ง ์ปจํ ์ด๋๋ ์ ์์ด ๊ด๊ณ๋ฅผ autowireํ ์ ์๋ค.
bean ์ autowire mode๋ฅผ ๋ช ์ํด์ค๋ค.
- no : default. ์๋ฌด autowire ๊ฐ ์๋ค.
- autodetect : constructor๋ก ์ฒซ ์๋ ํ ์ ๋๋ฉด byType ์๋ํจ.
- byName
- byType
- constructor
byType ๋ constructor๋ฅผ ์ฌ์ฉํจ์ผ๋ก์จ ๋ฐฐ์ด์ด๋ ๋ค๋ฅธ ์ปฌ๋ ์ ์ ์ฌ์ฉํ ์ ์๋ค.
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>
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๋ก ๋ฐ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
์ด๊ฒ์ 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>
autowiring์ ํ๋ก์ ํธ์์ ์ผ๊ด๋๊ฒ ์ฌ์ฉ๋ ๋ ๊ฐ์ฅ ์ ์๋ํฉ๋๋ค. ๋ง์ฝ autowiring์ ์ ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ, ๊ฐ๋ฐ์์๊ฒ ํผ๋์ ์ค ์ ์์ต๋๋ค. ์ ํ์ ๊ณผ ๋จ์ ์ ์๊ณ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
- Overriding possibility : ์ ๋ฅผ ๋ช ์ํจ์ผ๋ก์จ autowiring์ ๋ฎ์ด์ธ ์ ์๋ค.
- Primitive data types : primitives, Strings, and Classes ์ ๊ฐ์ ๊ฐ๋จํ ํ๋กํผํฐ์๋ ์ฌ์ฉ์ด ๋ถ๊ฐํฉ๋๋ค.
- Confusing nature : ๋ช ์์ ์ผ๋ก wiring ํด์ฃผ๋ ๊ฒ๋ณด๋ค ๋ถ์ ํํ๋ค.