最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?Boot常用功能Profile詳解

 更新時間:2022年07月15日 09:27:57   作者:lhf2112  
SpringBootProfile是一個很常用的功能,我們可以通過為開發(fā)/測試/生產(chǎn)環(huán)境配置不同的profile來實現(xiàn)配置隔離,那么在SpringBoot項目中是如何實現(xiàn)profile功能的呢

入口

相關(guān)邏輯的入口是listener類:ConfigFileApplicationListener,當(dāng)容器廣播器觸發(fā)ApplicationEnvironmentPreparedEvent事件時,ConfigFileApplicationListener會收到廣播器的通知,進(jìn)而執(zhí)行onApplicationEnvironmentPreparedEvent方法

入口處代碼:

	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof ApplicationEnvironmentPreparedEvent) {
			onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event);
		}
		if (event instanceof ApplicationPreparedEvent) {
			onApplicationPreparedEvent(event);
		}
	}

接下來onApplicationEnvironmentPreparedEvent方法會加載容器中的EnvironmentPostProcessor并進(jìn)行遍歷,調(diào)用他們的postProcessEnvironment方法,我們可以看一下此時的PostProcessor有哪些:

我們發(fā)現(xiàn)ConfigFileApplicationListener本身也是其中一個PostProcessor :)

我們直接進(jìn)入ConfigFileApplicationListener的postProcessEnvironment方法,它會調(diào)用一個addPropertySources方法

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

隨機(jī)屬性值部分暫且不表,我們看下loader.load方法

		public void load() {
			this.profiles = new LinkedList<>();
			this.processedProfiles = new LinkedList<>();
			this.activatedProfiles = false;
			this.loaded = new LinkedHashMap<>();
			initializeProfiles();
			while (!this.profiles.isEmpty()) {
				Profile profile = this.profiles.poll();
				if (profile != null && !profile.isDefaultProfile()) {
					addProfileToEnvironment(profile.getName());
				}
				load(profile, this::getPositiveProfileFilter, addToLoaded(MutablePropertySources::addLast, false));
				this.processedProfiles.add(profile);
			}
			resetEnvironmentProfiles(this.processedProfiles);
			load(null, this::getNegativeProfileFilter, addToLoaded(MutablePropertySources::addFirst, true));
			addLoadedPropertySources();
		}

load方法的重點邏輯-initializeProfiles()

private void initializeProfiles() {
	// The default profile for these purposes is represented as null. We add it
	// first so that it is processed first and has lowest priority.
	this.profiles.add(null);
	Set<Profile> activatedViaProperty = getProfilesActivatedViaProperty();
	this.profiles.addAll(getOtherActiveProfiles(activatedViaProperty));
	// Any pre-existing active profiles set via property sources (e.g.
	// System properties) take precedence over those added in config files.
	addActiveProfiles(activatedViaProperty);
	if (this.profiles.size() == 1) { // only has null profile
		for (String defaultProfileName : this.environment.getDefaultProfiles()) {
			Profile defaultProfile = new Profile(defaultProfileName, true);
			this.profiles.add(defaultProfile);
		}
	}
}

add(null) 可以處理application.properties/yml,接下來getProfilesActivatedViaProperty方法會從spring.profiles.active和spring.profiles.include配置中讀取激活的profile~(這兩者也是相對常用的配置);再接下來會判斷profiles大小是否為1,是的話會添加一個default profile,如圖所示:

這也解釋了為什么spring.profiles.default必須定義在其他屬性源(命令行啟動參數(shù)),因為這時候分散文件屬性元還沒有被解析到!

回到load方法,如果profiles不為空,就會進(jìn)行while遍歷,對其調(diào)用另外一個load方法:

(load-number2方法):

		private void load(Profile profile, DocumentFilterFactory filterFactory, DocumentConsumer consumer) {
			getSearchLocations().forEach((location) -> {
				boolean isFolder = location.endsWith("/");
				Set<String> names = isFolder ? getSearchNames() : NO_SEARCH_NAMES;
				names.forEach((name) -> load(location, name, profile, filterFactory, consumer));
			});
		}

