spring boot配置项profiles active
结论:通用项配置在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的更多相关文章
- spring boot maven profiles,打包不同的配置文件
1. 在pom.xml添加 <profiles> <profile> <id>dev</id> <properties> <envir ...
- Spring Boot features - Profiles
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html https://w ...
- Spring Boot 启动(二) Environment 加载
Spring Boot 启动(二) Environment 加载 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 上一节中 ...
- 在Spring Boot启动后执行指定代码
在开发时有时候需要在整个应用开始运行时执行一些特定代码,比如初始化环境,准备测试数据等等. 在Spring中可以通过ApplicationListener来实现相关的功能,不过在配合Spring Bo ...
- 将 Spring boot 项目打成可执行Jar包,及相关注意事项(main-class、缺少 xsd、重复打包依赖)
最近在看 spring boot 的东西,觉得很方便,很好用.对于一个简单的REST服务,都不要自己部署Tomcat了,直接在 IDE 里 run 一个包含 main 函数的主类就可以了. 但是,转念 ...
- 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 ...
- Spring Boot 启动:No active profile set, falling back to default profiles: default
启动 Spring Boot 失败,但是没有出现多余的异常信息: 检查之后发现是依赖的问题(之前依赖的是 spring-boot-starter),修改即可: 方法二: pom.xml加上下面两个依赖 ...
- spring boot 入门 使用spring.profiles.active来分区配置
很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同的环境 ...
- spring boot 入门 使用spring.profiles.active来分区配置(转)
很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同的环境 ...
随机推荐
- Solr安装(单机版)
本文记录的是solr在win下安装配置使用的过程,最后将solr部署到Linux上通过远程访问.下一篇文章会介绍 solr集群搭建(SolrCloud) 的安装! Solr是基于Lucene ...
- oracle在没
db_user表中数据: name age nub 张三 13 13 张三 12 12 张三 12 13 张三 12 13 李四 12 12 李四 12 12 查询重复数据(一条) sql:selec ...
- Redis持久化rdb&aof
Redis持久化rdb&aof 前言 持久化:即把数据存储于断电后不会丢失的设备中,通常是硬盘 常见的持久化方式: 主从:通过从服务器保持持久化,如mongoDB的replication se ...
- UIDynamic物理引擎
iOS开发拓展篇—UIDynamic(简单介绍) 一.简单介绍 1.什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能模拟 ...
- 大数据数据库HBase(一)——架构原理
一.HBase简介 1.1.Hadoop生态系统 1.2.非关系型数据库知识面扩展 Cassandra hbase mongodb Couchdb,文件存储数据库 Neo4j非关系型图数据库 1.3 ...
- qt04 中文显示问题
sockettools识别GB2312,接收qt server 数据时 QByteArray ba = str.toLocal8Bit(); char *ss = ba.data(); obj-> ...
- vue 之 双向绑定原理
一.实现双向绑定 详细版: 前端MVVM实现双向数据绑定的做法大致有如下三种: 1.发布者-订阅者模式(backbone.js) 思路:使用自定义的data属性在HTML代码中指明绑定.所有绑定起来的 ...
- springboot 初探 、基础及配置
一.spring boot 的核心功能 独立运行的spring项目.内嵌servlet容器.提供starter简化maven配置.自动配置Spring.准生产的应用监控.无代码生成和xml配置二.sp ...
- ZROI 19.07.30 简单图论/kk
1.最短路 NOI2019 D2T1 我被这题送Fe了/lb 只有zz才会写二维线段树,比如我. 实际上你只需要矩形取min就可以. kd-tree可以随便过,最慢的点\(0.1s\). 另外一种简单 ...
- 设计模式Design Pattern(1)--简介
什么是设计模式? 软件开发人员在长期实践中总结出来的解决特定问题的一套解决方案. 对象设计原则 计模式主要是基于以下的面向对象设计原则. 对接口编程而不是对实现编程. 优先使用对象组合而不是继承. 设 ...