结论:通用项配置在applicaton.yml,区别环境配置在application-{profile}.yml中

一直不知道这个参数要不要配,配了有什么用,今天搭一个工程来检验

此项作用:用来区分不同环境配置

application-dev.yml 开发环境

application-test.yml 测试环境

application-prod.yml 生产环境

applicaton.yml(通用项配置)用spring.profile.active=dev来决定启用上面的哪个环境配置文件(不同环境不同配置)

1.搭个简单的spring boot工程

此处省略过程,详细可参考。

结果目录结构

注:在服务器上时,yml文件与jar包同一级,若需指定路径时,启动脚本中,带参数--spring.config.location=/home/ap/testapp/myproject/config/

2.文件内容

application-dev.yml

application-test.yml

application-prod.yml

application.yml

spring:
application:
name: profileactivetest-service
profiles:
active: dev
server:
port: 8707

3.编写controller类ProfileTestController.java

package com.example.profileactivetest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @Title:
* @Auther: test
* @Date: 2019/7/3 8:54
* @Version: 1.0
* @Description:
*/
@RestController
public class ProfileTestController {
@Value("${envUrl}")
private String envUrl; @RequestMapping("/getEnvUrl")
public String getParam(){
return "My envUrl configed as :"+envUrl;
}
}

3.启动测试

当application.yml中spring.profile.active=prod时

spring:
application:
name: profileactivetest-service
profiles:
active: prod
server:
port: 8707

4.附

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<!--此工程没有加入聚合工程管理-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>profileactivetest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>profileactivetest</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

启动类

package com.example.profileactivetest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ProfileactivetestApplication { public static void main(String[] args) {
SpringApplication.run(ProfileactivetestApplication.class, args);
} }

spring boot配置项profiles active的更多相关文章

  1. spring boot maven profiles,打包不同的配置文件

    1. 在pom.xml添加 <profiles> <profile> <id>dev</id> <properties> <envir ...

  2. Spring Boot features - Profiles

    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html https://w ...

  3. Spring Boot 启动(二) Environment 加载

    Spring Boot 启动(二) Environment 加载 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 上一节中 ...

  4. 在Spring Boot启动后执行指定代码

    在开发时有时候需要在整个应用开始运行时执行一些特定代码,比如初始化环境,准备测试数据等等. 在Spring中可以通过ApplicationListener来实现相关的功能,不过在配合Spring Bo ...

  5. 将 Spring boot 项目打成可执行Jar包,及相关注意事项(main-class、缺少 xsd、重复打包依赖)

    最近在看 spring boot 的东西,觉得很方便,很好用.对于一个简单的REST服务,都不要自己部署Tomcat了,直接在 IDE 里 run 一个包含 main 函数的主类就可以了. 但是,转念 ...

  6. How to set spring boot active profiles with maven profiles

    In the previous post you could read about separate Spring Boot builds for a local development machin ...

  7. Spring Boot 启动:No active profile set, falling back to default profiles: default

    启动 Spring Boot 失败,但是没有出现多余的异常信息: 检查之后发现是依赖的问题(之前依赖的是 spring-boot-starter),修改即可: 方法二: pom.xml加上下面两个依赖 ...

  8. spring boot 入门 使用spring.profiles.active来分区配置

    很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同的环境 ...

  9. spring boot 入门 使用spring.profiles.active来分区配置(转)

    很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同的环境 ...

随机推荐

  1. 2019 上海市大学生网络安全大赛 RE部分WP

    这次比赛就做了这一道逆向题,看到队友的WP,下面的对v10的加密方式为RC4,从我提取的v4数组就能够察觉出这是CR4了,自己傻乎乎的用OD调试,跟踪数据半天才做出来,还是见得的少了... ...下面 ...

  2. Hive导入导出数据的方法

    Hive导入数据的方式 官网文档: LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (p ...

  3. 已知链表头结点指针head,写一个函数把这个链表逆序

    Node* ReverseList ( Node *head ) { if ( head == NULL || head->next == NULL ) return head; Node *p ...

  4. uwsgi支持http长链接

    http1.1支持长链接,而http1.0不支持,所以,在切换http版本号或者升级服务端版本时候,尤其要注意这个造成的影响. 当客户端以http1.1长链接方式连接服务端时,服务端如果不支持1.1, ...

  5. #pragma 的使用(转)

    尽管 C 和 C++ 都已经有标准,但是几乎每个编译器 (广义,包含连接器等) 扩展一些 C/C++ 关键字. 合理地应用这些关键字,有时候能使我们的工作非常方便.下面随便说说 Visual C++ ...

  6. DevExpress v18.2版本亮点——Analytics Dashboard篇(一)

    行业领先的.NET界面控件——DevExpress v18.2版本亮点详解,本文将介绍了DevExpress Analytics Dashboard v18.2 的版本亮点,新版30天免费试用!点击下 ...

  7. html2canvas 把h5网页保存为图片 区域保存

    html2canvas 把h5网页保存为图片 想把一个网页得某些元素,绘制成图片保存,有些数据是接口动态加载的,所以不能UI给到图片,需要我们把api的数据也绘制到图片上 html2canvas这个插 ...

  8. axios中put和patch的区别(都是update , put是需要提交整个对象资源,patch是可以修改局部)

    patch方法用来更新局部资源,这句话我们该如何理解? 假设我们有一个UserInfo,里面有userId, userName, userGender等10个字段.可你的编辑功能因为需求,在某个特别的 ...

  9. koa2基础

    1.安装脚手架 npm install -g koa-generator 2.创建目录 koa2 -e koa2-learn 3.cd koa2-learn 4.npm install 5.SET D ...

  10. 华为云服务器centos7.3安装tomcat

    1. 进入tomcat官网,复制下载地址 https://tomcat.apache.org/download-80.cgi#8.5.47 鼠标右键,复制链接地址:http://mirrors.tun ...