亲宝软件园·资讯

展开

springboot多环境配置文件及自定义配置文件路径详解

『梧桐雨』 人气:0

一:什么是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;
      。。。。。。。。。(略)

加载全部内容

相关教程
猜你喜欢
用户评论