properties配置文件如何使用Spring Boot进行读取

发布时间:2020-11-21 16:48:33 作者:Leah
来源:亿速云 阅读:227

这篇文章给大家介绍properties配置文件如何使用Spring Boot进行读取,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

在SpringApplication类中:

  private ConfigurableEnvironment prepareEnvironment(
      SpringApplicationRunListeners listeners,
      ApplicationArguments applicationArguments) {
    // Create and configure the environment
    ConfigurableEnvironment environment = getOrCreateEnvironment();
    configureEnvironment(environment, applicationArguments.getSourceArgs());

    //此处读取
    listeners.environmentPrepared(environment);

    if (isWebEnvironment(environment)
        && this.webApplicationType == WebApplicationType.NONE) {
      environment = convertToStandardEnvironment(environment);
    }
    return environment;
  }

关于监听器的过程在开头说的那篇的一系列中也说的挺细的,这里不介绍了:

properties配置文件如何使用Spring Boot进行读取

都是监听器相关的部分,略了,SpringApplicationRunListeners类中:

  public void environmentPrepared(ConfigurableEnvironment environment) {
    for (SpringApplicationRunListener listener : this.listeners) {
      listener.environmentPrepared(environment);
    }
  }

EventPublishingRunListener:

properties配置文件如何使用Spring Boot进行读取

onApplicationEnvironmentPreparedEvent事件触发org\springframework\boot\spring-boot\2.0.0.BUILD-SNAPSHOT\spring-boot-2.0.0.BUILD-20170421.122111-547-sources.jar!\org\springframework\boot\context\config\ConfigFileApplicationListener.java监听器执行:

properties配置文件如何使用Spring Boot进行读取

 properties配置文件如何使用Spring Boot进行读取

 现在这个postProcessors中包含Json之类其他的监听器,不过我现在只想扣出properties的代码,别的先略过,反正其实也没什么,本来也是想看看它的思路,扣着玩,不要太在意。

properties配置文件如何使用Spring Boot进行读取

  protected void addPropertySources(ConfigurableEnvironment environment,
      ResourceLoader resourceLoader) {
    RandomValuePropertySource.addToEnvironment(environment);
    new Loader(environment, resourceLoader).load();
  }

properties配置文件如何使用Spring Boot进行读取

    Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
      this.environment = environment;
      this.resourceLoader = resourceLoader == null ? new DefaultResourceLoader()
          : resourceLoader;
    }
    this.classLoader = ClassUtils.getDefaultClassLoader();
    //其实也就是Thread.currentThread().getContextClassLoader();

下面就是真正加载了配置文件的load方法了,先是初始化PropertySourcesLoader和一些临时的集合:

      this.propertiesLoader = new PropertySourcesLoader();
      this.activatedProfiles = false;
      this.profiles = Collections.asLifoQueue(new LinkedList<Profile>());
      this.processedProfiles = new LinkedList<>();

      // Pre-existing active profiles set via Environment.setActiveProfiles()
      // are additional profiles and config files are allowed to add more if
      // they want to, so don't call addActiveProfiles() here.
      Set<Profile> initialActiveProfiles = initializeActiveProfiles();
      this.profiles.addAll(getUnprocessedActiveProfiles(initialActiveProfiles));

这些集合其实如果没配置Profile基本是没用的,这东西现在已经很少用到了,这个环境当然是没配的:

properties配置文件如何使用Spring Boot进行读取

主要是下面这部分:

        for (String location : getSearchLocations()) {
          if (!location.endsWith("/")) {
            // location is a filename already, so don't search for more
            // filenames
            load(location, null, profile);
          }
          else {
            for (String name : getSearchNames()) {
              load(location, name, profile);
            }
          }
        }

就是去指定目录下去找各种以application为名字的指定类型的配置文件:

properties配置文件如何使用Spring Boot进行读取

properties配置文件如何使用Spring Boot进行读取

 我只关心application.properties,它是上面循环中的一次,走进了doLoadIntoGroup方法的下面那句:

properties配置文件如何使用Spring Boot进行读取

properties配置文件如何使用Spring Boot进行读取

properties配置文件如何使用Spring Boot进行读取

  private Map<String, &#63;> loadProperties(Resource resource) throws IOException {
    String filename = resource.getFilename();
    if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
      return (Map) PropertiesLoaderUtils.loadProperties(resource);
    }
    return new OriginTrackedPropertiesLoader(resource).load();
  }

这个resource其实只是封装了一下InputStream,具体的读取。。。反正也没啥特别的读法:

properties配置文件如何使用Spring Boot进行读取

读出的key和value放在Map<String, OriginTrackedValue>: 

  private void put(Map<String, OriginTrackedValue> result, String key,
      OriginTrackedValue value) {
    if (!key.isEmpty()) {
      result.put(key, value);
    }
  }

关于properties配置文件如何使用Spring Boot进行读取就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. spring boot 配置文件properties和YAML详解
  2. 使用Python怎么对properties配置文件进行读取

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot properties

上一篇:Android开发中怎么使用Dialog显示与隐藏软键盘

下一篇:深入浅析Kotlin中的异常

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》