zoukankan      html  css  js  c++  java
  • Springboot 自定义配置文件路径以及多 profile 配置文件

    一、什么是classpath

    classpath 指的就是 *.java 文件、资源文件等编译后存放的位置,对于 Maven 项目就是指 target/classes 这个路径,只要编译后的文件在这个目录下,springboot 就可以找到,我们这里指的是 Maven 项目, Java Web 项目可能会有区别.

     

    二、自定义springboot配置文件路径

    springboot 项目配置文件 application.properties/application.yml 默认放置的位置是 classpath:/、classpath:/config/、file:./、file:./config/ 这4个位置.只要我们编译后的文件位于这 4 个位置,springboot 就可以加载配置文件.但有时候我们需要以环境名称为标识,配置多个环境的配置文件.如下我们需要将配置文件放置在指定的环境标识下.

    如上图我们的资源文件的路径已经不再是 springboot 默认的配置路径了,因此 springboot 启动时将无法加载配置文件,需要我们在启动类中手动指定配置文件的加载路径了.

    @SpringBootApplication
    public class Springboot01Application {
        public static void main(String[] args) {
            // springboot 默认的配置文件路径
            // String addClassPath = "spring.config.location:classpath:/";
            // 自定义的配置文件路径
            String addClassPath = "spring.config.additional-location:classpath:/";
            addClassPath += ",classpath:/config/env/";
            new SpringApplicationBuilder(Springboot01Application.class).
                    properties("spring.config.name:application", addClassPath).build().run(args);
        }
    }
    

      

    三、多 profiles 配置文件的切换

    我们平时开发项目的时候,在不同的时期会存在不同的环境配置文件,例如: application-dev.properties、application-sit.properties、application-uat.properties、application-pro.properties,那么我们运行项目的时候如何做到根据当前所处的环境来切换对应的环境配置文件呢?

    springboot 为我们提供了切换配置文件的方式,我们在编写配置文件时,文件名可以是: application-{profile}.properties 或者是 application-{profile}.yml

    1、application-{profile}.properties方式

    一、各环境配置文件如下:

    application-dev.properties

    person.name=xiaomaomao-dev
    person.pet=dog-dev
    person.hobby=LOL-dev
    person.cleverLevel=nine-dev
    

    application-sit.properties

    person.name=xiaomaomao-sit
    person.pet=dog-sit
    person.hobby=LOL-sit
    person.cleverLevel=nine-sit
    

    application-uat.properties

    person.name=xiaomaomao-uat
    person.pet=dog-uat
    person.hobby=LOL-uat
    person.cleverLevel=nine-uat
    

    application-pro.properties 

    person.name=xiaomaomao-pro
    person.pet=dog-pro
    person.hobby=LOL-pro
    person.cleverLevel=nine-pro
    

    通过 application.properties 来选择需要激活的配置文件

    spring.profiles.active=sit

    二、实体类

    @Configuration
    @PropertySource("classpath:config/env/application.properties")
    @ConfigurationProperties(prefix = "person")
    // 省略了 get/set
    public class Person {
        private String name;
        private String pet;
        private String hobby;
        private String cleverLevel;
    }

    三、控制器

    @RestController
    public class SpringbootProfileController {
        @Autowired
        private ApplicationContext ioc;
        @RequestMapping(value = "/profile")
        public String testProfiles() {
            Person person = ioc.getBean("person", Person.class);
            System.out.println(person);
            return "hello profiles!!!";
        }
    }

    四、测试结果

     

    2、application.yaml方式

    spring:
       profiles:
          active: uat # 指定激活哪个配置文件
    ---
    
    server:
       port: 8082
    spring:
       profiles: dev
    
    ---
    
    server:
       port: 8083
    spring:
       profiles: sit
    
    
    ---
    
    server:
       port: 8084
    spring:
       profiles: uat
    

      

     

    参考:https://blog.csdn.net/m0_37735176/article/details/89085843

  • 相关阅读:
    将配置文件自动复制到vs的测试项目中
    用索引器简化的C#类型信息访问
    Requested Clipboard operation did not succeed的解决办法
    在win2003上IIS部署可能出现的问题的解决方案
    Login failed for user 'IIS APPPOOL\ASP.NET v4.0'.
    在Ubuntu虚拟机中配置bridge共享上网
    Ajax学习日志
    Workflow architecture in Windows SharePoint Services (version 3):WWF和WSS V3 的关系 无为而为
    使用BizTalk的必须关注:HWS已经死了,微软已经放弃HWS了,估计替代产品就WWF。(外加其它的宣告死亡的工具和API列表) 无为而为
    我想要求主管给我们升级电脑,但是主管不肯,大家报一报公司电脑的配置,我看我的电脑是不是最差的,顺便谈谈你们公司电脑硬件升级策略 无为而为
  • 原文地址:https://www.cnblogs.com/xiaomaomao/p/12092182.html
Copyright © 2011-2022 走看看