springboot多环境配置文件及自定义配置文件路径详解
作者:『梧桐雨』 发布时间:2021-09-30 03:55:54
一:什么是classpath?
classpath指的就是 *.java文件,资源文件等编译后存放的位置,对于maven项目就是指 target/classes:这个路径,只要编译后的文件在这个目录下,springboot就可以找到,这里指的是maven项目,javaWeb项目可能会有区别。
二、自定义springboot配置文件路径
springboot项目配置文件application.properties或者application.yml配置文件默认放置的位置是 **“classpath:/,classpath:/config/,file:./,file:./config/ ”**这4个位置。只要我们编译后的文件位于这4个位置,springboot就可以加载配置文件。
但有时候我们需要以环境名称为标识,配置多个环境的配置文件。如下我们需要配置dev1、dev2、stg1、stg2、prd 共5个环境,每个环境下分别配置我们的application.properties,数据库配置,redis配置,日志配置等配置。
这时我们的资源文件的路径已经不再是springboot默认的配置路径了,因此springboot启动时将无法加载配置文件,这里就需要我们在启动类中手动指定配置文件的加载路径了。如下:
public class DemoApplication {
public static void main(String[] args) {
//springboot默认的配置文件路径
// String addClassPath = "spring.config.location:classpath:/";
//自定义的配置文件路径
String addClassPath = "spring.config.additional-location:classpath:/";
addClassPath += ",classpath:/config/";
addClassPath += ",classpath:/config/dev1/";
addClassPath += ",classpath:/config/dev2/";
addClassPath += ",classpath:/config/stg1/";
addClassPath += ",classpath:/config/stg2/";
addClassPath += ",classpath:/config/prd/";
new SpringApplicationBuilder(DemoApplication.class).properties("spring.config.name:application", addClassPath).build().run(args);
}
springboot的加载配置项在ConfigFileApplicationListener类中,可以自行翻看下源码定义。如下是我们从spring源码中复制过来的一分部,可以发现一些我们熟悉默认配置定义,如spring.profiles.active、classpath:/,classpath:/config/,file:./,file:./config/、spring.config.name等。
public class ConfigFileApplicationListener
implements EnvironmentPostProcessor, SmartApplicationListener, Ordered {
private static final String DEFAULT_PROPERTIES = "defaultProperties";
// Note the order is from least to most specific (last one wins)
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";
private static final String DEFAULT_NAMES = "application";
private static final Set<String> NO_SEARCH_NAMES = Collections.singleton(null);
/**
* The "active profiles" property name.
*/
public static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active";
/**
* The "includes profiles" property name.
*/
public static final String INCLUDE_PROFILES_PROPERTY = "spring.profiles.include";
/**
* The "config name" property name.
*/
public static final String CONFIG_NAME_PROPERTY = "spring.config.name";
/**
* The "config location" property name.
*/
public static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";
/**
* The "config additional location" property name.
*/
public static final String CONFIG_ADDITIONAL_LOCATION_PROPERTY = "spring.config.additional-location";
/**
* The default order for the processor.
*/
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
/**
* Name of the application configuration {@link PropertySource}.
*/
@Deprecated
public static final String APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME = "applicationConfigurationProperties";
private final DeferredLog logger = new DeferredLog();
private String searchLocations;
private String names;
private int order = DEFAULT_ORDER;
。。。。。。。。。(略)
来源:https://blog.csdn.net/m0_37735176/article/details/89085843


猜你喜欢
- Spring 配置文件报错:元素 "context:component-scan" 的前缀 "context&
- 引言java中的Math.random()是一个在[0,1)范围等概率返回double数值类型的算法,基于此函数,我们来延申一些随机概率算法
- 目录类划分时关于内聚性的问题静态类的设计高内聚类的设计附:面向过程编程中模块的内聚性偶然内聚或巧合内聚(Coincidental)逻辑内聚(
- 在处理网络请求时,有一部分功能是需要抽出来统一处理的,与业务隔开。登录校验可以利用spring mvc的 * Interceptor,实现H
- 最近做MVC网站时刚好用到,用以提供一个完整的文件夹并压缩下载,正好做个笔记。拷贝文件夹的所有内容到另一个文件夹内:public stati
- tokentoken的意思是“令牌”,是用户身份的验证方式,最简单的token组成:uid(用户唯一的身份标识)、time(当前时间的时间戳
- kafka 架构原理大数据时代来临,如果你还不知道Kafka那就真的out了!据统计,有三分之一的世界财富500强企业正在使用K
- 导语关于<resultMap>标签映射,<association>&<collection>的使用什么时候用<resultMap>标签映射1
- 前言最近项目targetSdkVersion升级到了26,出现很多问题趟了很多坑,其中就包括本篇的需要解决的问题:全局dialog 不显示。
- 获取非公平锁(基于JDK1.7.0_40)非公平锁和公平锁在获取锁的方法上,流程是一样的;它们的区别主要表现在“尝试获取锁的机制不同”。简单
- 今天启动springboot项目时失败了解决检查原因发现是启动类的MapperScan("")的值写到类名了,改成类所在
- 一个应用场景,浏览器上传一个文件,此文件后台调用文件转换,需要耗费相当长的时间,这样,如果是一个线程同步式的做下去,那么用户在浏览器上感觉就
- 一、传入dll前,在C#中申请内存空间c#里面的指针即 IntPtr申请如下:IntPtr SrcImgData = Marshal.All
- 这篇文章主要讲解C#中的泛型,泛型在C#中有很重要的地位,尤其是在搭建项目框架的时候。一、什么是泛型泛型是C#2.0推出的新语法,不是语法糖
- 本文汇集36个Android开发常用经典代码片段,包括拨打电话、发送短信、唤醒屏幕并解锁、是否有网络连接、动态显示或者是隐藏软键盘等,希望对
- 学习Java实现飞机航班管理系统,本文有该系统的功能截图,和数据库设计SQL语句供大家参考1.飞机航班管理系统背景本系统模拟飞机航班管理业务
- 前言不知道大家对const和readonly这两个关键字的区别有什么了解,原来自己之前还真不清楚它们到底是怎么回事,那么如果你也不是很清楚的
- 目录一、Shiro简介 核心角色核心理念二、整合SpringBoot2框架 1、核心依赖2、Shiro核心配置3、域对象
- 一. 安装依赖包yum install -y wgetyum install -y gcc-c++yum install -y zlib-d
- 1.使用WIFI首先设置用户权限<uses-permission android:name="android.permiss