Spring Boot 之 Profile 使用 一个应用为了在不同的环境下工作,常常会有不同的配置,代码逻辑处理。Spring Boot 对此提供了简便的支持。...: spring: profiles: active: prod 此外,yml 文件也可以在一个文件中完成所有 profile 的配置: # 激活 prod spring: profiles...: active: prod # 也可以同时激活多个 profile # spring.profiles.active: prod,proddb,prodlog --- # dev 配置 spring...插件激活 profile spring-boot:run -Drun.profiles=prod main 方法激活 profile --spring.profiles.active=prod jar...引申和引用 引申 Spring Boot 教程 参考 Bean Definition Profiles boot-features-profiles
Spring Boot profile用于分离不同环境的参数配置,通过spring.profile.active参数设置使用指定的profile。...在Spring Boot中应用程序配置可以使用2种格式:application.properties 或者 application.yml。...application-dev.yml: 用于开发环境配置 application-test.yml: 用于测试环境配置 application-prod.yml: 用于生产环境配置 在application.yml中设置: spring...: profile: active: dev | test | prod # 设置profile参数 例如:当指定spring.profile.active = dev 后,程序启动时将加载.../article/details/50443491 http://www.jianshu.com/p/01efe59d6a64 http://blog.javachen.com/2016/02/22/profile-usage-in-spring-boot.html
=production 在 Spring Boot 中,您还可以在 application.properties 中设置 active profile ,如下面的示例所示: 可以是一个 spring.profiles.active...设置默认 Profile Name 除了 application.properties 文件,配置文件特定的属性也可以通过以下变数命名原则定义: application-{ profile }。...在 Spring Boot 中,您还可以在 application.properties 中设置默认配置文件名,如下面的示例所示: spring.profiles.default=dev mvn spring-boot...代码中在 bean上配置 @Profile 代码中可借助 @Profile 进行不同 bean 的激活。...idea 2021 中去切换 profile 在 Run/Debug 选项的 Active profiles 可以进行相应配置的激活。
Profile是什么 Profile我也找不出合适的中文来定义,简单来说,Profile就是Spring Boot可以对不同环境或者指令来读取不同的配置文件。...Profile使用 假如有开发、测试、生产三个不同的环境,需要定义三个不同环境下的配置。...spring.profiles.active: prod,proddb,prodmq 基于Java代码 在JAVA配置代码中也可以加不同Profile下定义不同的配置文件,@Profile注解只能组合使用...: // 在Eclipse Arguments里面添加 --spring.profiles.active=prod 插件启动方式: spring-boot:run -Drun.profiles=prod...jar运行方式: java -jar xx.jar --spring.profiles.active=prod 除了在配置文件和命令行中指定Profile,还可以在启动类中写死指定,通过SpringApplication.setAdditionalProfiles
Spring Boot 对此提供了简便的支持。...关键词: @Profile、spring.profiles.active 目录 区分环境的配置 properties 配置 yml 配置 区分环境的代码 修饰类 修饰注解 修饰方法 激活 profile...: spring: profiles: active: prod 此外,yml 文件也可以在一个文件中完成所有 profile 的配置: # 激活 prod spring: profiles...: active: prod # 也可以同时激活多个 profile # spring.profiles.active: prod,proddb,prodlog --- # dev 配置 spring...插件激活 profile spring-boot:run -Drun.profiles=prod main 方法激活 profile --spring.profiles.active=prod jar
一 这是很简单的事,我都不想写 但是用了Maven Profile之后,我打包就不用每次都改application.yml文件了 所以特意记录一下 二 首先你的Spring Profile要有多环境配置文件... prod org.springframework.boot... spring-boot-maven-plugin 修改application.yml spring
前言 因为之前搭建到服务器的博客系统使使用spring boot自带的插件运行的,所以响应速度慢到无以复加。这次的目的是将项目构建成war包,然后发布到服务器。...build.gradle中添加依赖 providedCompile("org.springframework.boot:spring-boot-starter-tomcat") 注:如果之前就已经有该依赖...在项目路径下运行: gradle build -x test 注:-x 是跳过编译选项, -x test则为跳过单元测试 5.
maven中的profile概念,在spring-boot中一样适合,只要约定以下几个规则即可: 一、不同环境的配置文件以"application-环境名.yml"命名 举个粟子: ?.../config下放置一个application.yml来覆盖默认的激活环境(这个可以在打包一键部署的shell脚本中自动处理) 4.2 启动shell脚本中,动态指定,例如 java -jar spring-boot-web-demo...-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod 4.3 也可以参考gradle项目中profile的通用做法(见之前的文章: gradle项目中profile
问题出处 今天,一个简单的demo项目为了方便,使用了spring boot搭建,功能性上考虑不仅要使用spring boot自身的profile功能,还有结合maven的profile功能来编译时即指定环境变量...问题就出在这里了,maven的profile等都配置好了,配置文件中使用${xxx}就是引用不到profile里配置的变量!...问题定位 最后找了很久,在spring-boot-starter-parent .pom里找到了答案,spring boot starter parent里build里resource配置如下 ...exclude明确指定了只有application.properties和application.yml才有资格使用maven里的profile里的properties配置的变量,而spring-boot-starter-parent...又是spring boot 项目必须 依赖的,所有这里别无他法,只有在自己的pom文件里重写resource了,重写后得resource如下 至此问题解决就迎刃而解了 ps小结:本来问题很早就定位到是
前不久,Spring Boot发布了 2.3.0 版本,此版本对 Spring Boot 进行了相当重大的更改,并且这是使用 Gradle 而非 Maven 构建项目的第一个版本。...本文需要讲的是为什么要切换为Gradle而非Maven。 为什么要切换 Spring Boot 团队考虑由 Maven 切换到 Gradle 的主要原因为了减少构建项目所需的时间。...Spring Boot 团队在其他利用 Gradle 作为构建工具的 Spring 项目中看到了 Gradle 的增量和并行构建以及在第三方项目中看到了 Gradle 的构建缓存的好处。...希望通过使用 Gradle 为 Spring Boot 构建获得类似的好处。 Gradle 具有非常灵活的构建模型,并且可以定义每个任务的输入和输出及其相互依赖性。...虽然现有 Gradle 生态系统对 Spring Boot 构建几乎空白,需要从头通过编写很多通用的 gradle 插件来应用到 Spring Boot,但迁移到 Gradle 的提交从代码库中删除了近
最近项目用到了Spring Boot ,但是在控制器返回html视图并渲染参数的时候,存在了疑问。... spring-boot-starter-thymeleaf 2、我现在的项目是用的Gradle...,在build.gradle 里面的dependency加入以下配置: compile "org.springframework.boot:spring-boot-starter-thymeleaf"...二、Spring Boot 控制器Controller的配置,需要使用Model来进行参数传递(或者自定义Map) @RequestMapping("/index") public String index...例如我的是application.properties 里面加入一句: #Thymeleaf cache set spring.thymeleaf.cache=false 重启,配置完成
: "dev" apply from: "profile-${env}.gradle" 大概意思是,根据传入的参数env不同,加载不同的profile文件。...在同级目录下,要放二个文件(下面演示的场景为,dev环境加载的spring版本为4.1.6,而prod环境加载的spring版本为4.2.3): profile-dev.gradle dependencies...:spring-context-support:4.1.6.RELEASE' compile 'org.springframework:spring-aop:4.1.6.RELEASE' } profile-prod.gradle...:spring-beans:4.2.3.RELEASE' compile 'org.springframework:spring-context:4.2.3.RELEASE' } 编译时,gradle...示例源代码: https://github.com/yjmyzz/gradle-profile-sample
运行程序 使用 Gradle 编译: gradle build 运行: java -jar build/libs/docker-spring-boot-gradle-1.0.0.jar 访问项目 如果程序正确运行...@waylau spring-boot]# docker run -p 8080:8080 -t waylau/docker-spring-boot-gradle . ____...[root@waylau spring-boot] The push refers to a repository [docker.io/waylau/docker-spring-boot-gradle...获取项目镜像, 执行 docker pull waylau/docker-spring-boot-gradle 参考引用 http://spring.io/guides/gs/spring-boot-docker.../ https://hub.docker.com/r/waylau/docker-spring-boot-gradle/
本文演示了如何用 Docker、Gradle 来构建、运行、发布来一个 Spring Boot 应用。...@waylau spring-boot]# docker run -p 8080:8080 -t waylau/docker-spring-boot-gradle . ____...[root@waylau spring-boot] The push refers to a repository [docker.io/waylau/docker-spring-boot-gradle...获取项目镜像, 执行 docker pull waylau/docker-spring-boot-gradle 参考引用 http://spring.io/guides/gs/spring-boot-docker.../ https://hub.docker.com/r/waylau/docker-spring-boot-gradle/ 作者:waylau 来源:https://waylau.com/docker-spring-boot-gradle
如何在 Gradle 中配置一个项目可以打包为 Spring Boot 可执行 Jar?...---- 你首先需要添加到 org.springframework.boot 到插件中: 例如下面的代码: plugins { id 'maven-publish' id "org.asciidoctor.convert..." version "2.3.0" id 'org.springframework.boot' version '2.1.4.RELEASE' } 然后应用插件,使用下面的代码: apply plugin...: 'io.spring.dependency-management' 在 Build 界面中添加 bootJar 参数。
如何在 Gradle 中配置一个项目可以打包为 Spring Boot 可执行 Jar?...你首先需要添加到 org.springframework.boot 到插件中: 例如下面的代码: plugins { id 'maven-publish' id "org.asciidoctor.convert..." version "2.3.0" id 'org.springframework.boot' version '2.1.4.RELEASE' } 然后应用插件,使用下面的代码: apply plugin...: 'io.spring.dependency-management' ?
Spring Boot 以Jar的方式部署启动,这个不用介绍了, 之前也介绍了关于 Spring Boot + thymeleaf 的简单使用 ,但是今天遇到一个问题, 我先描述下问题的场景: 由于运维部门的需求...最终发现问题在Thymeleaf的配置,话不多说,具体看操作步骤: 1、Spring boot 容器配置需要继承 SpringBootServletInitializer 这里我继承的是web.suport...-- … --> org.springframework.boot spring-boot-starter-tomcat...-- … --> 如果使用Gradle: dependencies { // … providedRuntime 'org.springframework.boot...:spring-boot-starter-tomcat' // … } 以上步骤配置好,maven or Gradle 在build的时候就会打成war包,这里 可能还需要注意一个编码的问题
文章目录 Pre Spring Boot 中的配置体系 配置文件与 Profile 主 application.properties 中指定激活的Profile Profile 配置信息只保存在一个文件...当然,今天我们关注的主要是如何理解并使用 Spring Boot 中的配置信息组织方式,这里就需要引出一个核心的概念,即 Profile。...,而 profile 则用来指定该配置文件所对应的环境 在 Spring Boot 中,配置文件同时支持 .properties 和 .yml 两种文件格式,结合 label 和 profile 概念,...配置信息只保存在一个文件中而不是分散在多个配置文件中, Spring Boot 也是支持的,需要做的事情只是对这些信息按 Profile 进行组织、分段,如下所示: spring:...---- 代码控制与Profile 在 Spring Boot 中,Profile 这一概念的应用场景还包括动态控制代码执行流程。为此,我们需要使用 @Profile 注解,先来看一个简单的示例。
通过Profile配置,Spring可以在根据环境在运行阶段来决定bean的创建与否,先举例如下,主要从Profile bean的配置和激活来展开。...从Spring 3.1版本中就引入了bean profile的功能,可以让你将不同的bean定义到一个或者多个profile里,然后在部署应用时告知要激活那个profile,则相应的bean就会被创建。...这里我们需要两个属性spring.profile.active以及spring.profile.default。 ...如果spring.profile.active被赋值了,则spring.profile.default就不会起作用,如果spring.profie.active没有赋值,则使用默认的spring.profile.default...另外对于测试,spring为什么提供了一个简单的注解可以使用@ActiveProfiles,它可以指定运行测试的时候应该要激活那个profile。
概述 将工程托管到Github Gradle构建 为什么一个main函数就能启动web并提供这么多功能 幕后的 Spring Boot 分发器和 multipart 配置 视图解析器、静态资源以及区域配置...错误与转码配置 嵌入式 Servlet 容器(Tomcat)的配置 HTTP 端口 SSL 配置 其他配置 概述 Spring Tool Suit + Gradle 构建第一个Spring Boot...:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse...:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') } 在这里... 对 Spring Boot 插件的依赖,它分布在 Maven 中央仓库中。 我们的项目是 Java 工程。Gradle 可以为 Eclipse 生成 IDE 工程文件。
领取专属 10元无门槛券
手把手带您无忧上云