spring 自定义参数配置

系统里用到的可能需要改动的配置参数可以单独写在一个参数文件里,改动的时候会非常方便。

新建一个config.properties文件

config.rows=15

config.cacheExpireSeconds=60

 写一个AppConfig

public class AppConfig {
    /**
     * 每页行数
     */
    private Integer rows = 15;
    /**
     * 缓存失效时间(秒)
     */
    private Long cacheExpireSeconds = Long.valueOf(60 * 5);

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        if(rows!=null){
            this.rows = rows;
        }
    }

    public long getCacheExpireSeconds() {
        return cacheExpireSeconds;
    }

    public void setCacheExpireSeconds(Long cacheExpireSeconds) {
        if(cacheExpireSeconds!=null){
            this.cacheExpireSeconds = cacheExpireSeconds;
        }
    }
    
}

 

在spring文件里配置

<!-- 引入属性文件 -->
<context:property-placeholder location="classpath*:config/*.properties" />

<bean id="appConfig" class="cn.com.cicpay.model.AppConfig">
    <property name="rows" value="${config.rows}"/>
    <property name="cacheExpireSeconds" value="${config.cacheExpireSeconds}"/>
</bean>

 

posted @ 2016-11-23 10:31  冯瑞  阅读(519)  评论(0编辑  收藏  举报