Java——spring boot打的jar包如何引用外部application.properties

 前言

现在的项目越来越多的都是打包成jar运行,尤其是springboot项目,这时候配置文件如果一直放在项目中,每次进行简单的修改时总会有些不方便,

问题解决

启动类修改如下,需要把application.properties放在jar包同级目录下

@SpringBootApplication
@EnableScheduling
public class Application {
    public static void main(String[] args) throws Exception {
        Properties prop = new Properties();
        String property = System.getProperty("user.dir");
        System.out.println(property);
        FileInputStream inputStream = new FileInputStream(new File(property) + "/application.properties");
        prop.load(inputStream);
        inputStream.close();
        SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
        SpringApplication app = builder.build();
        app.setDefaultProperties(prop);
        app.run(args);
    }
}

 

posted @ 2021-08-28 10:03  前方一片光明  阅读(354)  评论(0编辑  收藏  举报