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);
    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值