더북(TheBook)

예제 2-14 XML기반 스프링 애플리케이션 구성 파일([[hello-world]] app-context.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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="provider"
        class="com.apress.prospring5.ch2.decoupled.HelloWorldMessageProvider"/>

  <bean id="renderer"
        class="com.apress.prospring5.ch2.decoupled.StandardOutMessageRenderer"
        p:messageProvider-ref="provider"/>
</beans>

 

이 파일은 전형적인 스프링 ApplicationContext 구성 파일입니다. 먼저 스프링에서 사용하는 네임스페이스를 선언했는데 기본 네임스페이스는 beans입니다. 이 beans 네임스페이스는 스프링이 관리하는 빈을 정의하는 데 사용합니다. 또한, 이 빈들 사이의 의존성(앞서 설명한 팩터리 예제에서 renderer 빈의 messageProvider 속성은 provider 빈을 참조합니다) 요구사항을 정의하는 데에도 사용됩니다. 스프링이 이 파일을 해석(resolve)해 의존성을 주입할 것입니다.

그 후, provider 빈의 ID와 해당 구현 클래스를 정의합니다. ApplicationContext를 초기화할 때 스프링이 이 빈 정의를 만나면 클래스의 인스턴스를 생성한 뒤 이를 지정된 ID로 저장합니다.

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.