Spring對properties檔案的管理其實還是很容易的。
在app-config.xml裡面聲明一個class。
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:epublish-config.properties</value>
</list>
</property>
</bean>
這個類是Spring用來管理properties檔案的。
properties檔案就放在src下面的resource裡。
我的properties裡只有一行
authentication.control = FALSE
現在我想讓我的一個java class的一個變數拿到這個properties檔案裡面的值
<bean id="service" class="Service">
<property name="authenticationController">
<value>${authentication.control}</value>
</property>
</bean>
在Service.java裡
private String authenticationController;
public String getAuthenticationController() {
return authenticationController;
}
public void setAuthenticationController(String authenticationController) {
this.authenticationController = authenticationController;
}
由於我是要在子類ChildService裡調用,
所以
<bean id="childService" class="ChildService" parent="service"/>
一定要告訴Spring,你這個child class的老爸是誰,才可以調用,否則,只有null了。