一个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. 【Python实例二】之前期准备:Windows下的BeautifulSoup安装

    前言 一直久闻Python的爬虫很高效,而且操作便捷,因此决定开始练习爬虫的相关内容. 首先尝试的是Python的爬虫利器之一:BeautifulSoup.(这名字听起来就有种想要去探究的兴趣.... ...

  2. LeetCode 10 Regular Expression Match

    '.' Matches any single character.'*' Matches zero or more of the preceding element. The matching sho ...

  3. swift网址

    http://www.cocoachina.com/industry/20140613/8818.html Swift -- 中文版两大官方文档汇总发布于:2014-06-13 15:34阅读数:22 ...

  4. HDU1024(最大M子段和)

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. HDU1385 (Floyd记录路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  6. Linux c括号作用域【原创笔记】

    大师指点后,所做的笔记,很感谢一起愿意研究技术的同事,以下不是本人原创,是他分析的成果 #include <stdio.h> #include <time.h> struct ...

  7. python基础===两个list合并成一个dict的方法

    def Run(): list2 = [, , , , ]; list3 = ["a", "b", "c", "d",& ...

  8. 《Java编程思想》笔记 第四章 控制执行流程

    1.true和false if--else if--else, while, do--while 都使用条件表达式的真假来决定执行路径. Java不允许数字作为真假判断,C和C++可以非0即真. 2. ...

  9. ubuntu 16.04安装redis群集zz

    之前有文章,写明了如何安装redis.这里,进行群集配置. 创建Redis配置目录 /etc/redis: $ sudo mkdir /etc/redis/redis_cluster $cd /etc ...

  10. VMware vCenter Server安装与配置

    预先准备好安装包 ESXI6    VMware-VMvisor-Installer-6.0.0.update01-3073146.x86_64.iso VC        VMware-VIMSet ...