国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring Boot的properties配置文件讀取

Spring Boot的properties配置文件讀取

2020-11-08 17:41draculav Java教程

這篇文章主要介紹了Spring Boot的properties配置文件讀取,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

我在自己寫點東西玩的時候需要讀配置文件,又不想引包,于是打算扣點spring boot讀取配置文件的代碼出來,當(dāng)然只是讀配置文件沒必要這么麻煩,不過反正閑著也是閑著,扣著玩了。

具體啟動過程以前的博客寫過spring boot啟動過程(一),這次入口在springapplication類中:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
}

關(guān)于監(jiān)聽器的過程在開頭說的那篇的一系列中也說的挺細的,這里不介紹了:

Spring Boot的properties配置文件讀取

都是監(jiān)聽器相關(guān)的部分,略了,springapplicationrunlisteners類中:

?
1
2
3
4
5
public void environmentprepared(configurableenvironment environment) {
  for (springapplicationrunlistener listener : this.listeners) {
    listener.environmentprepared(environment);
  }
}

eventpublishingrunlistener:

Spring Boot的properties配置文件讀取

onapplicationenvironmentpreparedevent事件觸發(fā)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監(jiān)聽器執(zhí)行:

Spring Boot的properties配置文件讀取

 Spring Boot的properties配置文件讀取

 現(xiàn)在這個postprocessors中包含json之類其他的監(jiān)聽器,不過我現(xiàn)在只想扣出properties的代碼,別的先略過,反正其實也沒什么,本來也是想看看它的思路,扣著玩,不要太在意。

Spring Boot的properties配置文件讀取

?
1
2
3
4
5
protected void addpropertysources(configurableenvironment environment,
    resourceloader resourceloader) {
  randomvaluepropertysource.addtoenvironment(environment);
  new loader(environment, resourceloader).load();
}

Spring Boot的properties配置文件讀取

?
1
2
3
4
5
loader(configurableenvironment environment, resourceloader resourceloader) {
  this.environment = environment;
  this.resourceloader = resourceloader == null ? new defaultresourceloader()
      : resourceloader;
}
?
1
2
this.classloader = classutils.getdefaultclassloader();
//其實也就是thread.currentthread().getcontextclassloader();

下面就是真正加載了配置文件的load方法了,先是初始化propertysourcesloader和一些臨時的集合:

?
1
2
3
4
5
6
7
8
9
10
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基本是沒用的,這東西現(xiàn)在已經(jīng)很少用到了,這個環(huán)境當(dāng)然是沒配的:

Spring Boot的properties配置文件讀取

主要是下面這部分:

?
1
2
3
4
5
6
7
8
9
10
11
12
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為名字的指定類型的配置文件:

Spring Boot的properties配置文件讀取

Spring Boot的properties配置文件讀取

 我只關(guān)心application.properties,它是上面循環(huán)中的一次,走進了doloadintogroup方法的下面那句:

Spring Boot的properties配置文件讀取

Spring Boot的properties配置文件讀取

Spring Boot的properties配置文件讀取

?
1
2
3
4
5
6
7
private map<string, ?> 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,具體的讀取。。。反正也沒啥特別的讀法:

Spring Boot的properties配置文件讀取

讀出的key和value放在map<string, origintrackedvalue>: 

?
1
2
3
4
5
6
private void put(map<string, origintrackedvalue> result, string key,
    origintrackedvalue value) {
  if (!key.isempty()) {
    result.put(key, value);
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家

原文鏈接:http://www.cnblogs.com/saaav/p/6937602.html?utm_source=tuicool&utm_medium=referral

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲资源在线 | 激情一区二区 | 国产一区二区在线视频 | 久久奸| 精品无码久久久久久久动漫 | 精品国偷自产国产一区 | 久久视频免费 | 综合久久精品 | 国产偷亚洲偷欧美偷精品 | 超碰成人在线免费 | 蜜桃免费一区二区三区 | 成人在线小视频 | 韩国精品一区二区三区 | 国产精品久久久久免费a∨ 欧美黄色精品 | 日韩欧美不卡 | 亚洲成人一区二区三区 | 精品久久久久国产 | 精品视频国产 | 国产精品第一国产精品 | 国内久久精品 | 成人网久久| 国产老头老太作爱视频 | 日韩欧美的一区二区 | 色8久久| 激情五月婷婷av | 国产一区自拍视频 | 在线观看日韩精品 | 性视频网站免费 | 久久成人18免费网站 | 久久久综合网 | 精品久久网 | 91新视频| 久久精品在线 | 亚洲成a人 | 懂色一区二区三区av片 | 国产成人精品久久二区二区 | 午夜精品在线 | 国内精品久久久久久久影视简单 | 日韩免费视频 | 伊人久久综合 | 婷婷丁香激情网 |