一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

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

Spring Boot的properties配置文件讀取

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

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

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

具體啟動過程以前的博客寫過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;
}

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

Spring Boot的properties配置文件讀取

都是監聽器相關的部分,略了,springapplicationrunlisteners類中:

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

eventpublishingrunlistener:

Spring Boot的properties配置文件讀取

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監聽器執行:

Spring Boot的properties配置文件讀取

 Spring Boot的properties配置文件讀取

 現在這個postprocessors中包含json之類其他的監聽器,不過我現在只想扣出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基本是沒用的,這東西現在已經很少用到了,這個環境當然是沒配的:

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配置文件讀取

 我只關心application.properties,它是上面循環中的一次,走進了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);
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 北条麻妃黑人正在播放 | 香蕉97超级碰碰碰免费公 | 小草高清视频免费直播 | 免费观看www视频 | chinese野外gay军人 | 亚洲社区在线 | 99国产牛牛视频在线网站 | 午夜伦午夜伦锂电影 | 色综合久久天天综合 | 国产欧美一区二区三区久久 | 狠狠婷婷综合缴情亚洲 | tube69xxxxhd日本| 欧美日韩一区二区中文字幕视频 | 亚洲国产精品久久久久久 | 97理伦 | 日本爽p大片免费观看 | 高h扶她文肉 | 国产精品久久久久jk制服 | 蜜桃在线 | 日本伦理动漫在线观看 | 亚洲男男video | 91香蕉国产在线观看人员 | 91色资源网在线观看 | 亚洲日日做天天做日日谢 | 欧美午夜精品久久久久久黑人 | 成人另类视频 | 999久久久 | 成人免费公开视频 | spank日本网站脱裤子打屁股 | 久久足恋网 | 亚洲品质自拍视频 | 日本久久啪啪婷婷激情五月 | 哇嘎在线精品视频在线观看 | 猥琐对着美女飞机喷到脸上 | 日本在线观看免费观看完整版 | 欧美一级高清片免费一级 | 精品综合久久久久久88小说 | 桃色公寓 | 国产激情一区二区三区成人91 | 成人久久18网站 | 亚洲精品一区二区久久久久 |