- Spring Boot下配置文件,读取的顺序:
项目根目录下的config目录。
项目根目录。
项目classpath下的config目录。
项目classpath根目录。 - 如果我们想指定配置文件的位置,需要怎么做呢?
ConfigurableApplicationContext context = new SpringApplicationBuilder(当前的类名.class)
.properties("spring.config.location=classpath:/resources目录下配置文件的路径").run(args);
可以使用context.getEnvironment().getProperty(“配置文件中的属性名称”)
,查看效果。
3. 需要在不同的环境中,激活使用不同的配置,可以使用profiles,例如,如下配置:
spring:
profiles: mysql
jdbc:
driver:
com.mysql.jdbc.Driver
--- #进行分割
spring:
profiles: oracle
jdbc:
driver:
oracle.jdbc.driver.OracleDriver
启动时,通过spring.profiles.active
来指定激活的profiles。
ConfigurableApplicationContext context = new SpringApplicationBuilder(当前的类名.class)
.properties("spring.config.location=classpath:/此处是上面配置文件的名称.yml")
.properties("spring.profiles.active=oracle").run(args);
System.out.println(context.getEnvironment().getProperty("jdbc.driver"));