解决方案一:编程式指定
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(AppStart.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")})
这个@PropertySources注解也可以用来加载配置,但是这种方式不能用来加载application文件,只能加载其他的配置
本文介绍了两种SpringBoot中配置文件的加载方式:一种是通过编程式指定的方式加载properties文件;另一种是在启动时直接指定配置文件的位置,这种方式支持加载yml文件。
1108

被折叠的 条评论
为什么被折叠?



