Thursday 28 May 2009

spring'de PropertyPlaceholderConfigurer ile multiple properties dosyası kullanımı

Spring'de PropertyPlaceholderConfigurer class'ını kullanarak xml'lerde properties dosya değerlerini ilgili bean'lere geçirebiliriz. Bunun için aşağıdaki gibi bir kullanım yeterlidir.

<bean id="propertyConfigurer" class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:appConfig.properties</value>
</list>
</property>
</bean>


Fakat birden fazla properties dosyasi kullanim ihtiyaci cikti. Bu durumda buraya birden fazla properties dosyasi value'su ekleyebiliyoruz.

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:file.properties</value>
<value>classpath:ldap.properties</value>
</list>
</property>
</bean>

gibi.

XML içinde bu değerlerin kullanımı da şöyle oluyor. ldap.properties dosyamda config.ldap.username isimli property olduğunu varsayarak:
<property name="username" value="${config.ldap.username}"/>

Şık, temiz.

No comments: