通常情况下,Spring Boot 在启动时会将 resources 目录下的 application.properties 或 apllication.yml 作为其默认配置文件,我们可以在该配置文件中对项目进行配置,但这并不意味着 Spring Boot 项目中只能存在一个 application.properties 或 application.yml。
默认配置文件
Spring Boot 项目中可以存在多个 application.properties 或 apllication.yml。
项目根路径下配置文件 application.yml 配置如下。
#项目更目录下
#上下文路径为 /abc
server:
servlet:
context-path: /abc
项目类路径下 config 目录下配置文件 application.yml 配置如下。
#类路径下的 config 目录下
#端口号为8084
#上下文路径为 /helloWorld
server:
port: 8084
servlet:
context-path: /helloworld
项目类路径下的 application.yml 配置如下。
#默认配置
server:
port: 8080
在 net.biancheng.www.controller 包下创建一个名为 MyController 的类,代码如下。
package net.biancheng.www.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@ResponseBody
@RequestMapping("/test")
public String hello() {
return "hello Spring Boot!";
}
}
启动 Spring Boot,查看控制台输出。