본문 바로가기
강의 실습/스프링 핵심 원리 - 기본편

다양한 설정 형식 지원 - 자바 코드, XML

by jint 2026. 4. 11.

스프링 컨테이너는 다양한 형식의 설정 정보를 받아들일 수 있게 유연하게 설계되어 있음 : 자바 코드, XML, Groovy 등

 

다양한 설정 형식 지원 - 자바 코드, XML 등



1. 어노테이션 기반 자바 코드 설정 사용
AnnotationConfigApplicationContext 를 사용하면서 자바 코드로 된 설정 정보를 매개변수로 넘긴다.
-> new AnnotationConfigApplicationContext(AppConfig.class);


2. XML 설정 사용
최근엔 스프링 부트를 많이 사용하면서 XML 기반 설정은 잘 사용하지 않지만, 아직 많은 레거시 프로젝트들은 XML로 되어 있다.
XML을 사용하면 컴파일 없이 빈 설정 정보를 변경할 수 있는 장점도 있다. (.xml 파일만 교체하면 됨)
GenericXmlApplicationContext 를 사용하면서 .xml 설정 파일을 매개변수로 넘긴다.

1) XmlAppConfig 사용 자바 코드
- src/test/java/hello/core/xml/XmlAppContext.java

package hello.core.xml;

import hello.core.member.MemberService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

import static org.assertj.core.api.Assertions.*;

public class XmlAppContext {

    @Test
    void xmlAppContext() {
        ApplicationContext ac = new GenericXmlApplicationContext("appConfig.xml"); // classpath 에 있는 것을 자동으로 읽어 동작
        MemberService memberService = ac.getBean("memberService", MemberService.class);
        assertThat(memberService).isInstanceOf(MemberService.class);
    }

}


2) xml 기반의 스프링 빈 설정 정보
- src/main/resources/appConfig.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.xsd">

    <bean id="memberService" class="hello.core.member.MemberServiceImpl">
        <constructor-arg name="memberRepository" ref="memberRepository"/>
    </bean>

    <bean id="orderService" class="hello.core.order.OrderServiceImpl">
        <constructor-arg name="memberRepository" ref="memberRepository"/>
        <constructor-arg name="discountPolicy" ref="discountPolicy"/>
    </bean>

    <bean id="memberRepository" class="hello.core.member.MemoryMemberRepository"/>

    <bean id="discountPolicy" class="hello.core.discount.RateDiscountPolicy"/>

</beans>


· xml 기반의 appConfig.xml 스프링 설정 정보와 자바 코드로 된 AppConfig.java 설정 정보를 비교하면 거의 비슷하다.
· xml 기반 설정은 최근에 잘 사용하지 않는다. 필요하면 스프링 공식 레퍼런스 문서를 확인한다.
스프링 공식 레퍼런스 문서 : https://spring.io/projects/spring-framework

 

Spring Framework

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuse

spring.io



참고링크 : https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%95%B5%EC%8B%AC-%EC%9B%90%EB%A6%AC-%EA%B8%B0%EB%B3%B8%ED%8E%B8?cid=325969

 

스프링 핵심 원리 - 기본편| 김영한 - 인프런 강의

현재 평점 5.0점 수강생 49,538명인 강의를 만나보세요. 스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다. 스프링 기본 기능, 스프

www.inflearn.com

댓글