我們可以看到還會調(diào)用到另一個load方法

(load-number3方法):

private void load(String location, String name, Profile profile, DocumentFilterFactory filterFactory,
		DocumentConsumer consumer) {
	if (!StringUtils.hasText(name)) {
		for (PropertySourceLoader loader : this.propertySourceLoaders) {
			if (canLoadFileExtension(loader, location)) {
				load(loader, location, profile, filterFactory.getDocumentFilter(profile), consumer);
				return;
			}
		}
	}
	Set<String> processed = new HashSet<>();
	for (PropertySourceLoader loader : this.propertySourceLoaders) {
		for (String fileExtension : loader.getFileExtensions()) {
			if (processed.add(fileExtension)) {
				loadForFileExtension(loader, location + name, "." + fileExtension, profile, filterFactory,
						consumer);
			}
		}
	}
}

這里的propertySourceLoaders包括:

是不是很熟悉?這就是properties和yaml分別對應(yīng)的PropertySourceLoader~

下面代碼會調(diào)用loadForFileExtension方法,而這個方法又會調(diào)用新的load方法(??):

(load-number4方法):

private void load(PropertySourceLoader loader, String location, Profile profile, DocumentFilter filter,
		DocumentConsumer consumer) {
	try {
		Resource resource = this.resourceLoader.getResource(location);
		if (resource == null || !resource.exists()) {
			if (this.logger.isTraceEnabled()) {
				StringBuilder description = getDescription("Skipped missing config ", location, resource,
						profile);
				this.logger.trace(description);
			}
			return;
		}
		if (!StringUtils.hasText(StringUtils.getFilenameExtension(resource.getFilename()))) {
			if (this.logger.isTraceEnabled()) {
				StringBuilder description = getDescription("Skipped empty config extension ", location,
						resource, profile);
				this.logger.trace(description);
			}
			return;
		}
		String name = "applicationConfig: [" + location + "]";
		List<Document> documents = loadDocuments(loader, name, resource);
		if (CollectionUtils.isEmpty(documents)) {
			if (this.logger.isTraceEnabled()) {
				StringBuilder description = getDescription("Skipped unloaded config ", location, resource,
						profile);
				this.logger.trace(description);
			}
			return;
		}
		List<Document> loaded = new ArrayList<>();
		for (Document document : documents) {
			if (filter.match(document)) {
				addActiveProfiles(document.getActiveProfiles());
				addIncludedProfiles(document.getIncludeProfiles());
				loaded.add(document);
			}
		}
		Collections.reverse(loaded);
		if (!loaded.isEmpty()) {
			loaded.forEach((document) -> consumer.accept(profile, document));
			if (this.logger.isDebugEnabled()) {
				StringBuilder description = getDescription("Loaded config file ", location, resource, profile);
				this.logger.debug(description);
			}
		}
	}
	catch (Exception ex) {
		throw new IllegalStateException("Failed to load property " + "source from location '" + location + "'",
				ex);
	}
}

這個方法會讀取application-profile.properties/yaml文件,并加載文件內(nèi)激活的profile(如果有的話)

到這里已經(jīng)讀取到了所有的配置信息,主邏輯大概結(jié)束了,還差最后一個步驟:addLoadedPropertySources:

		private void addLoadedPropertySources() {
			MutablePropertySources destination = this.environment.getPropertySources();
			List<MutablePropertySources> loaded = new ArrayList<>(this.loaded.values());
			Collections.reverse(loaded);
			String lastAdded = null;
			Set<String> added = new HashSet<>();
			for (MutablePropertySources sources : loaded) {
				for (PropertySource<?> source : sources) {
					if (added.add(source.getName())) {
						addLoadedPropertySource(destination, lastAdded, source);
						lastAdded = source.getName();
					}
				}
			}
		}

