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
猜你喜欢
- 周末这天手痒,正好没事干,想着写一个分页的例子出来给大家分享一下。这个案例分前端和后台两部分,前端使用面向对象的方式写的,里面用到了一些回调
- 简介使用配置类和注解代替web.xml和SpringMVC配置文件的功能在Servlet3.0环境中,容器会在类路径中查找实现javax.s
- 本文实例讲述了Java访问WebService返回XML数据的方法。分享给大家供大家参考。具体如下:import java.io.IOExc
- * 的实现使用的模式:代理模式。代理模式的作用是:为其他对象提供一种代理以控制对这个对象的访问。类似租房的中介。两种 * :(1)jd
- 前言面向切面(AOP)Aspect Oriented Programming是一种编程范式,与语言无关,是一种程序设计思想,它也是sprin
- 解决Spring in action @valid验证不生效按照书上的示例代码来实现但是,添加了验证但是没有生效。Spring提供了校验Ap
- 1、Jetbrains官网下载IntelliJ IDEA1.1 官方网站http://www.jetbrains.com/idea/&nbs
- 本篇使用java自带的MessageDigest实现对文本的md5加密算法,具体代码如下: /** *@Description
- 大家好,在这篇文章中,我们将学习如何添加动画,同时从一个页面到其他在 Flutter。我们将覆盖不同类型的动画和实现基本动画 Flutter
- Mybatis-Plus是一个优秀的Mybatis增强工具,目前更新到3.1.1。Mybatis-Plus原生提供了很多单表操作的方法,极大
- 在Android Studio中对一个自己库进行生成操作时将会同时生成*.jar与*.aar文件。分别存储位置: &n
- java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的。静态方法是属于类的,不是属于类的对象。所以可以直接使用类名加
- 背景产品想对多次快速点击做一下优化,想要的效果就是双击不会打开多次但是从开发角度来说,我可以用kotlin的拓展方法来调整这个,但是之前的历
- 三目条件运算公式为 x?y:z 其中x的运算结果为boolean类型,先计算x的值,若为true,则整个三目运算的结果为表达式y
- 如下所示://view重绘时回调view.getViewTreeObserver().addOnDrawListener(new OnDra
- 我们知道,Maven 是通过仓库对依赖进行管理的,当 Maven 项目需要某个依赖时,只要其 POM 中声明了依赖的坐标信息,Maven 就
- 你知道String、StringBuilder、Stringbuffer的区别吗?当你创建字符串的时候,有考虑过该使用哪个吗?别急,这篇文章
- 本文实例为大家分享了java图形用户界面实现菜单功能的具体代码,供大家参考,具体内容如下题目:编写一个图形用户界面,实现菜单的功能。有3个一
- 前言之前我们提到了 CustomPaint er 的 Paint 可以使用渐变(GradientShader)来填充绘制的图形,本篇我们来介
- Java读取json数据并存入数据库1. pom依赖<dependency> &nbs