实现项目的多环境配置的方法有很多,比如通过在Pom.xml中配置profiles(最常见) 然后在Install项目打War包的时候,根据需求打不同环境的包,如图:

这种配置多环境的方法在SSM框架中使用的最多,但在SpringBoot中使用最多的是在启动SpringBoot项目的时候指定运行环境,下面也是主要描述这种配置的方法:

1.添加配置文件

在SpringBoot的Resources目录下建4个配置文件 application.yml、application-dev.yml、application-qa.yml、application-online.yml

dev:开发环境

qa:测试环境

online:生产环境

然后在application.yml配置文件中配置默认的运行环境:

  1. spring:
  2. profiles:
  3. active: dev

然后在dev、qa、online中分别配置不同的配置内容,例如变更端口:

dev

  1. server:
  2. port: 8085
  3. servlet:
  4. context-path: /api
  5. tomcat:
  6. max-threads: 100
  7. connection-timeout: 5000
  8. spring:
  9. profiles: dev

qa

  1. server:
  2. port: 8086
  3. servlet:
  4. context-path: /api
  5. tomcat:
  6. max-threads: 100
  7. connection-timeout: 5000
  8. spring:
  9. profiles: qa

online

  1. server:
  2. port: 8087
  3. servlet:
  4. context-path: /api
  5. tomcat:
  6. max-threads: 100
  7. connection-timeout: 5000
  8. spring:
  9. profiles: online

然后在 SpringBoot系统列 1 - HelloWorld!  的基础上继续添加代码,新建WebConfig用于存放SpringBoot的一些配置信息(SpringBoot的配置即可以在配置文件中配置,也可以在类中配置):

  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.boot.SpringBootConfiguration;
  3.  
  4. /**
  5. * 配置类
  6. * @author XIHONGLEI
  7. * @date 2018-10-31
  8. */
  9. @SpringBootConfiguration
  10. public class WebConfig {
  11.  
  12. @Value("${server.port}")
  13. public String port;
  14. }

然后改造一下HelloContrlller,为了区分环境,我们在请求/api/hello的时候将端口号展示出:

  1. import com.hello.WebConfig;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5.  
  6. @RestController
  7.  
  8. public class HelloController {
  9.  
  10. @Autowired
  11. private WebConfig webConfig;
  12.  
  13. @RequestMapping("hello")
  14. public String hello() {
  15. return "Hello World! port:".concat(webConfig.port);
  16. }
  17. }

然后在pom.xml配置Jar包的打包配置:

  1. <packaging>jar</packaging>
  2. <build>
  3. <finalName>spring-boot-hello</finalName>
  4. <resources>
  5. <resource>
  6. <directory>src/main/java</directory>
  7. <includes>
  8. <include>**/*.yml</include>
  9. <include>**/*.properties</include>
  10. <include>**/*.xml</include>
  11. </includes>
  12. <filtering>false</filtering>
  13. </resource>
  14. <resource>
  15. <directory>src/main/resources</directory>
  16. <includes>
  17. <include>**/*.yml</include>
  18. <include>**/*.properties</include>
  19. <include>**/*.xml</include>
  20. </includes>
  21. <filtering>false</filtering>
  22. </resource>
  23. </resources>
  24. <plugins>
  25. <plugin>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-maven-plugin</artifactId>
  28. <configuration>
  29. <fork>true</fork>
  30. <mainClass>com.hello.Application</mainClass>
  31. </configuration>
  32. <executions>
  33. <execution>
  34. <goals>
  35. <goal>repackage</goal>
  36. </goals>
  37. </execution>
  38. </executions>
  39. </plugin>
  40. <plugin>
  41. <artifactId>maven-resources-plugin</artifactId>
  42. <version>2.5</version>
  43. <configuration>
  44. <encoding>UTF-8</encoding>
  45. <useDefaultDelimiters>true</useDefaultDelimiters>
  46. </configuration>
  47. </plugin>
  48. <plugin>
  49. <groupId>org.apache.maven.plugins</groupId>
  50. <artifactId>maven-surefire-plugin</artifactId>
  51. <version>2.18.1</version>
  52. <configuration>
  53. <skipTests>true</skipTests>
  54. </configuration>
  55. </plugin>
  56. <plugin>
  57. <groupId>org.apache.maven.plugins</groupId>
  58. <artifactId>maven-compiler-plugin</artifactId>
  59. <version>2.3.2</version>
  60. <configuration>
  61. <source>1.8</source>
  62. <target>1.8</target>
  63. </configuration>
  64. </plugin>
  65. </plugins>
  66. </build>

然后Install,找打Jar包 spring-boot-hello.jar;

在Window控制台或者Linux中可以使用java -jar spring-boot-hello.jar来启动SpringBoot项目,然后通过在后方添加--spring.profiles.active来指定启动SpringBoot项目时使用的环境:

  1. # Dev环境
  2. $ java -jar spring-boot-hello.jar --spring.profiles.active=dev
  3.  
  4. # qa环境
  5. $ java -jar spring-boot-hello.jar --spring.profiles.active=qa
  6.  
  7. # online环境
  8. $ java -jar spring-boot-hello.jar --spring.profiles.active=online

