Springboot打包jar包读取外部properties文件运行
获取jar包所在的位置
//获取jar包所在的位置
String property = System.getProperty("user.dir");
jar包和配置文件在同一目录下获取文件流
//jar包和外部配置文件在同一目录下,获得properties文件流
Properties prop = new Properties();
FileInputStream inputStream = new FileInputStream(new File(property) + "/application.properties");
prop.load(inputStream);
inputStream.close();
springboot启动类设置默认配置文件
完整代码
@SpringBootApplication
public class ReadconfigApplication {
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
//获取jar包所在位置
String property = System.getProperty("user.dir");
System.out.println(property);
FileInputStream inputStream = new FileInputStream(new File(property) + "/application.properties");
//properties对象读取文件输入流的数据
prop.load(inputStream);
inputStream.close();
SpringApplication app = new SpringApplication(ReadconfigApplication.class);
//springboot启动时设置外部的application.properties为默认配置文件
app.setDefaultProperties(prop);
//启动项目
app.run(args);
//一般项目的启动代码
//SpringApplication.run(ReadconfigApplication.class, args);
}
}