Spring 프레임워크 기반하에서 컴포넌트 관리 전략

Toggle Space Navigation Tree
Space Map

필요성

Spring 프레임워크를 이용하여 컴포넌트 기반의 개발을 진행할 경우 설정파일을 어떻게 관리하는 것이 효율적일지에 대하여 항상 고민하게 된다. 최근에 일하고 있는 프로젝트에서도 빈 이름이 같은 상황이 발생하는 상황이 발생했다. 이 같은 상황이 발생하는 것을 피하기 위하여 컴포넌트 기반으로 애플리케이션을 개발할 때 Spring 프레임워크 설정파일을 어떻게 관리하느냐는 중요한 이슈가 된다.

해결 방법

Java Development with the Spring Framework 책을 읽던 중 좋은 해결책이 있어 공개한다. 이 책에서 제시하는 방법은 간단하다. 각 컴포넌트의 설정파일에서 컴포넌트 이름을 Namespace 형태로 사용하자는 것이다. 예를 들어 모든 빈 이름에 "componentname-, componentname:"와 같은 접두사를 붙여 사용하자는 것이다. 지금 생각해보면 참 쉬운 해결책이 있었음에도 불구하고 고민하지 않은 결과가 지금 프로젝트와 같은 상황을 만들어버렸다.

예를 통하여 이 같은 문제를 어떻게 해결하고 있는지 살펴보도록 하자. "devices" 모듈을 개발하고 있으며, "devices" 모듈은 DataSource에 레퍼런스를 가진다고 할 경우 다음과 같이 설정하는 것이 가능하다.

devices 모듈의 Spring 프레임워크 설정파일
<beans>
    <bean id="devices:deviceDescriptorDataMapper" class="ch04.sampleX.devices.DeviceDescriptorDataMapperImpl">
        <property name="dataSource" ref="devices:dataSource"/>
    </bean>

    <bean id="devices:deviceMapper" class="ch04.sampleX.devices.DeviceMapperImpl">
        <property name="dataSource" ref="devices:dataSource"/>
    </bean>
</beans>

"user" 모듈 또한 DataSource에 대한 레퍼런스를 가지며, 이에 대한 Spring 프레임워크 파일은 다음과 같이 구현하는 것이 가능하다.

user 모듈의 Spring 프레임워크 설정파일
<beans>
    <bean id="users:userMapper" class="ch04.sampleX.users.UserMapperImpl">
        <property name="dataSource" ref="users:dataSource"/>
    </bean>

    <bean id="users:roleMapper" class="ch04.sampleX.users.RoleMapperImpl">
        <property name="dataSource" ref="users:dataSource"/>
    </bean>
</beans>

위와 같이 "devices" 모듈과 "users" 모듈에서 관리하고 있는 클래스들에 대한 설정 정보를 포함하여 jar로 압축한 다음 배포하게 된다. jar로 압축하여 배포한 컴포넌트들은 이 API를 사용하는 곳에서 다음과 같이 활용하는 것이 가능하게 된다.

devices 모듈과 users 모듈을 사용하는 API에 대한 Spring 프레임워크 설정파일
<beans>
    <bean id="app:userDeviceService" class="ch04.sampleX.users.UserDeviceServiceImpl">
        <property name="userMapper" ref="users:userMapper"/>
        <property name="deviceMapper" ref="devices:deviceMapper"/>
    </bean>

    <bean id="app:dataSource" name="users:dataSource, devices:dataSource" class="org.apache.commons.decp.BasicDataSource" destroy-method="close">
        ....
    </bean>
</beans>

위 설정파일에서 볼 수 있듯이 공통적으로 사용하는 외부 리소스와 같은 Spring 설정파일은 Spring 프레임워크의 name 속성을 이용하여 해결하고 있는 것을 확인할 수 있다. 그 동안 name 속성에 대하여 그다지 많은 활용을 하지 않았는데 위 예에서와 같이 멋드러지게 사용하고 있는 것을 확인할 수 있다. 이 얼마나 멋지지 않은가?

참고문서

  • Java Development with the Spring Framework, 97Page. Strageis for Handling Components
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.