ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring Core] 공식문서 공부 - 1.2.2 Instantiating a Container
    JAVA/Spring 2022. 8. 21. 15:02

    https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-introduction

     

    Core Technologies

    In the preceding scenario, using @Autowired works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig, how do

    docs.spring.io

     

    이번 글에서는 Spring IoC 컨테이너의 인스턴스화에 대해서 다룬다.

     

    ApplicationContext 인터페이스를 구현한 구현체들의 생성자(constructor)는 위치 경로(location path)를 매개변수로 받는다.

    IoC 컨테이너가 이렇게 위치 경로를 매개변수로 전달 받으면 해당 경로로 부터 구성 메타데이터를 불러오게된다.

    위치 경로에는 로컬 파일 시스템 경로, 자바 클래스패스(CLASSPATH) 등 다양한 형태의 리소스를 입력할 수 있다.

     

    ApplicationContext의 구현체 중 하나인 ClassPathXmlApplicationContext 클래스를 인스턴스화 하는 예제를 보자.

    ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

    ClassPathXmlApplicationContext 클래스의 생성자에 위치 경로(services.xml, daos.xml)를 인수로 넘겨주고,

    생성된 인스턴스를 ApplicationContext 형태로 반환 받는 것을 확인할 수 있다.

    더보기
    Figure 1. ApplicationContext API 문서

    실제 공식 API 문서를 보면, ApplicationContext를 구현한 구현체들의 목록을 확인할 수 있다.

    위에서도 언급했지만 IoC 컨테이너가 ApplicationContext의 구현체들로 부터 다양한 형태로 리소스를 제공 받을 수 있다는 것을 다시한번 확인할 수 있었다.

     

    다음으로 공식문서에서는 "services.xml", "daos.xml" 파일을 아래와 같이 작성하였다.

    <!-- services.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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!-- services -->
    
        <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
            <property name="accountDao" ref="accountDao"/>
            <property name="itemDao" ref="itemDao"/>
            <!-- additional collaborators and configuration for this bean go here -->
        </bean>
    
        <!-- more bean definitions for services go here -->
    
    </beans>
    <!-- daos.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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="accountDao"
            class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
            <!-- additional collaborators and configuration for this bean go here -->
        </bean>
    
        <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
            <!-- additional collaborators and configuration for this bean go here -->
        </bean>
    
        <!-- more bean definitions for data access objects go here -->
    
    </beans>

    정리해보면 PetStoreServiceImpl 클래스는 accountDao, itemDao 2가지 타입의 데이터 접근 객체(Data Access Object)를 가지고있다.

     

    아래는 "services.xml", "daos.xml"의 일부분만 따온 것이다.

    <!-- services.xml -->
    
    <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
        <property name="accountDao" ref="accountDao"/>
        <property name="itemDao" ref="itemDao"/>
    </bean>
    
    
    <!-- daos.xml -->
    
    <bean id="accountDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">
    </bean>
    
    <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">
    </bean>

    "services.xml" 부분을 보면 petStore id를 가진 Bean 정의 내에 <property name="..." ref="..."> 형태로 협력 객체(collaborating object)들이 정의되어 있는것을 볼 수 있는데,

    이 <property> 태그 내의 name 속성이 JavaBean 프로퍼티의 이름(name)을 나타낸다.

    또한, ref 속성의 경우는 다른 bean 정의의 이름(name)을 나타낸다.

     

    "daos.xml" 부분을 보면 bean 정의 내에 id 속성과

    방금 말한 <property> 태그 내에 ref 속성이 서로 연결되는 것을 알 수 있다.

    (name 속성의 경우는 지금 예시에서는 같지만 PetStoreServiceImpl 클래스의 프로퍼티 값에 따라 달라질 수도 있다.)

     

    이러한 연결점이 협력 객체들 사이의 의존성(dependency)을 표현한다고 한다.

    이에 관한 자세한 내용은 Dependencies 챕터에서 다룬다.

     


    Composing XML-based Configuration Metadata

    때로는 여러개의 XML 파일들로부터 Bean 정의들을 가져오는 것이 유용할 수도 있다.

    앞에서 ApplicationContext의 구현체 생성자에 2개의 위치 경로를 넣어 인스턴스화 하는 예시처럼

    생성자에 여러개의 위치 경로들을 전달하는 방식으로도 할 수 있고,

    XML 파일내에 <import /> 요소를 통해서도 할 수 있다.

     

    아래는 <import /> 요소를 통한 방식의 예시이다.

    <beans>
        <import resource="services.xml"/>
        <import resource="resources/messageSource.xml"/>
        <import resource="/resources/themeSource.xml"/>
    
        <bean id="bean1" class="..."/>
        <bean id="bean2" class="..."/>
    </beans>

    만약 <import /> 요소를 사용하지 않고 생성자에 전달하는 방식을 사용한다면 아래와 같을 것이다.

    ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "resources/messageSource.xml", "/resources/themeSource.xml");

    <import /> 요소를 사용하는 방식이나 생성자에 전달하는 방식이나 크게 차이는 없는거 같다.

     


    The Groovy Bean Definition DSL

    이 부분은 아직은 공부하기에 이른거 같아서 나중에 정리하기로 했다.

     

     

     

    댓글

Designed by Tistory.