Ek açıklamalar aracılığıyla sınıf yolundan alınan bir grup Spring çekirdeğim var, örn.
@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {
// Implementation omitted
}
Spring XML dosyasında, tanımlanmış bir PropertyPlaceholderConfigurer vardır:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/app.properties" />
</bean>
app.properites dosyasındaki özelliklerden birini yukarıda gösterilen bean'e enjekte etmek istiyorum. Basitçe şöyle bir şey yapamam
<bean class="com.example.PersonDaoImpl">
<property name="maxResults" value="${results.max}"/>
</bean>
Çünkü PersonDaoImpl Spring XML dosyasında yer almaz (sınıf yolundan ek açıklamalar aracılığıyla alınır). Aşağıdakilere kadar geldim:
@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {
@Resource(name = "propertyConfigurer")
protected void setProperties(PropertyPlaceholderConfigurer ppc) {
// Now how do I access results.max?
}
}
Ancak ppc
den ilgilendiğim özelliğe nasıl erişeceğim açık değil mi?
Başka bir alternatif de aşağıda gösterilen appProperties fasulyesini eklemektir:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/app.properties" />
</bean>
<bean id="appProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true"/>
<property name="properties">
<props>
<prop key="results.max">${results.max}</prop>
</props>
</property>
</bean>
Bu fasulye alındığında, değeri app.properties
dosyasından okunan results.max
adlı bir özellik içeren bir java.util.Properties
dosyasına dönüştürülebilir. Yine, bu fasulye @Resource ek açıklaması aracılığıyla herhangi bir sınıfa bağımlılık enjekte edilebilir (java.util.Properties örneği olarak).
Şahsen, appProperties tarafından tam olarak hangi özelliklerin gösterileceğini sınırlayabildiğiniz ve app.properties dosyasını iki kez okumanıza gerek kalmadığı için bu çözümü (önerdiğim diğerine) tercih ediyorum.
Biri üretim için ve diğeri geliştirme için (dağıtılmayacak) olmak üzere iki özellik dosyasına ihtiyacım var.
Hem otomatik olarak bağlanabilen bir Properties Bean'e hem de bir PropertyConfigurer'a sahip olmak için yazabilirsiniz:
<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:live.properties</value>
<value>classpath:development.properties</value>
</list>
</property>
</bean>
ve PropertyConfigurer'da Properties Bean'e referans verin
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="appProperties" />
</bean>
Olası bir çözüm, aynı özellikler dosyasından okuyan ikinci bir fasulye bildirmektir:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/app.properties" />
</bean>
<util:properties id="appProperties" location="classpath:/WEB-INF/app.properties"/>
appProperties' adlı bean java.util.Properties türündedir ve yukarıda gösterilen @Resource attruibute kullanılarak bağımlılık enjekte edilebilir.