파란하늘의 지식창고
반응형

applicationContext에서 properties를 사용하기 위해 PropertyPlaceholderConfigurer 를 제공한다.

사용 방법은 아주 편하게 다음과 같이 설정하면 된다.

<bean id="아무이름이나" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 	<property name="location">classpath:spring2/core/config/datasource.properties</property> </bean>

위와 같이 사용하거나 환경설정파일의 상단에 xmlns:p="http://www.springframework.org/schema/p"를 선언한 경우 다음과 같이 설정한다.

<bean id="아무이름이나" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="classpath:spring2/core/config/datasource.properties" />

만약 여러개로 properties 환경 설정파일을 나누어 관리한다면 아래와 같이 리스트를 호출하면 된다.

<bean id="아무이름이나" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 	<property name="locations"> 		<list> 			<value>classpath:spring2/core/config/datasource.properties</value> 			<value>classpath:spring2/core/config/coreConfig.properties</value> 		</list> 	</property> </bean>

만약 applicatonContext가 분리되어 있는 상황에서 각각의 경우 호출되는 properties가 다르다면 어떻게 설정을 해야 할까?
두 군데에 PropertyPlaceholderConfigurer가 존재하는 경우 최초 호출되는 빈객체에 대해서만 생성이 되며 나머지는 무시된다. (싱글톤처럼 사용이 되어진다.)

따라서 여러개의 PropertyPlaceholderConfigurer를 사용하려면 최초 호출되는 Configurer를 기본 Configurer보다 먼저 호출하면 된다.
이때 기존의 Configurer를 포함한 Configurer를 선언해야 한다.

<!-- 확장한 호출을 먼저 호출하여 불러들인다. --> 	<bean id="guiPropertyPlaceholderConfigurer" p:fileencoding="UTF-8" parent="corePropertyPlaceholderConfigurer"> 	<property name="locations"> 		<list> 			<value>classpath:spring2/core/config/datasource.properties</value> 			<value>classpath:spring2/gui/config/guiConfig.properties</value> 		</list> 	</property> </bean> <!--이전 호출이 이후 포함되어 있다고 가정한다.--> <bean id="corePropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="classpath:spring2/core/config/datasource.properties" p:fileencoding="UTF-8"></bean>

결국 multi에 대해 별다른 설정을 하는 방법이 없어서 이와 같이 사용하게 되는 것이다. (만약 멀티 configurer 설정이 있다면 댓글을 부탁드립니다.)

반응형
profile

파란하늘의 지식창고

@Bluesky_

내용이 유익했다면 광고 배너를 클릭 해주세요