(一)创建Spring Boot Web项目ProfileDemo01
(二)创建多环境配置文件
1、将application.properties更名为application.yaml
2、模拟开发环境,创建配置文件application-dev.yaml
3、模拟测试环境,创建配置文件application-test.yaml
4、模拟生产环境,创建配置文件application-pro.yaml
(三)创建控制器
package net.hw.lesson05.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProfileController {
@GetMapping("/hello")
public String hello() {
return “Welcome to Spring Boot World~”;
}
}
(四)指定使用环境
(1) 在全局配置文件里指定当前使用环境 - 开发环境
- 启动项目,查看采用的使用环境:服务器端口号与虚拟路径
- 访问http://localhost:8081/lzy/hello
(2) 在全局配置文件里指定当前使用环境 - 测试环境
- 启动项目,查看采用的使用环境:服务器端口号与虚拟路径
访问http://localhost:8081/lzy/hello
访问http://localhost:8082/ied/hello
(3) 在全局配置文件里指定当前使用环境 - 生产环境
启动项目,查看采用的使用环境:服务器端口号与虚拟路径
- 访问http://localhost:8082/ied/hello
- 访问http://localhost:8083/china/hello