Profile是什么

Profile我也找不出合适的中文来定义,简单来说,Profile就是Spring Boot可以对不同环境或者指令来读取不同的配置文件。

Profile使用

假如有开发、测试、生产三个不同的环境,需要定义三个不同环境下的配置。

基于properties文件类型

你可以另外建立3个环境下的配置文件:

applcation.properties

application-dev.properties

application-test.properties

application-prod.properties

然后在applcation.properties文件中指定当前的环境:

spring.profiles.active=test

这时候读取的就是application-test.properties文件。

基于yml文件类型

只需要一个applcation.yml文件就能搞定,推荐此方式。

spring:
profiles:
active: prod ---
spring:
profiles: dev server:
port: 8080 ---
spring:
profiles: test server:
port: 8081 ---
spring.profiles: prod
spring.profiles.include:
- proddb
- prodmq server:
port: 8082 ---
spring:
profiles: proddb db:
name: mysql ---
spring:
profiles: prodmq mq:
address: localhost

此时读取的就是prod的配置,prod包含proddb,prodmq,此时可以读取proddb,prodmq下的配置。

也可以同时激活三个配置。

spring.profiles.active: prod,proddb,prodmq

基于Java代码

在JAVA配置代码中也可以加不同Profile下定义不同的配置文件,@Profile注解只能组合使用@Configuration和@Component注解。

@Configuration
@Profile("prod")
public class ProductionConfiguration { // ... }

指定Profile

main方法启动方式:

// 在Eclipse Arguments里面添加
--spring.profiles.active=prod

插件启动方式:

spring-boot:run -Drun.profiles=prod

jar运行方式:

java -jar xx.jar --spring.profiles.active=prod

除了在配置文件和命令行中指定Profile,还可以在启动类中写死指定,通过SpringApplication.setAdditionalProfiles方法。

SpringApplication.class

public void setAdditionalProfiles(String... profiles) {
this.additionalProfiles = new LinkedHashSet<String>(Arrays.asList(profiles));
}

推荐:Spring Boot & Cloud 最强技术教程

Spring Boot - Profile配置的更多相关文章

  1. spring boot 环境配置(profile)切换

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  2. Spring Boot 属性配置和使用

    Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...

  3. Spring Boot 属性配置和使用(转)

    Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...

  4. 转:spring boot log4j2配置(使用log4j2.yml文件)---YAML 语言教程

    转:spring boot log4j2配置(使用log4j2.yml文件) - CSDN博客http://blog.csdn.net/ClementAD/article/details/514988 ...

  5. Spring Boot 日志配置

    Spring Boot 日志配置 默认日志 Logback: 默认情况下,Spring Boot会用Logback来记录日志,并用INFO级别输出到控制台.在运行应用程序和其他例子时,你应该已经看到很 ...

  6. Spring Boot 属性配置&自定义属性配置

    在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...

  7. Spring Boot自定义配置与加载

    Spring Boot自定义配置与加载 application.properties主要用来配置数据库连接.日志相关配置等.除了这些配置内容之外,还可以自定义一些配置项,如: my.config.ms ...

  8. Spring Boot常用配置

    概述 本文主要写了下Spring Boot的一些常用配置. Spring Boot基本配置 入口类: Spring Boot通常有一个名为*Application的入口类,入口类里面有一个main方法 ...

  9. Spring Boot - Logback配置日志要考虑哪些因素

    Spring Boot - Logback配置日志 出于性能等原因,Logback 目前是springboot应用日志的标配: 当然有时候在生产环境中也会考虑和三方中间件采用统一处理方式.@pdai ...

随机推荐

  1. Django提交文件的方式

    以上传图片为列, 提交图片文件后能在不刷新页面的前提下显示图片 首先写出一个简单的提交文件的页面: <h>文件上传</h> 选择文件<input type="f ...

  2. DJango 基础 (5)

    模板加载静态文件 在settings.py文件中添加STATICFILES_DIRS,设置静态文件目录路径,同templates. # settings.py文件中​STATIC_URL = '/st ...

  3. 663. Equal Tree Partition 能否把树均分为求和相等的两半

    [抄题]: Given a binary tree with n nodes, your task is to check if it's possible to partition the tree ...

  4. 视觉和imu融合的算法研究

    融合方式概述 同SLAM发展过程类似,视觉融合IMU问题也可以分成基于滤波和基于优化两大类. 同时按照是否把图像特征信息加入状态向量来进行分类,可以分为松耦合和紧耦合两大类. 一.基于滤波的融合算法 ...

  5. javascript 新建实例对象

    在main js里面new 这样一个实例算怎么回事,如果不这么new, ToolBar里就会报错: Portal.gbl = { constants : new Portal.common.Const ...

  6. linux学习第八天 (Linux就该这么学)

    今天学了,mount 挂载,umount撤销挂载,.fdisk 命令 管理硬盘 交换分区swap,硬盘配额 xfs_quota命令 今天工作,手机看了,看的不全,回头看录播了.

  7. Netsharp总体介绍

    作者:秋时   日期:2014年02月05日   转载须说明出处  Netsharp交流群:338963050(请有详细的请求说明) Netsharp系列文章目录结构 Netsharp是一款免费的基于 ...

  8. log4net 写日志

    转载地址:https://www.cnblogs.com/vichin/p/6022612.html   //基本使用 https://www.cnblogs.com/genesis/p/498562 ...

  9. js--随机产生100个从0 ~ 1000之间不重复的整数(me)

    <style>       div{text-indent:40px;} </style> <script> window.onload=function(){ v ...

  10. solr7.7.0搜索引擎使用(四)(搜索语法)

    solr搜索语法 参数defType   指定用于处理查询语句(参数q的内容)的查询解析器,eg:defType=lucenesort    指定响应的排序方式:升序asc或降序desc.同时需要指定 ...