这里介绍的是用 加载Properties流实现 springboot在静态方法中读取xxx.properties配置文件属性
没有用spring Bean注入方法实现的原因:项目中有个特殊的静态方法需要在很靠前的时候执行并且需要用到一些配置属性,尝试了用 spring @Value注入的各种方法,由于该静态方法执行非常靠前,而通过spring 读取注入的配置类还没有加载,最终都是在静态方法中无法获取到xxx.properties配置属性。
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
/**
* @author:
* @create:
**/
public class PropertyUtil {
/**
* 读取 classpath 下 指定的properties配置文件,加载到Properties并返回Properties
* @param name 配置文件名,如:mongo.properties
* @return
*/
public static Properties getConfig(String name){
Properties props=null;
try{
props = new Properties();
InputStream in = PropertyUtil.class.getClassLoader().getResourceAsStream(name);
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
props.load(bf);
in.close();
}catch(Exception ex){
ex.printStackTrace();
}
return props;
}
public static String getPropValue(Properties prop,String key){
if(key == null || "".equals(key.trim())){
return null;
}
String value = prop.getProperty(key);
if(value == null){
return null;
}
value = value.trim();
//判断是否是环境变量配置属性,例如 server.env=${serverEnv:local}
if(value.startsWith("${") && value.endsWith("}") && value.contains(":")){
int indexOfColon = value.indexOf(":");
String envName = value.substring(2,indexOfColon);
//获取系统环境变量 envName 的内容,如果没有找到,则返回defaultValue
String envValue = System.getenv(envName);
if(envValue == null){
//配置的默认值
return value.substring(indexOfColon+1,value.length()-1);
}
return envValue;
}
return value;
}
public static void main(String[] args) {
Properties prop = PropertyUtil.getConfig("mongo-pool.properties");
//
System.out.println(prop.getProperty("spring.data.mongodb.database"));
//建议采用下面这种获取方法,能够处理 环境变量配置属性 例如 server.env=${serverEnv:local}
System.out.println( PropertyUtil.getPropValue( prop , "spring.data.mongodb.database" ) );
}
}