springboot编程式指定默认的application.properties文件路径

解决方案一:编程式指定  

Properties defaultProperties = new Properties();
InputStream in;
    try {
        in = new FileInputStream("E:/conf/application.properties");
	defaultProperties.load(in);
	in.close();
      } catch (IOException e) {
	e.printStackTrace();
      }
        SpringApplication app = new SpringApplication(EurakaApp.class);
	app.setDefaultProperties(defaultProperties);

特别注意:

这种方法无法加载yml文件,看源码可以发现 defaultProperties.load方法是基于自定义的LineReader实现的,不支持yml文件这种格式,不过也可以使用在线的工具类将yml文件转换为properties


解决方案二:启动时指定  

    java -jar vPaas.jar  --spring.config.location=/home/springboot/config/application.yml

    注:这种方式是可以成功读取yml文件的


其他

@PropertySources({@PropertySource("file:E:/conf/jdbcConfig.properties")})

扫描二维码关注公众号,回复: 2496699 查看本文章

这个@PropertySources注解也可以用来加载配置,但是这种方式不能用来加载application文件,只能加载其他的配置


猜你喜欢

转载自blog.csdn.net/callmev6/article/details/80931081