Spring 是怎么管理properties文件的

来源:互联网 发布:收淘宝评价 编辑:程序博客网 时间:2024/05/16 08:52

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了。