Python 官方文档:入门教程 => 点击学习
目录一:什么是classpath?二、自定义SpringBoot配置文件路径一:什么是classpath? classpath指的就是 *.java文件,资源文件等编译后存放的位置,
classpath指的就是 *.java文件,资源文件等编译后存放的位置,对于Maven项目就是指 target/classes:这个路径,只要编译后的文件在这个目录下,springboot就可以找到,这里指的是maven项目,javaweb项目可能会有区别。
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);
public static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active";
public static final String INCLUDE_PROFILES_PROPERTY = "spring.profiles.include";
public static final String CONFIG_NAME_PROPERTY = "spring.config.name";
public static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";
public static final String CONFIG_ADDITIONAL_LOCATION_PROPERTY = "spring.config.additional-location";
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
@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;
。。。。。。。。。(略)
到此这篇关于springboot多环境配置文件及自定义配置文件路径的文章就介绍到这了,更多相关springboot多环境配置文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: springboot多环境配置文件及自定义配置文件路径详解
本文链接: https://www.lsjlt.com/news/195104.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0