這里會將所有屬性加入到“destination”,即environment.getPropertySources(),到這里,加載配置屬性的邏輯就完成了。

到此這篇關(guān)于Spring Boot常用功能Profile詳解的文章就介紹到這了,更多相關(guān)Spring Boot Profile內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 實例分析java開啟線程的方法

    實例分析java開啟線程的方法

    在本文里我們通過實例給大家講解了JAVA開啟線程的方法和相關(guān)知識點,需要的朋友們跟著學(xué)習(xí)下。
    2019-03-03
  • java中ResultSet遍歷數(shù)據(jù)操作

    java中ResultSet遍歷數(shù)據(jù)操作

    這篇文章主要介紹了java中ResultSet遍歷數(shù)據(jù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 詳解5種Java中常見限流算法

    詳解5種Java中常見限流算法

    在高并發(fā)系統(tǒng)中,出于系統(tǒng)保護(hù)角度考慮,通常會對流量進(jìn)行限流;不但在工作中要頻繁使用,而且也是面試中的高頻考點。本文就為大家整理了5種Java中常見限流算法,需要的可以參考一下
    2023-04-04
  • Spring多線程事務(wù)處理解決方案

    Spring多線程事務(wù)處理解決方案

    這篇文章主要介紹了Spring多線程事務(wù)處理,本文主要介紹了spring多線程事務(wù)的解決方案,心急的小伙伴可以跳過上面的理論介紹分析部分直接看最終解決方案,需要的朋友可以參考下
    2024-03-03
  • maven工程中讀取resources中的資源文件

    maven工程中讀取resources中的資源文件

    Web項目中應(yīng)該經(jīng)常有這樣的需求,在maven項目的resources目錄下放一些文件,比如一些配置文件,資源文件等,本文主要介紹了maven工程中讀取resources中的資源文件,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • Java解析變量公式的簡單示例

    Java解析變量公式的簡單示例

    在Java編程中,經(jīng)常會遇到需要解析表達(dá)式或公式的情況,特別是涉及到動態(tài)計算或配置項的場景,在本篇文章中,我將介紹如何在Java中解析變量公式,并給出一個簡單的實現(xiàn)示例,需要的朋友可以參考下
    2024-10-10
  • idea2020.3測試評價及感受

    idea2020.3測試評價及感受

    idea2020.3版本這次變化最大的也就是 UI了完全拋棄了之前一直使用的模板更改成了新的樣式,感興趣的朋友快來下載體驗下吧
    2020-10-10
  • 淺談Thread.sleep(0)到底有什么用

    淺談Thread.sleep(0)到底有什么用

    為什么要用sleep,主要是為了暫停當(dāng)前線程,把cpu片段讓出給其他線程,減緩當(dāng)前線程的執(zhí)行,本文主要介紹了Thread.sleep(0)到底有什么用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 解決IDEA創(chuàng)建maven項目時pom.xml沒有變藍(lán)的問題

    解決IDEA創(chuàng)建maven項目時pom.xml沒有變藍(lán)的問題

    這篇文章主要介紹了解決IDEA創(chuàng)建maven項目時pom.xml沒有變藍(lán)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Java日期時間操作的方法

    Java日期時間操作的方法

    這篇文章主要為大家詳細(xì)介紹了Java日期時間操作的一些方法,獲得Calendar,定義日期/時間的格式等,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評論

花莲县| 镇沅| 农安县| 抚宁县| 磐安县| 达日县| 三都| 东城区| 志丹县| 凤城市| 微山县| 莆田市| 邹城市| 巫溪县| 稻城县| 遵义市| 陆河县| 宁化县| 景宁| 确山县| 涿鹿县| 江孜县| 三台县| 偏关县| 吴川市| 隆尧县| 昌邑市| 长顺县| 东兴市| 兴海县| 沙河市| 常州市| 淄博市| 墨脱县| 三门峡市| 辽阳市| 乐平市| 万山特区| 庆安县| 同江市| 抚州市|