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>
相关文章
|
10天前
|
Cloud Native Java 对象存储
面向未来的架构设计:Spring Cloud和Netflix OSS在云原生环境下的发展趋势
展望未来,随着5G、边缘计算等新技术的兴起,微服务架构的设计理念将会更加深入人心,Spring Cloud和Netflix OSS也将继续引领技术潮流,为企业带来更为高效、灵活且强大的解决方案。无论是对于初创公司还是大型企业而言,掌握这些前沿技术都将是在激烈市场竞争中脱颖而出的关键所在。
26 0
|
2月前
|
Java Maven Spring
SpringBoot 系列之 Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resource
这篇文章描述了在使用Maven构建Spring Boot项目时遇到的`maven-resources-plugin`插件版本问题导致的编译失败,并提供了通过修改插件版本至3.1.0来解决这个问题的方法。
SpringBoot 系列之 Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resource
|
2月前
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
36 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
|
2月前
|
SQL 前端开发 Java
在IDEA中使用Maven将SpringBoot项目打成jar包、同时运行打成的jar包(前后端项目分离)
这篇文章介绍了如何在IntelliJ IDEA中使用Maven将Spring Boot项目打包成可运行的jar包,并提供了运行jar包的方法。同时,还讨论了如何解决jar包冲突问题,并提供了在IDEA中同时启动Vue前端项目和Spring Boot后端项目的步骤。
在IDEA中使用Maven将SpringBoot项目打成jar包、同时运行打成的jar包(前后端项目分离)
|
2月前
|
前端开发 Java 测试技术
单元测试问题之在Spring MVC项目中添加JUnit的Maven依赖,如何操作
单元测试问题之在Spring MVC项目中添加JUnit的Maven依赖,如何操作
|
2月前
|
Java Maven
SpringBoot 引用仓库中没有 第三方包 - 将jar 包安装本地 maven
SpringBoot 引用仓库中没有 第三方包 - 将jar 包安装本地 maven
21 0
|
3月前
|
Java Maven Spring
如何使用Maven构建SpringBoot项目
如何使用Maven构建SpringBoot项目
|
3月前
|
Java Maven
SpringBoot第一次导入项目,Maven依赖全爆红,该怎样解决,idea2019.3版本,必须用application2.7.6或者以下
SpringBoot第一次导入项目,Maven依赖全爆红,该怎样解决,idea2019.3版本,必须用application2.7.6或者以下
|
4月前
|
Java 项目管理 Maven
Java一分钟之-Maven profiles与dependencyManagement
【6月更文挑战第5天】本文探讨了Maven的profiles和dependencyManagement特性在Java项目管理中的应用,包括基本概念和常见问题。Profiles用于根据不同环境激活配置,易错点在于忘记激活,应通过命令行或设置默认profile来避免。dependencyManagement集中管理依赖版本,过度依赖会导致子模块灵活性降低,应合理使用。结合两者,可在不同环境中控制依赖版本,提高项目配置效率。
82 8
|
4月前
|
XML Java 数据格式
SpringBoot Profiles特性
SpringBoot Profiles特性
下一篇
无影云桌面