XML 설정 알아보기

Soo·2024년 3월 5일

Java 어노테이션을 사용하여 설정파일을 구성하기 전에는 XML을 사용해서 Spring Bean을 정의하고 사용하였습니다.

이번 기록에서는 XML을 사용하여 Spring Bean을 설정하고 조회하는 법을 정리하겠습니다.

XML 설정파일 만들기

https://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/xsd-configuration.html 에 들어가서 context schema를 검색하시면 아래 코드를 찾을 수 있습니다.

<?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 definitions here -->
</beans>

XML 설정 파일을 만드셨다면 Spring 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"
       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 definitions here -->

    <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.in28minutes.learnspringframework.game"/>

</beans>

Bean을 등록하기 위해 태그를 사용합니다.

  • id : 빈의 이름으로 등록이됩니다.
  • class : 빈으로 등록할 class를 지정합니다.

<constructor-arg> : 생성자 인자를 전달받습니다.

  • 위 코드에서는 “Ranga”를 문자열로 입력받습니다.

<context:component-scan base-package="com.in28minutes.learnspringframework.game"/>

: base-package에 지정된 경로의 모든 컴포넌트를 등록합니다.

모든 컴포넌트를 등록하지 않고 한 개씩 Bean으로 등록하고 싶다면 태그를 사용해서 등록이 가능합니다.

<bean id="game" class="com.in28minutes.learnspringframework.game.PacmanGame"></bean>

class 속성에 등록하고 싶은 class의 package 경로를 적어주고 마지막에 class이름을 적습니다. id 속성을 사용해서 bean의 이름을 등록합니다.

전체 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 definitions here -->

    <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.in28minutes.learnspringframework.game"/>-->

    <bean id="game" class="com.in28minutes.learnspringframework.game.PacmanGame"></bean>
</beans>

XML로 정의한 빈을 사용하기 위한 클래스를 생성합니다.

XmlConfigurationContextLauncherApplication.class

xml 파일을 사용하기위해 ClassPathXmlApplicationContext() 클래스를 사용합니다. 매개변수로 xml 설정 파일의 이름을 넣어줍니다.

package com.in28minutes.learnspringframework.example.h1;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;

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"));
        }
    }
}

결과

xml 파일로 Bean 등록했던 name, age, game과 생성자 인수로 받은 Ranga, 35가 출력됩니다.

0개의 댓글