首页 > 编程知识 正文

springboot指定多个配置文件,springboot多配置文件选择

时间:2023-05-06 19:30:27 阅读:229253 作者:116

一:什么是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 ConfigFileApplicationListenerimplements 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}. */@Deprecatedpublic 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; 。。。。。。。。。(略)

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。