skip to content
Q
Table of Contents

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 쀑 ν•˜λ‚˜λ§Œ μ‚¬μš©ν•΄μ„œ 섀정을 일관성 있게 μœ μ§€. 두 κ°€μ§€ 방식을 ν˜Όμš©ν•˜λ©΄ μ„€μ •μ˜ λ³΅μž‘μ„±μ΄ μ¦κ°€ν•˜κ³  μœ μ§€λ³΄μˆ˜κ°€ μ–΄λ €μ›Œμ§ˆ 수 μžˆλ‹€.

참고자료 πŸ“š