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

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
스프링 핵심 원리 - 기본편| 김영한 - 인프런 강의
현재 평점 5.0점 수강생 49,538명인 강의를 만나보세요. 스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다. 스프링 기본 기능, 스프
www.inflearn.com
'강의 실습 > 스프링 핵심 원리 - 기본편' 카테고리의 다른 글
| 스프링 빈 설정 메타 정보 - BeanDefinition (0) | 2026.04.12 |
|---|---|
| BeanFactory와 ApplicationContext (0) | 2026.04.11 |
| 스프링 빈 조회 - 상속 관계 (1) | 2026.04.10 |
| 스프링 빈 조회 - 동일한 타입이 둘 이상 (0) | 2026.04.09 |
| 스프링 빈 조회 - 기본 (0) | 2026.04.08 |
댓글