一个spring boot 项目在开发环境、测试环境、生产环境下,好多的配置都是不尽相同的。所以配置多分的资源文件,仅仅在部署在不同环境的时候,选择激活不同的资源文件就可以实现多环境的部署。

项目结构如下:

1.配置多个环境下的不同的资源文件

多个资源文件的格式如下:

application-{profile}.properties

{profile}自定义的不同环境标识,本项目中分别对应如下:

==========================================================================

列出各个环境下的资源文件内容:

application-dev.properties  开发资源文件

application-pro.properties  生产资源文件

application-test.properties     测试资源文件

2.主资源文件中 选择激活一种环境下的资源文件

spring.profiles.active=dev

dev就是上面一种资源文件的自定义标识

3.绑定到一个bean,提供给程序中使用

package com.sxd.beans;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "com.sxd")
public class ConfigBean { private String ip;
private String value; public String getIp() {
return ip;
} public void setIp(String ip) {
this.ip = ip;
} public String getValue() {
return value;
} public void setValue(String value) {
this.value = value;
}
}

4.程序主入口,激活绑定的bean,顺便使用了

package com.sxd.firstdemo;

import com.sxd.beans.ConfigBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})
public class FirstdemoApplication { @Autowired
ConfigBean configBean; @RequestMapping("/")
public String index(){ return "IP:"+configBean.getIp()+"\n环境:"+configBean.getValue();
}
public static void main(String[] args) {
SpringApplication.run(FirstdemoApplication.class, args);
}
}

5.启动并访问  ,当前激活的是开发环境资源文件

==================================================================================================================

spring.profiles.active=dev

是选择一种资源文件

spring.profiles.include=dev,test,pro

可以叠加多个资源文件

【spring boot】4.spring boot配置多环境资源文件的更多相关文章

  1. 学记:为spring boot写一个自动配置

    spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...

  2. Spring Boot 部署与服务配置

    Spring Boot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函数入口启动.其内置Tomcat容器或Jetty容器,具体由配置来决定(默认Tomcat).当然你也可以将项 ...

  3. Spring Boot 探索系列 - 自动化配置篇

    26. Logging Prev  Part IV. Spring Boot features  Next 26. Logging Spring Boot uses Commons Logging f ...

  4. 十六、Spring Boot 部署与服务配置

    spring Boot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函数入口启动.其内置Tomcat容器或Jetty容器,具体由配置来决定(默认Tomcat).当然你也可以将项 ...

  5. Spring Boot 2.0 教程 | 配置 Undertow 容器

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发于个人网站 https://ww ...

  6. spring boot +mybatis(通过properties配置) 集成

    注:日常学习记录贴,下面描述的有误解的话请指出,大家一同学习. 因为我公司现在用的是postgresql数据库,所以我也用postgresql进行测试 一.前言 1.Spring boot 会默认读取 ...

  7. Spring Boot + Spring Cloud 实现权限管理系统 配置中心(Config、Bus)

    技术背景 如今微服务架构盛行,在分布式系统中,项目日益庞大,子项目日益增多,每个项目都散落着各种配置文件,且随着服务的增加而不断增多.此时,往往某一个基础服务信息变更,都会导致一系列服务的更新和重启, ...

  8. Spring Boot之实现自动配置

    GITHUB地址:https://github.com/zhangboqing/springboot-learning 一.Spring Boot自动配置原理 自动配置功能是由@SpringBootA ...

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

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

随机推荐

  1. [USACO1.3]虫洞

    Luogu链接 题目描述 农夫约翰爱好在周末进行高能物理实验的结果却适得其反,导致N个虫洞在农场上(2<=N<=12,n是偶数),每个在农场二维地图的一个不同点. 根据他的计算,约翰知道他 ...

  2. vivo面试学习3(git和svn的区别)

    git和svn有什么区别? svn: 系统特点: 1).集中式版本控制系统(存在一个中央版本库,所有开发人员所使用的代码都是来源于版本库,提交代码也是这个中央版本库) 2).企业内部并行集中开发 3) ...

  3. bzoj 3224 裸平衡树

    裸的平衡树,可以熟悉模板用,写题写不出来的时候可以A以下缓解下心情. /************************************************************** P ...

  4. Ubuntu 15.10 安装比特币客户端

    下载 git clone https://github.com/bitcoin/bitcoin.git cd bitcoin ./autogen.sh 安装依赖包: ++-dev sudo apt-g ...

  5. shell脚本复制文件夹内容到另外的文件夹,如果存在则自动备份

    有时我们需要将一个文件夹覆盖到我们的工作目录,但需要自动备份已经存在的文件,一个一个去备份太麻烦了,全部备份又没有必要.shell脚本可以很好滴完成这个任务.原文链接http://back.zhizh ...

  6. linux下卸载Oracle

    1.卸载数据库软件--10g[oracle]# cd /u01/app/oracle/product/10.2.0/db_1/oui/bin[oracle]# ./runInstaller -igno ...

  7. delphi string,pchar,char的比较

    来自:http://blog.163.com/kat_/blog/static/189284269201152513331999/ ---------------------------------- ...

  8. tushrea知识笔记

    生成时间序列: dates = pandas.date_range('2013-01-01',periods = 6) Pandas读取excel数据: df=pd.read_excel(" ...

  9. 深入理解Java的注解(Annotation):自定义注解入门(2)

    要深入学习注解,我们就必须能定义自己的注解,并使用注解,在定义自己的注解之前,我们就必须要了解Java为我们提供的元注解和相关定义注解的语法. 元注解: 元注解的作用就是负责注解其他注解.Java5. ...

  10. React Native - 2 控件Flexbox

    *强烈建议使用Genymotion模拟器,比AVD速度快,功能强大.   1. flexDirection Flexbox是连续布局,它有主轴(primary axis)和交叉轴(cross axis ...