读取配置文件的方案:
public String getProperty(String name,String k){
Properties prop = new Properties();
String key=null;
String val=null;
try{
//读取属性文件a.properties
InputStream in = new BufferedInputStream (new FileInputStream(name));
prop.load(in); ///加载属性列表
Iterator it=prop.stringPropertyNames().iterator();
while(it.hasNext()){
key=it.next();
if(key.equals(k)){
System.out.println(key+":"+prop.getProperty(key));
val=prop.getProperty(key);
break;
}
}
in.close();
}catch(Exception e){
System.out.println(e);
}
return val;
}
在springboot中,可以将其设置为aop的切面;
注意:第一个参数name,直接写a.properties,只能读取到该目录下的配置文件;
若在其他目录中,则:
method.getProperty("src\\main\\resources\\a.properties" ,"localPath");