spring boot + maven使用profiles进行环境隔离

简介: 通过maven + springboot进行profiles的切换 spring boot热部署

Spring Profile

Spring可使用Profile决定程序在不同环境下执行情况,包含配置、加载Bean、依赖等。
Spring的Profile一般项目包含:dev(开发), test(单元测试), qa(集成测试), prod(生产环境)。由spring.profiles.active属性决定启用的profile。
SpringBoot的配置文件默认为 application.properties(或yaml,此外仅以properties配置为说明)。不同Profile下的配置文件由application-{profile}.properties管理,同时独立的 Profile配置文件会覆盖默认文件下的属性。

Maven Profile

Maven同样也有Profile设置,可在构建过程中针对不同的Profile环境执行不同的操作,包含配置、依赖、行为等。
Maven的Profile由 pom.xml 的标签管理。每个Profile中可设置:id(唯一标识), properties(配置属性), activation(自动触发的逻辑条件), dependencies(依赖)等。
此文章不对Spring和Maven的Profile作过多说明,详细情况请自行查阅。

spring 多环境

maven增加profiles配置

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <!-- 环境标识,需要与配置文件的名称相对应 -->
                <profile>deve</profile>
            </properties>
            <activation>
                <!-- 默认环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profile>test</profile>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profile>prod</profile>
            </properties>
        </profile>
    </profiles>

application.properties

spring.profiles.active=@profile@

application-deve.properties

management.server.port=9001
management.endpoints.web.base-path=/monitor


management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env


management.endpoint.health.show-details=always

management.endpoint.beans.cache.time-to-live=10s

management.endpoints.web.cors.allowed-origins=http://example.com
management.endpoints.web.cors.allowed-methods=GET,POST

image

  • application.properties中定义激活的环境
  • @profile@与maven中定义的profile保持一致

打包

run --> maven build... --> profile选择对应的环境,如下图则是打的生成的包
image

image

本地运行

通过spring boot参数覆盖的方式执行激活环境
image

image

2018-11-16 14:52:28.211  INFO 2068 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9001 (http) with context path ''
2018-11-16 14:52:28.223  INFO 2068 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-16 14:52:28.224  INFO 2068 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.179 seconds (JVM running for 2.587)

热部署

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>true</scope>
            <optional>true</optional>
        </dependency>


<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
                </configuration>
            </plugin>
        </plugins>
    </build>
相关文章
|
2月前
|
IDE Java Maven
Spring Boot之如何解决Maven依赖冲突Maven Helper 安装使用
Spring Boot之如何解决Maven依赖冲突Maven Helper 安装使用
19 2
|
18天前
|
Java Maven Kotlin
[AIGC] 请你写一遍博客介绍 “使用idea+kotinlin+springboot+maven 结合开发一个简单的接口“,输出markdown格式,用中文回答,请尽可能详细
[AIGC] 请你写一遍博客介绍 “使用idea+kotinlin+springboot+maven 结合开发一个简单的接口“,输出markdown格式,用中文回答,请尽可能详细
|
22天前
|
存储 安全 Java
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
23 0
|
22天前
|
安全 Java 数据库
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(上)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)
49 0
|
1月前
|
Java Maven Spring
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)2
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)
25 2
|
1月前
|
Java Maven 开发工具
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)1
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)
27 2
|
2月前
|
XML 设计模式 Java
springboot创建并配置环境3 - 配置扩展属性(下)
springboot创建并配置环境3 - 配置扩展属性(下)
springboot创建并配置环境3 - 配置扩展属性(下)
|
2月前
|
XML JSON Java
springboot如何创建并配置环境3 - 配置扩展属性(上)
springboot如何创建并配置环境3 - 配置扩展属性(上)
springboot如何创建并配置环境3 - 配置扩展属性(上)
|
2月前
|
Java
springboot创建并配置环境2 - 配置基础环境
springboot创建并配置环境2 - 配置基础环境
springboot创建并配置环境2 - 配置基础环境
|
2月前
|
Java API uml
springboot创建并配置环境1 - 创建环境
springboot创建并配置环境1 - 创建环境
springboot创建并配置环境1 - 创建环境

推荐镜像

更多