[Spring] 외부 설정 프로퍼티


외부 설정 프로퍼티


1. 외부 설정 프로퍼티

PropertyPlaceholderConfigurer 클래스를 빈으로 등록하면 외부의 프로퍼티에 저장된 정보를 스프링 설정 파일에서 사용할 수 있다.

예를 들어, 다음과 같은 프로퍼티 파일을 작성했다고 하자.

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://dbserver:3306/springbook

jdbc.username=springbook

jdbc.password=springbook

위 프로퍼티의 파일의 정보를 스프링 설정 파일에서 사용하고 싶다면, 다음과 같이 PropertyPlaceholderConfigurer 클래스를 빈으로 등록한 뒤, 프로퍼티의 이름을 파일에서 사용하면 된다.


<bean class="org.springframwork.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<!-- 프로퍼티 파일 경로 지정 -->

<value>classpath:config/jdbc.properties</value>

</property>

</bean>


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destory-method="close">

<property name="driverClassName" value="${jdbc.driver}"/>

<property name="url" value="${jdbc.url}">

<property name="username" value="${jdbc.username}/>

<property name="password" value="${jdbc.password}/>

</bean>

locations 프로퍼티의 값에는 콤마나 공백으로 구분된 프로퍼티 파일 목록이 오며, 프로퍼티 파일에 포함된 프로퍼티의 값은 

'${프로퍼티의 이름}' 형식으로 사용할 수 있다. 즉, 위 코드에서 ${jdbc.driver}는 프로퍼티 파일의 jdbc.driver 프로퍼티의 값으로 대체된다.


 한 개 이상의 프로퍼티 파일을 지정하면 <list> 태그를 이용하여 프로퍼티 목록을 지정해주면 된다.

<bean class="org.springframwork.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>classpath:config/jdbc.properties</value>

<value>classpath:config/monitor.properties</value>

</list>

</property>

</bean>

외부 환경에 따라 설정 정보가 변경되는 경우에 프로퍼티 파일을 유용하게 사용할 수 있다.

예를 들어, 로컬에서의 테스트와 테스트 서버에서의 통합 테스트, 그리고 실제 운영 환경에서 사용하는 데이터베이스가 다르다고 하자.

이 경우 각 환경에 맞게 스프링 설정 파일을 변경하는 것 보다는 환경에 맞는 프로퍼티 설정 파일을 작성하고, 

환경에 따라 알맞은 프로퍼티 파일을 사용하도록 배치 스크립트를 작성하는 것이 유지 및 보수에 더 편리하다.


2. <context:property-placeholder> 태그를 사용한 외부 설정 프로퍼티 사용

<context:property-placeholder> 태그를 사용하여 외부 프로퍼티 파일을 로딩하도록 설정할 수 있다.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

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-3.0.xsd">

  http://www.springframework.org/schema/context 

    http://www.springframework.org/schema/beans/spring-context-3.0.xsd

  <!--두 개이상 프로퍼티 사용할 수 있다.(콤마로 구분하여)-->

<context-property-placeholder location="classpath:config/jdbc.properties, classpath:config/monitor.properties"/>


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destory-method="close">

<property name="driverCalssName" value="${jdbc.driver}">/>

</bean>

....


</beans>

<context:property-placeholder> 태그를 사용하려면 위 코드와 같이 context 접두어에 대한 네임스페이스를 http://www.springframework.org/schema/context로 지정하고 네임스페이스와 관련된 XML 스키마 경로를 

spring-context-3.0.xsd로 지정해 주어야 한다.


3. PropertyPlaceholderConfigurer 사용시 주의 사항

PropertyPlaceholderConfigurer를 사용할 때 주의할 점은 두 개 이상의 PropertyPlaceholderConfigurer 빈을 설정하면 안 된다는 점이다.

이 경우, 첫 번째 PropertyPlaceholderConfigurer의 설정이 적용되며, 두 번째 설정 내용은 적용되지 않는다.

--> 각각의 PropertyPlaceholderConfigurer 빈은 서로 프로퍼티 정보를 공유하지 않기 때문이다.

스프링을 이용하다보면 두 개 이상의 설정 파일을 사용하는 경우가 빈번한데, 이때 서로 다른 파일에 PropertyPlaceholderConfigurer 빈이 설정되어 있을 경우 프로퍼티 값이 올바르게

처리되지 않아서 올바르게 처리되지 않아서 예외가 발생할 수 있다.




'Programing > Spring' 카테고리의 다른 글

[Spring] 자바 코드 기반 설정  (0) 2014.12.17
[Spring] 어노테이션 기반 설정  (0) 2014.12.17
[Spring] iBatis 2  (0) 2014.12.16
[Spring] iBatis 1  (0) 2014.12.16
[Spring] web.xml 기본 설정  (1) 2014.12.13