zoukankan      html  css  js  c++  java
  • SpringBoot 多环境配置

    SpringBoot 多环境配置

     

      在我们的实际开发中,一般都有三套环境,开发环境,测试环境,生产环境,三套环境的数据库连接配置也有所不同,比如,端口,IP地址等等。如果在打包时候都频繁的修改配置文件信息,那必将是非常容易出错的地方。

      在springBoot多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,例如:

      (1)application-dev.properties 对应开发环境  (2)application-test.properties 对应测试环境  (3)application-pro.properties 对应生产环境

      对于哪个配置会生效,需要在application.properties中通过spring.profiles.active属性来设置,其值对应{profile}值,例如:

      spring.profiles.active=dev 就会加载开发环境配置的信息。

      下面我们进行测试一下,首先新建一个user类,如下:

    复制代码
    package springboot.domain;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component
    public class User {
    
        @Value("${com.name}")
        private String name;
        
        private String age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAge() {
            return age;
        }
    
        public void setAge(String age) {
            this.age = age;
        }
    }
    复制代码

      在三个配置文件分别输入com.name=zhengxisheng,com.name=zhengxisheng2,com.name=zhengxisheng3,如下:

      

       在全局配置文件中指定使用测试环境配置文件,如下:

      

       访问hello方法,查看输出信息,如下:

      

  • 相关阅读:
    angular 按下回车键触发事件
    vue 父组件与子组件的通信
    最近在开发一个文章聚合的工具
    Martinjingyu的开发环境
    个推push数据统计(爬虫)
    基于redis的订单号生成方案
    电商平台--Mysql主从搭建(2)
    Mysql主从搭建(1)
    mysql物理级别热备脚本
    外键查询及删除
  • 原文地址:https://www.cnblogs.com/smallfa/p/13664980.html
Copyright © 2011-2022 走看看