例启动Online环境:

然后通过 http://localhost:8087/api/hello 来访问,因为Online中配置的端口是8087

完成!

在IDEA中怎么在运行的时候选定执行环境,可以通过配置Application的program arguments中配置运行环境:

SpringBoot系统列 2 - 配置文件,多环境配置(dev,qa,online)的更多相关文章

  1. SpringBoot系统列 1 - HelloWorld!

    学习SpringBoot系统列之HelloWorld! 1.新建一个Maven项目 2.添加POM配置 <parent> <groupId>org.springframewor ...

  2. WIN7系统JavaEE(java+tomcat7+Eclipse)环境配置

    https://jingyan.baidu.com/article/3a2f7c2e62d25e26afd611fa.html WIN7系统JavaEE(java+tomcat7+Eclipse)环境 ...

  3. SpringBoot在启动时的多环境配置以及加载顺序

    通常我们在开发完成一个SpringBoot项目时,总是要打包部署的. 在启动SpringBoot应用时,我们常常会使用命令java -jar xxx.jar来启动这个服务. 命令java -jar 除 ...

  4. SpringBoot | 第五章:多环境配置

    前言 写上一篇看英文资料,耗费了心力呀,这章,相对来说简单点.也比较熟悉,但是这很实用.不扯了,开始~ 多环境配置 maven的多环境配置 springboot多环境配置 总结 老生常谈 多环境配置 ...

  5. windows系统下简单nodej.s环境配置 安装

    国内目前关注最高,维护最好的一个关于nodejs的网站应该是http://www.cnodejs.org/ windows系统下简单nodejs环境配置. 第一步:下载安装文件 下载地址:官网 htt ...

  6. 配置文件,环境配置和war报分离,方便生产更改

    在生产环境实现配置文件和war包 的分离,为方便在必要的时候进行一定的更改,可以避免修改包,但是需要重启 最初为这样的选择配置,单不知为何未生效,修改为配置2配置方法,但不灵活,待跟进.配置1: &l ...

  7. windows系统下简单node.js环境配置 安装

    国内目前关注最高,维护最好的一个关于nodejs的网站应该是http://www.cnodejs.org/ windows系统下简单nodejs环境配置. 第一步:下载安装文件 下载地址:官网 htt ...

  8. (四)Spring Boot之配置文件-多环境配置

    一.Properties多环境配置 1. application.properties配置激活选项 spring.profiles.active=dev 2.添加其他配置文件 3.结果 applica ...

  9. Ubuntu系统下《汇编语言》环境配置

    说明 1.系统:Ubuntu codists@pc:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Des ...

随机推荐

  1. MySQL解压版的安装与配置

    1.解压mysql-5.7.15-winx64.zip到D:\MySQL Server 5.7(你想安装的位置) 2.如果机器上安装过其他版本的mysql先删除环境变量PATH中的mysql路径,然后 ...

  2. 获取html下的所有纯文本的方法

    第一种是看别人博客的,第二种是自己发现的. 第一种: #-*- coding: utf8 -*- import re html = """ <div class=& ...

  3. Gird Layout代码解释

    <div class="wrapper"> <!--定义一个类名为wrapper的div盒子--> <div class="one" ...

  4. jQuery 选择同时包含两个或多个class的元素的实现方法

    Jquery选择器 多个 class属性参照以下案例 <element class="a b good list card"> 1. 交集选择: $(".a. ...

  5. java 使用CXF将wsdl文件生成客户端代码命令java调用第三方的webservice应用实例

    1.先下载cxf包https://download.csdn.net/download/suizhikuo/108112362.解压缩包,通过cmd命令进入到bin目录下(cd cxf\bin的路径) ...

  6. NoSQL简单介绍

    这里介绍一下如今经常使用的NoSQL以及各自的特点. NoSQL是2009年突然发展起来的.如今趋于稳定的状态,市场上也有了一些比較成熟的产品. 传统的关系型数据库为了保证通用性的设计而带来了功能复杂 ...

  7. 解决IE11 Array没有find的方法

    IE9以上版本都对 Array中的大部分方法进行了支持,然而在一次浏览器兼容性测试时发现,IE11浏览器不支持Array.find方法 然后查看了一下IE Edge之前的版本是不支持,所以我自己实现了 ...

  8. Android之Wifi学习(1)

    在Android中对Wifi操作,android本身提供了一些实用的包.在android.net.wifi包以下.简介一下: 大致能够分为四个基本的类ScanResult,wifiConfigurat ...

  9. [Java] Windows/Linux路径不同时,统一war的最简办法

    作者: zyl910 一.缘由 在项目开发时,因为运行环境的不同,导致有时得分别为不同的环境,切换配置参数打不同war包.但手工切换配置文件的话,不仅费时费力,而且容易出错. 有些打包工具支持配置切换 ...

  10. 解决CEF中显示Flash动画弹出安全警告问题

    一. 1.Xilium.CefGlue. CefApp (CefApp.cs文件)类on_before_command_line_processing方法内设置flash路径.版本号等. m_comm ...