zoukankan      html  css  js  c++  java
  • Spring boot结合Maven实现不同环境的配置

    配置文件层次:

    pom.xml

     1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     3   <modelVersion>4.0.0</modelVersion>
     4 
     5   <groupId>com.yanwu.www</groupId>
     6   <artifactId>Spring-Env</artifactId>
     7   <version>0.0.1-SNAPSHOT</version>
     8   <packaging>jar</packaging>
     9 
    10   <name>example</name>
    11   <url>http://maven.apache.org</url>
    12   
    13   <parent>
    14         <groupId>org.springframework.boot</groupId>
    15         <artifactId>spring-boot-starter-parent</artifactId>
    16         <version>1.5.6.RELEASE</version>
    17         <relativePath/> <!-- lookup parent from repository -->
    18 </parent>
    19 
    20   <properties>
    21     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    22   </properties>
    23 
    24   <dependencies>
    25     <dependency>
    26       <groupId>junit</groupId>
    27       <artifactId>junit</artifactId>
    28       <version>3.8.1</version>
    29       <scope>test</scope>
    30     </dependency>
    31     <!-- web -->
    32     <dependency>
    33         <groupId>org.springframework.boot</groupId>
    34         <artifactId>spring-boot-starter-web</artifactId>
    35     </dependency>
    36   </dependencies>
    37   
    38   <profiles>
    39     <profile>
    40         <!-- 本地开发环境 -->
    41         <id>dev</id>
    42         <properties>
    43             <profiles.active>dev</profiles.active>
    44         </properties>
    45         <activation>
    46             <activeByDefault>true</activeByDefault>
    47         </activation>
    48     </profile>
    49     <profile>
    50         <!-- 测试环境 -->
    51         <id>sit</id>
    52         <properties>
    53             <profiles.active>sit</profiles.active>
    54         </properties>
    55     </profile>
    56     <profile>
    57         <!-- 生产环境 -->
    58         <id>pro</id>
    59         <properties>
    60             <profiles.active>pro</profiles.active>
    61         </properties>
    62     </profile>
    63     
    64 </profiles>
    65 
    66  <build>
    67     <filters>
    68         <filter>src/main/resources/env/${profiles.active}/application.yml</filter>
    69     </filters>
    70     <!-- 替换${key}内容 -->
    71      <resources>
    72             <resource>
    73                 <filtering>true</filtering>
    74                 <!--要到达最底层目录-->
    75                 <directory>src/main/resources/env/${profiles.active}</directory>
    76             </resource>
    77      </resources>
    78  </build> 
    79   
    80   
    81   
    82   
    83   
    84 </project>
    View Code

    带有${key}的配置文件:

    /Spring-Env/src/main/resources/application.yml

    1 #激活的环境配置
    2 spring:
    3   profiles:
    4     active: ${spring.profiles.active}
    5 #端口
    6 server: 
    7   port: ${server.port}

    配置文件一:

    src/main/resources/env/pro/application.yml

    1 server:
    2   port: 8083
    3   env: pro
    4   
    5 ftp: hello pro
    6 
    7 spring:
    8   profiles:
    9     active: pro

    配置文件二:

    src/main/resources/env/dev/application.yml

    1 server:
    2   port: 8084
    3   
    4 ftp: hello dev
    5 
    6 spring:
    7   profiles:
    8     active: dev

    配置文件三:

    src/main/resources/env/sit/application.yml

    1 server:
    2   port: 8082
    3   env: sit
    4 ftp: hello sit
    5 
    6 spring:
    7   profiles:
    8     active: sit

    运行Spring boot主程序即可实现!

    测试程序:

     1 @RestController
     2 public class SampleController {
     3     
     4      @Autowired
     5      private  Environment env;
     6     
     7     @RequestMapping("/sample")
     8     public String sample(){
     9         
    10         return "spring boot success ! and profile is ==>"+
    11         env.getProperty("spring.profiles.active")+"=====>"+
    12         env.getProperty("ftp");
    13     }
    14 }
    View Code

    maven打包:

    mvn clean install -P pro

    maven打包:

    mvn clean install -P sit

     

    注:maven打包也可以使用eclipse的maven插件

  • 相关阅读:
    EasyUI DataGrid undefined处理
    EasyUI Tabs
    EasyUI ComboBox ajax
    Spring Controller RequestMapping
    Spring前台填充数据
    关于Map集合的遍历总结
    mvc与三层结构终极区别
    关于cron4j的使用
    中国各个省市县的人口统计,echart展示
    windows 安装nodejs 和 npm
  • 原文地址:https://www.cnblogs.com/harvey2017/p/7762286.html
Copyright © 2011-2022 走看看