zoukankan      html  css  js  c++  java
  • Spring 环境与profile(三)——利用maven的resources、filter和profile实现不同环境使用不同配置文件

    基本概念 

    • profiles定义了各个环境的变量id 
    • filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profile中定义的值 
    • resources中是定义哪些目录下的文件会被配置文件中定义的变量替换

    原理

    利用filter实现对资源文件(resouces)过滤

    maven filter可利用指定的xxx.properties中对应的key=value对资源文件中的${key}进行替换,最终把你的资源文件中的username=${key}替换成username=value 

    利用profile来切换环境

    示例

    代码结构

    db.properties

    jdbc.username=${jdbc.username}
    jdbc.password=${jdbc.password}
    jdbc.url=${jdbc.url}
    name=${myName}

    dev.properties

    jdbc.url=jdbc:mysql://127.0.0.1:3306/devdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
    jdbc.username=devuser
    jdbc.password=dev123456

    product.properties

    jdbc.url=jdbc:mysql://127.0.0.1:3306/productdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
    jdbc.username=productuser
    jdbc.password=product123456

    test.properties

    jdbc.url=jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
    jdbc.username=testuser
    jdbc.password=test123456

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>mavenImparityProfile</groupId>
      <artifactId>mavenImparityProfile</artifactId>
      <packaging>war</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>mavenImparityProfile Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <profiles>
          <profile>
              <id>test</id>
              <properties>
                  <env>test</env><!--相当于定义一个变量 供下面使用-->
                  <myName>张三</myName><!--使用一个properties文件中未定义,但是其他地方会取值的变量-->
              </properties>
              <activation><!--默认激活-->
                  <activeByDefault>true</activeByDefault>
              </activation>
          </profile>
          <profile>
              <id>dev</id>
              <properties>
                  <env>dev</env>
                  <myName>李四</myName>
              </properties>
              <activation><!--默认激活-->
                  <activeByDefault>false</activeByDefault>
              </activation>
          </profile>
          <profile>
              <id>product</id>
              <properties>
                  <env>product</env>
              </properties>
          </profile>
      </profiles>
    
      <build>
        <finalName>mavenImparityProfile</finalName>
          <filters> <!--filters中定义了变量配置文件的地址,其中地址中的环境变量env就是上面profile中定义的值-->
              <filter>src/main/resources/properties/${env}.properties</filter>
          </filters>
    
          <resources>
            <resource> <!--resources中是定义哪些目录下的文件会被配置文件中定义的变量替换,一般我们会把项目的配置文件放在src/main/resources下-->
                <directory>src/main/resources</directory>
                 <filtering>true</filtering> <!--是否使用过滤器-->
             </resource>
         </resources>
      </build>
    
    </project>

    代码

    地址

  • 相关阅读:
    201771010125王瑜《面向对象程序设计(Java)》第十周学习总结
    201771010125王瑜《面向对象程序设计(Java)》第九周学习总结
    201771010125王瑜《面向对象程序设计(Java)》第八周学习总结
    7 Python外壳:代码结构
    6 python容器
    元组
    列表
    4 python基本元素之变量
    3 关于操作系统基本了解
    1 python是什么?
  • 原文地址:https://www.cnblogs.com/kaituorensheng/p/8783014.html
Copyright © 2011-2022 走看看