โ€ข 5 min read โ€ข

[Spring๐ŸŒฑ] XML Configuration ๐Ÿ“„

Spring Boot์—์„œ XML์„ ์‚ฌ์šฉํ•œ ์„ค์ • ๋ฐฉ๋ฒ•๊ณผ ์–ด๋…ธํ…Œ์ด์…˜๊ณผ์˜ ๋น„๊ต

Author: qyinm
[Spring๐ŸŒฑ] XML Configuration ๐Ÿ“„

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 ์ค‘ ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•ด์„œ ์„ค์ •์„ ์ผ๊ด€์„ฑ ์žˆ๊ฒŒ ์œ ์ง€. ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์„ ํ˜ผ์šฉํ•˜๋ฉด ์„ค์ •์˜ ๋ณต์žก์„ฑ์ด ์ฆ๊ฐ€ํ•˜๊ณ  ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์–ด๋ ค์›Œ์งˆ ์ˆ˜ ์žˆ๋‹ค.

์ฐธ๊ณ ์ž๋ฃŒ ๐Ÿ“š