一、我们使用Profile文件进行多环境配置

(一)然后创建Spring Boot Web项目ProfileDemo01

项目初始化过程可以参看上一讲ConfigDemo01的创建过程

spring boot 引入多个xml springboot配置多个yml_虚拟路径


(二)开始创建多环境配置文件

配置文件命名格式:application-xxx.yaml

此例仅演示端口号与虚拟路径的配置,实际应用中可以配置许多内容

1、将application.properties更名为application.yaml

spring boot 引入多个xml springboot配置多个yml_虚拟路径_02


2、模拟开发环境,创建配置文件application-dev.yaml

spring boot 引入多个xml springboot配置多个yml_配置文件_03


3、模拟测试环境,创建配置文件application-test.yaml

spring boot 引入多个xml springboot配置多个yml_jar包_04


4、模拟生产环境,创建配置文件application-pro.yaml

spring boot 引入多个xml springboot配置多个yml_虚拟路径_05


(三)创建控制器

在net.ql.lesson05里创建controller子包,在子包里创建ProfileController

spring boot 引入多个xml springboot配置多个yml_spring boot_06


spring boot 引入多个xml springboot配置多个yml_spring boot_07


四指定使用环境

方式1、使用配置文件全局指定使用环境

(1) 在全局配置文件里指定当前使用环境 - 开发环境

spring boot 引入多个xml springboot配置多个yml_虚拟路径_08


启动项目,查看采用的使用环境:服务器端口号与虚拟路径

spring boot 引入多个xml springboot配置多个yml_配置文件_09


访问http://localhost:8081/lzy/hello

spring boot 引入多个xml springboot配置多个yml_配置文件_10


(2) 在全局配置文件里指定当前使用环境 - 测试环境

spring boot 引入多个xml springboot配置多个yml_虚拟路径_11


启动项目,查看采用的使用环境:服务器端口号与虚拟路径

spring boot 引入多个xml springboot配置多个yml_配置文件_12


访问http://localhost:8081/lzy/hello

spring boot 引入多个xml springboot配置多个yml_配置文件_13


访问http://localhost:8082/ied/hello

spring boot 引入多个xml springboot配置多个yml_虚拟路径_14


(3) 在全局配置文件里指定当前使用环境 - 生产环境

spring boot 引入多个xml springboot配置多个yml_spring boot 引入多个xml_15


启动项目,查看采用的使用环境:服务器端口号与虚拟路径

spring boot 引入多个xml springboot配置多个yml_虚拟路径_16


访问http://localhost:8082/ied/hello

spring boot 引入多个xml springboot配置多个yml_虚拟路径_17


访问http://localhost:8083/china/hello

spring boot 引入多个xml springboot配置多个yml_jar包_18


方式2、通过命令行方式指定使用环境

(1)使用IDEA将Maven项目打成jar包

Maven - ProfileDemo01 - LifeCycle - package,单击右键,在快捷菜单里执行“Run Maven Build”

spring boot 引入多个xml springboot配置多个yml_配置文件_19


spring boot 引入多个xml springboot配置多个yml_spring boot_20


在target目录里查看生成的项目jar包profiledemo01-0.0.1-SNAPSHOT.jar

spring boot 引入多个xml springboot配置多个yml_jar包_21


(2)在终端执行jar包,选择使用环境 - 开发环境

spring boot 引入多个xml springboot配置多个yml_虚拟路径_22


访问http://localhost:8081/lzy/hello

spring boot 引入多个xml springboot配置多个yml_配置文件_23


按Ctrl + C组合键,停止项目的运行

spring boot 引入多个xml springboot配置多个yml_jar包_24


在终端执行jar包,选择使用环境 - 测试环境

spring boot 引入多个xml springboot配置多个yml_jar包_25


访问http://localhost:8082/ied/hello

spring boot 引入多个xml springboot配置多个yml_spring boot_26


按Ctrl + C组合键,停止项目的运行

spring boot 引入多个xml springboot配置多个yml_配置文件_27


4)在终端执行jar包,选择使用环境 - 生产环境

spring boot 引入多个xml springboot配置多个yml_配置文件_28


访问http://localhost:8083/china/hello

spring boot 引入多个xml springboot配置多个yml_虚拟路径_29


按Ctrl + C组合键,停止项目的运行

spring boot 引入多个xml springboot配置多个yml_spring boot 引入多个xml_30


1、测试随机数my.number

注入配置文件里的属性

输出配置文件里的属性

package net.ql.lesson05;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class RandomSetDemoApplicationTests {
    // 注入配置文件里的属性
    @Value("${my.number}")
    private String number;

    @Test
    void contextLoads() {
        // 输出配置文件里的属性
        System.out.println(number);
    }
}

运行了三次,最后一次结果

spring boot 引入多个xml springboot配置多个yml_jar包_31


2、测试随机整数my.integer

注入配置文件里的属性

输出配置文件里的属性

spring boot 引入多个xml springboot配置多个yml_spring boot 引入多个xml_32


运行了三次,最后一次结果

spring boot 引入多个xml springboot配置多个yml_spring boot 引入多个xml_33