SpringBoot之配置文件加载位置

一、SpringBoot启动会扫描application.properties或者application.yml文件做为springboot的配置文件。默认建立项目生成application.properties/yml位置在classpath目录下,也能够在如下4个地方建立优先级自上而下,可是各个配置文件成互补状态存在

   * file: ./config/java

  * file: ./web

  * classpath: /config/spring

  * classpath:/springboot

测试1:classpath目录下建立application.properties文件app

classpath:application.properties server.port=8888

 

测试2:classpath目录下建立config目录再建立application.properties文件ide

classpath:config/application.properties server.port=8001

 

测试3:项目根目录下建立application.properties文件spring-boot

file:./application.properties server.port=8002

测试4:项目根目录下建立config目录再建立application.properties文件测试

file:./config/application.properties server.port=8003

 

二、在打包完成的状况下,须要新增一些配置,这时该怎么作呢? 能够经过配置spring.config.location来改变默认配置。

  * G盘符下建立文件application.propertiesui

G:\application.propertiesidea

server.port=9999
server.servlet.context-path=/boot02

  * 在idea中terminal执行语句:

 java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.config.location=G:\application.properties

  * controller代码

package com.atguigu.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello() { return "hello...."; } }

  * 访问出结果

相关文章
相关标签/搜索