• 24-SpringBoot Profile环境切换


    1.相关介绍

    为了方便多环境适配,Spring Boot简化了profile功能。

    • 默认配置文件application.yaml(application.properties)任何时候都会加载。
    • 指定环境配置文件application-{env}.yaml,env通常替代为test,
    • 激活指定环境
      • 配置文件激活:spring.profiles.active=prod
      • 命令行激活:java -jar xxx.jar --spring.profiles.active=prod --person.name=haha(修改配置文件的任意值,命令行优先)
    • 默认配置与环境配置同时生效
    • 同名配置项,profile配置优先

    2.运行测试

    1.配置文件中指定运行环境

    在这里插入图片描述

    application.properties

    server.port=8080
    spring.profiles.active=prod
    
    • 1
    • 2

    application-prod.properties

    project.env=prod
    
    • 1

    application-test.properties

    project.env=test
    
    • 1
    @Controller
    public class HelloController {
    
    
        @Value("${project.env}")
        private String env;
    
        @ResponseBody
        @GetMapping("/test")
        public String test1(){
    
            return "env="+env;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    2.命令行中指定运行环境

    把web项目打包为jar包, 在命令行中启动

    java -jar springboot-test2-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
    
    • 1

    在这里插入图片描述

    在这里插入图片描述

    命令中还可以修改配置文件中的任意属性

    java -jar springboot-test2-0.0.1-SNAPSHOT.jar --spring.profiles.active=test --server.port=8081 --project.env=cmd-test
    
    • 1

    在这里插入图片描述
    在这里插入图片描述

    3.@Profile条件装配功能

    @Profile可写在类上或者方法上, 当为指定环境时该类或者方法才生效
    MyConfig

    @Configuration
    public class MyConfig {
    
        private String env;
    
        @Profile("test") //当环境是test时才生效
        @Bean
        public Color red() {
            Red red = new Red();
            red.setName("red");
            return red;
        }
    
        @Profile("prod") //当环境是prod时才生效
        @Bean
        public Color bule() {
            Blue blue = new Blue();
            blue.setName("blue");
            return blue;
        }
    }
    
    HelloController
    ```java
    @Controller
    public class HelloController {
    
        @Value("${project.env}")
        private String env;
    
        @Autowired
        private Color color;
    
        @ResponseBody
        @GetMapping("/test")
        public String test1(){
    
            System.out.println("env="+env);
            return "color="+color.getName();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    Color
    ```java
    public interface Color {
        String getName();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Blue

    @Data
    public class Blue implements Color{
        private String name;
    }
    
    • 1
    • 2
    • 3
    • 4

    Red

    @Data
    public class Red implements Color {
        private String name;
    }
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述
    application.properties

    server.port=8080
    spring.profiles.active=test
    
    • 1
    • 2

    application-prod.properties

    project.env=prod
    
    • 1

    application-test.properties

    project.env=test
    
    • 1

    在这里插入图片描述
    在这里插入图片描述

    4.配置文件分组

    配置文件可以分组, 然后可以整合导入使用
    在这里插入图片描述

    application.properties

    server.port=8080
    spring.profiles.active=production
    
    spring.profiles.group.production[0]=prod1
    spring.profiles.group.production[1]=prod2
    
    • 1
    • 2
    • 3
    • 4
    • 5

    application-prod1.properties

    color.name=red
    
    • 1

    application-prod2.properties

    color.size=10
    
    • 1

    HelloController

    @Controller
    public class HelloController {
    
        @Autowired
        private Red red;
    
        @ResponseBody
        @GetMapping("/test")
        public Red test1(){
    
            return red;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Red

    @Data
    @Component
    @ConfigurationProperties(prefix = "color")
    public class Red {
        private String name;
        private Integer size;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    5.配置加载优先级

    • 外部配置源(优先级由低到高)

      1. Java属性文件。
      2. YAML文件。
      3. 环境变量。
      4. 命令行参数。
    • 配置文件查找位置

      1. classpath 根路径。
      2. classpath 根路径下config目录。
      3. jar包当前目录。
      4. jar包当前目录的config目录。
      5. /config子目录的直接子目录。
    • 配置文件加载顺序:

      1. 当前jar包内部的application.properties和application.yml。
      2. 当前jar包内部的application-{profile}.properties 和 application-{profile}.yml。
      3. 引用的外部jar包的application.properties和application.yml。
      4. 引用的外部jar包的application-{profile}.properties和application-{profile}.yml。
    • 指定环境优先,外部优先,后面的可以覆盖前面的同名配置项。

  • 相关阅读:
    springboot配置log4j2,现扒现用,简单易懂
    深入解析JVM G1 垃圾回收器
    大数据运维一些常见批量操作命令
    讯飞有一个可以根据描述文本自动生成PPT的AI接口,有趣
    【JVM技术专题】针对于ASM库生成和修改class文件开发指南 「 入门篇」
    Next.js和sharp实现占位图片生成工具
    前端面试宝典React篇15 如何提升 React 代码可维护性?
    鼎盛合:adc芯片的五种结构
    手撕Vue-编译指令数据
    Docker搭建RK3568开发环境
  • 原文地址:https://blog.csdn.net/qq_41865229/article/details/125479075