一、springboot方式获取
配置参数
application.properties文件中的dal.file.path就是上传文件时所需要的额参数key了
用法
在由spring管理的类中通过注解 @Value 即可注入参数
二、直接读取配置文件
获取
以下是获取配置的工具类
public class ConstantFP {
public static String filePath;
// 文件读取,只会执行一次,使用静态代码块
static {
//读取文件,获取值
try {
//1.创建Properties集合类
Properties props = new Properties();
//2.加载文件
//获取src路径下的文件--->ClassLoader类加载器
props.load(ConstantFP.class.getClassLoader()
.getResourceAsStream("application.properties"));
//3获取数据
filePath = props.getProperty("dal.file.path");
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用
在任意位置,比如各种工具类 ConstantFP.filePath 即可获取配置参数