读取项目各个模块的版本号

package com.runtrial.version.controller.version;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.runtrial.version.bean.Version;

/**
* 读取版本
*
* @author ssn
*
*/
@Controller
public class VersionController {

@RequestMapping("/version/list.htm")
public String find(ModelMap map) {

    Properties prop = new Properties();
    String filePath = "/application.properties";
    try {
        InputStream in = VersionController.class.getResourceAsStream(filePath);
        prop.load(in);
        Iterator<String> it = prop.stringPropertyNames().iterator();
        List<Version> versions = new ArrayList<Version>();
        while (it.hasNext()) {
            String key = it.next();
            Version version = new Version();
            version.setName(key);
            version.setVersion(prop.getProperty(key));
            versions.add(version);
            map.put("versions", versions);
        }
        in.close();

    } catch (Exception e) {
        System.out.println(e);
    }

    return "/version/version";

}

}

创建一个version bean
package com.runtrial.version.bean;

import java.io.Serializable;

public class Version implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -9138828550310842522L;
private String name;
private String version;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getVersion() {
    return version;
}

public void setVersion(String version) {
    this.version = version;
}

}

还要自己写一个前端页面。
差不多就这样吧。今天在写这个东西的时候遇到了两个问题
1.是application.properties文件配置在哪的问题。
2.也是一个路径读取问题
getResourceAsStream这个方法不使用绝对路径,
而是使用相对于classpath环境变量的相对路径。
所 以,如果写:
getResourceAsStream(“/application.properties”);
则要保证classpath下有application.properties文件就能找到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值