XML Configuration ๐ ๏ธ
XML์ ์ฌ์ฉํด Bean์ ์ค์ ํ๋ ๊ฒ์ Annotation์ด ์๋ ์์ ์ ์ฌ์ฉ๋์ผ๋ฉฐ ์ต๊ทผ์๋ ๊ฑฐ์ ์ฌ์ฉํ์ง ์๋๋ค.
ํ์ง๋ง Legacy ์ฝ๋๋ฅผ ๋ณด๊ธฐ ์ํด ๋ณผ ์๋ ์์ผ๋ ์ดํด๊ฐ ํ์ํ๋ค.
XML Configuration ์์ ์ฝ๋ ๐
Java ์ฝ๋
package com.hippoo.studyspring.examples.h1;
import com.hippoo.studyspring.game.GameRunner;
import java.util.Arrays;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlConfigurationContextLauncherApplication {
public static void main(String[] args) {
try (var context = new ClassPathXmlApplicationContext("contextConfiguration.xml")) {
Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);
System.out.println(context.getBean("name"));
System.out.println(context.getBean("age"));
context.getBean(GameRunner.class).run();
}
}
}
XML ์ค์ ํ์ผ
์์น: resources/contextConfiguration.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"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="name" class="java.lang.String">
<constructor-arg value="Ranga"/>
</bean>
<bean id="age" class="java.lang.Integer">
<constructor-arg value="35"/>
</bean>
<context:component-scan base-package="com.hippoo.studyspring.game"/>
<bean id="game" class="com.hippoo.studyspring.game.PacmanGame"/>
<bean id="gameRunner" class="com.hippoo.studyspring.game.GameRunner">
<constructor-arg ref="game" />
</bean>
</beans>
์ค๋ช ๐
XML ํ์ผ์ ํตํด ์คํ๋ง ๋น์ ์ค์ ํ ์ ์์ผ๋ฉฐ, <bean> ํ๊ทธ๋ฅผ ์ฌ์ฉํ์ฌ ๋น์ ์์ฑํ๋ค. ์ด๋ @Bean, @Component ๋ฑ์ ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ๋ ๊ฒ๊ณผ ๋์ผํ ์ญํ ์ ํ๋ฉฐ, ์์ฑ์ ์ธ์๋ก ๊ฐ์ฒด๋ฅผ ๋๊ธธ ๋๋ ref ์ต์
์ ์ถ๊ฐํ์ฌ ์์กด์ฑ์ ์ฃผ์
ํ๋ค.
์์ ์๋ @Configuration, @Component, @Bean๊ณผ ๊ฐ์ ์ด๋
ธํ
์ด์
์ด ์์๊ธฐ ๋๋ฌธ์ XML๋ก ๋ชจ๋ ์ค์ ์ ํ๋ค. ์ต๊ทผ์ ๋ง๋ค์ด์ง ์ฝ๋๋ค์ ์ค์ ์ ์ด๋
ธํ
์ด์
์ผ๋ก ์ฃผ๋ก ์ฌ์ฉํ์ง๋ง, ์ฌ์ ํ ๊ธฐ์กด์ XML ์ค์ ์ ์ฌ์ฉํ๋ ํ๋ก์ ํธ๋ค์ด ๋ง๊ธฐ ๋๋ฌธ์ ์ด๋ฅผ ์ดํด๊ฐ ํ์ํ๋ค.
์ด๋ ธํ ์ด์ ๊ณผ ๋น๊ตํ ๐
์๋ ํ๋ XML ์ค์ ๊ณผ ์ด๋ ธํ ์ด์ ๊ธฐ๋ฐ ์ค์ ์ ์ฃผ์ ์ฐจ์ด์ ์ ๋น๊ตํ ๊ฒ.
| ํญ๋ชฉ | XML ์ค์ | ์ด๋ ธํ ์ด์ ๊ธฐ๋ฐ ์ค์ |
|---|---|---|
| ์ค์ ๋ฐฉ์ | XML ํ์ผ์ ๋น ์ ์ | ์ฝ๋ ๋ด ์ด๋ ธํ ์ด์ ์ฌ์ฉ |
| ๊ฐ๋ ์ฑ | ์ค์ ํ์ผ์ด ๊ธธ์ด์ง ์ ์์ | ์ฝ๋์ ์ค์ ์ด ๋ฐ์ ํ๊ฒ ์ฐ๋๋์ด ๊ฐ๋ ์ฑ์ด ์ข์ |
| ์ ์ง๋ณด์์ฑ | ์ค์ ๋ณ๊ฒฝ ์ XML ํ์ผ ์์ ํ์ | ์ฝ๋ ๋ณ๊ฒฝ๊ณผ ๋์์ ์ค์ ๋ ๋ณ๊ฒฝ ๊ฐ๋ฅ |
| ์์กด์ฑ ์ฃผ์ | ref๋ value ์์ฑ ์ฌ์ฉ | ์์ฑ์, ํ๋, ์ธํฐ๋ฅผ ํตํ ์ฃผ์ |
| ์ ์ฐ์ฑ | ๋ค์ํ ์ค์ ์ XML์์ ์ฝ๊ฒ ๊ด๋ฆฌ ๊ฐ๋ฅ | ์ด๋ ธํ ์ด์ ์ ํตํด ๋์ ์ผ๋ก ์ค์ ๊ฐ๋ฅ |
| ๋๋ฒ๊น | XML ์ค์ ์ค๋ฅ ์ ๋ฐํ์์์ ๋ฐ๊ฒฌ ๊ฐ๋ฅ | ์ปดํ์ผ ํ์์ ์ผ๋ถ ์ค๋ฅ ๋ฐ๊ฒฌ ๊ฐ๋ฅ |
| ํ์ต ๊ณก์ | XML ๋ฌธ๋ฒ์ ๋ํ ์ดํด ํ์ | ์๋ฐ ์ด๋ ธํ ์ด์ ์ ๋ํ ์ดํด ํ์ |
| ํ๊ฒฝ ์ค์ ๋ถ๋ฆฌ | ์ค์ ๊ณผ ์ฝ๋๊ฐ ๋ช ํํ ๋ถ๋ฆฌ๋จ | ์ค์ ๊ณผ ์ฝ๋๊ฐ ํจ๊ป ์กด์ฌํจ |
Pros & Cons โ๏ธ
Pros โ
- ์์ ์๋ฐ๋ก POJO ์ ์ง: XML ์ค์ ์ ์์ ์๋ฐ ๊ฐ์ฒด๋ฅผ ์ ์งํ ์ ์์ด ํ ์คํธ๊ฐ ์ฉ์ดํฉ๋๋ค.
- ๋๋ฒ๊น ์ด ์กฐ๊ธ ๋ ์ฝ๋ค: ์ด๋ ธํ ์ด์ ์ ๊ฒฝ์ฐ ์คํ๋ง ํ๋ ์์ํฌ๊ฐ ์์์ ์ค์ ์ ์ฒ๋ฆฌํ๊ธฐ ๋๋ฌธ์ ํ๋ ์์ํฌ์ ๋์์ ๊น์ด ์ดํดํด์ผ ํ์ง๋ง, XML ์ค์ ์ ์ค์ ํ์ผ์ ์ง์ ๋ณด๊ณ ์ดํดํ ์ ์์ต๋๋ค.
Cons โ
- ์ ์ง๋ณด์๊ฐ ํ๋ค๋ค: XML ์ค์ ํ์ผ์ด ์ปค์ง์๋ก ๊ด๋ฆฌํ๊ธฐ ์ด๋ ค์์ง๋๋ค.
- ์ฅํฉํ๋ค: ์ด๋ ธํ ์ด์ ์ ๋นํด ์ค์ ์ด ๊ธธ์ด์ ธ ์ฝ๋๊ฐ ์ฅํฉํด์ง ์ ์์ต๋๋ค.
TIP ๐ก
Annotation๊ณผ XML ์ค ํ๋๋ง ์ฌ์ฉํด์ ์ค์ ์ ์ผ๊ด์ฑ ์๊ฒ ์ ์ง. ๋ ๊ฐ์ง ๋ฐฉ์์ ํผ์ฉํ๋ฉด ์ค์ ์ ๋ณต์ก์ฑ์ด ์ฆ๊ฐํ๊ณ ์ ์ง๋ณด์๊ฐ ์ด๋ ค์์ง ์ ์๋ค.