springboot 多环境配置

1.案例简单介绍

  • 1.1项目结构
    springboot 多环境配置_第1张图片
  • 1.2 yml中的内容(不包括application.yml)

2.不借助maven的profile

  • 2.1 application.yml中的内容----->这里指定dev为默认环境配置
spring:
  profiles:
    active: dev
  • 2.2 打包----->在项目根目录下运行如下命令
mvn clean package -Dmaven.test.skip=true
  • 2.3 项目部署 ----->在jar包所在目录运行如下命令
java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod

等号后面等于prod则实际使用的是application-prod.yml文件里的配置,等于test则实际使用的是application-test.yml文件里的配置,以此类推,去掉 --spring.profiles.active=prod,则使用默认的application-dev.yml文件里的配置.

3.借助maven的profile标签

  • 3.1 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.2.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.nrsc.applicationgroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demoname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <profiles>
        
        <profile>
            <id>devid>
            <properties>
                <packaging>jarpackaging>
                <activatedProperties>devactivatedProperties>
                <main-class>com.nrsc.application.demo.DemoApplicationmain-class>
            properties>
            <activation>
                
                <activeByDefault>trueactiveByDefault>
            activation>
        profile>
        <profile>
            <id>testid>
            <properties>
                <packaging>jarpackaging>
                <activatedProperties>testactivatedProperties>
                <main-class>com.nrsc.application.demo.DemoApplicationmain-class>
            properties>
        profile>
        <profile>
            <id>uatid>
            <properties>
                <packaging>jarpackaging>
                <activatedProperties>uatactivatedProperties>
                <main-class>com.nrsc.application.demo.DemoApplicationmain-class>
            properties>
        profile>
        <profile>
            <id>prodid>
            <properties>
                <packaging>jarpackaging>
                <activatedProperties>prodactivatedProperties>
                <main-class>com.nrsc.application.demo.DemoApplicationmain-class>
            properties>
        profile>
        
    profiles>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        
        
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>
  • 3.2 application.yml中需要修改成下面的样子
spring:
  profiles:
    active: @activatedProperties@
  • 3.3 打包和部署同不借助maven的profile标签中内容相同.

4. 两者区别

我所知道的区别主要在idea的显示上,描述如下:
springboot 多环境配置_第2张图片

5 本demo的地址

你可能感兴趣的:(springboot,多环境配置,项目架构相关)