亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring中的@PropertySource注解源碼詳解

 更新時(shí)間:2023年11月14日 10:35:05   作者:drunk in spring  
這篇文章主要介紹了Spring中的@PropertySource注解源碼詳解,@PropertySource注解用于指定資源文件讀取的位置,它不僅能讀取properties文件,也能讀取xml文件,并且通過(guò)yaml解析器,配合自定義PropertySourceFactory實(shí)現(xiàn)解析yaml文件,需要的朋友可以參考下

@PropertySource注解使用

@PropertySource注解用于指定資源文件讀取的位置,它不僅能讀取properties文件,也能讀取xml文件,并且通過(guò)yaml解析器,配合自定義PropertySourceFactory實(shí)現(xiàn)解析yaml文件

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {
	//資源名稱(chēng),為空則根據(jù)資源的描述符生成
	String name() default "";
	/**
	 *資源路徑
	 *classpath:application.properties
	 *file:/
	 */
	String[] value();
	//是否忽略資源不存在的情況,如果不忽略,當(dāng)資源不存在時(shí)報(bào)錯(cuò)
	boolean ignoreResourceNotFound() default false;
	//指定資源文件的編碼格式
	String encoding() default "";
	//資源工廠
	Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}

源碼解析

在bean實(shí)例化之前有一個(gè)ConfigurationClassPostProcessor類(lèi),會(huì)操作BeanDefinition并且加入到BeanDefinitionRegistry中,它的優(yōu)先級(jí)在所有的BeanDefinitionRegistryPostProcessor里面是最低的。

優(yōu)先級(jí):最低

public int getOrder() {
	return Ordered.LOWEST_PRECEDENCE;  // within PriorityOrdered
}

會(huì)在這個(gè)類(lèi)中進(jìn)行相關(guān)注解的解析操作,在進(jìn)行@PropertySource注解解析的時(shí)候要借助ConfigurationClassParser的parse方法

在進(jìn)行@PropertySource注解解析之前,需要拿到兩個(gè)對(duì)象ConfigurationClass和SourceClass

bean的元數(shù)據(jù)信息(bd.getMetadata),和beanName,封裝成ConfigurationClass對(duì)象

new ConfigurationClass(metadata, beanName)

SourceClass這個(gè)對(duì)象理解為跟類(lèi)或者接口對(duì)應(yīng),然后把metadata對(duì)象包裝進(jìn)去

SourceClass sourceClass = asSourceClass(configClass, filter);

接下來(lái)正式進(jìn)入解析流程

for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
		sourceClass.getMetadata(), PropertySources.class,
		org.springframework.context.annotation.PropertySource.class)) {
	if (this.environment instanceof ConfigurableEnvironment) {
		//核心邏輯
		processPropertySource(propertySource);
	}
	else {
		logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
				"]. Reason: Environment must implement ConfigurableEnvironment");
	}
}

拿到類(lèi)上面的所有PropertySource注解的值A(chǔ)nnotationAttributes,放到processPropertySource方法中進(jìn)行解析

private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
	String name = propertySource.getString("name");
	if (!StringUtils.hasLength(name)) {
		name = null;
	}
	String encoding = propertySource.getString("encoding");
	if (!StringUtils.hasLength(encoding)) {
		encoding = null;
	}
	//獲取配置文件路徑
	String[] locations = propertySource.getStringArray("value");
	Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
	boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");

	Class<? extends PropertySourceFactory> factoryClass = propertySource.getClass("factory");
	PropertySourceFactory factory = (factoryClass == PropertySourceFactory.class ?
			DEFAULT_PROPERTY_SOURCE_FACTORY : BeanUtils.instantiateClass(factoryClass));

	for (String location : locations) {
		try {
			//替換占位符
			String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
			//流的方式加載配置文件并封裝成Resource對(duì)象
			Resource resource = this.resourceLoader.getResource(resolvedLocation);
			//加載Resource中的配置屬性封裝成Properties對(duì)象中,并創(chuàng)建PropertySource對(duì)象加入到Environment對(duì)象中
			addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
		}
		catch (IllegalArgumentException | FileNotFoundException | UnknownHostException ex) {
			// Placeholders not resolvable or resource not found when trying to open it
			if (ignoreResourceNotFound) {
				if (logger.isInfoEnabled()) {
					logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
				}
			}
			else {
				throw ex;
			}
		}
	}
}

這里看一下createPropertySource方法,就是將配置文件中的內(nèi)容封裝成ResourcePropertySource對(duì)象,作為屬性源

public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
	return (name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource));
}

屬性源拿到之后,要統(tǒng)一交給Environment管理,調(diào)用addPropertySource方法

private void addPropertySource(PropertySource<?> propertySource) {
	String name = propertySource.getName();
	//獲取Environment對(duì)象中的MutablePropertySources
	MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();

	//如果已經(jīng)存在了該配置文件的PropertySource則合并久的
	if (this.propertySourceNames.contains(name)) {
		// We've already added a version, we need to extend it
		PropertySource<?> existing = propertySources.get(name);
		if (existing != null) {
			PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ?
					((ResourcePropertySource) propertySource).withResourceName() : propertySource);
			//合并二次后的類(lèi)型
			if (existing instanceof CompositePropertySource) {
				((CompositePropertySource) existing).addFirstPropertySource(newSource);
			}
			else {
				if (existing instanceof ResourcePropertySource) {
					existing = ((ResourcePropertySource) existing).withResourceName();
				}
				//其實(shí)就是CompositePropertySource里面有一個(gè)Set,Set里面裝了新和舊的PropertySource對(duì)象
				CompositePropertySource composite = new CompositePropertySource(name);
				composite.addPropertySource(newSource);
				composite.addPropertySource(existing);
				propertySources.replace(name, composite);
			}
			return;
		}
	}

	if (this.propertySourceNames.isEmpty()) {
		propertySources.addLast(propertySource);
	}
	else {
		//用于計(jì)算插入的位置index
		String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
		//吧propertySource對(duì)象存入MutablePropertySources的list中
		propertySources.addBefore(firstProcessed, propertySource);
	}
	this.propertySourceNames.add(name);
}

拿到Environment中的MutablePropertySources,用來(lái)放置拿到的屬性源。 如果有相同的屬性源,則升級(jí)成CompositePropertySource,把新舊相同的屬性源進(jìn)行合并,再放到MutablePropertySources中

看看CompositePropertySource類(lèi)的內(nèi)部 有Set<PropertySource<?>> propertySources = new LinkedHashSet<>();用來(lái)放置屬性源 也重寫(xiě)了getProperty方法

public Object getProperty(String name) {
	for (PropertySource<?> propertySource : this.propertySources) {
		Object candidate = propertySource.getProperty(name);
		if (candidate != null) {
			return candidate;
		}
	}
	return null;
}

@PropertySource注解中配置的屬性源都交給Environment管理

到此這篇關(guān)于Spring中的@PropertySource注解源碼詳解的文章就介紹到這了,更多相關(guān)@PropertySource注解源碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot整合Xxl-Job的完整步驟記錄

    SpringBoot整合Xxl-Job的完整步驟記錄

    這篇文章主要給大家介紹了關(guān)于SpringBoot整合Xxl-Job的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 教你怎么用Java數(shù)組和鏈表實(shí)現(xiàn)棧

    教你怎么用Java數(shù)組和鏈表實(shí)現(xiàn)棧

    本篇文章為大家詳細(xì)介紹了怎么用Java數(shù)組和鏈表實(shí)現(xiàn)棧,文中有非常詳細(xì)的代碼示例及注釋,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • java如何消除太多的if else判斷示例代碼

    java如何消除太多的if else判斷示例代碼

    這篇文章主要介紹了java如何消除太多的if else判斷,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Java實(shí)現(xiàn)DES加密與解密,md5加密以及Java實(shí)現(xiàn)MD5加密解密類(lèi)

    Java實(shí)現(xiàn)DES加密與解密,md5加密以及Java實(shí)現(xiàn)MD5加密解密類(lèi)

    這篇文章主要介紹了Java實(shí)現(xiàn)DES加密與解密,md5加密以及Java實(shí)現(xiàn)MD5加密解密類(lèi) ,需要的朋友可以參考下
    2015-11-11
  • idea配置springboot熱部署終極解決辦法(解決熱部署失效問(wèn)題)

    idea配置springboot熱部署終極解決辦法(解決熱部署失效問(wèn)題)

    這篇文章主要介紹了idea配置springboot熱部署終極解決辦法(解決熱部署失效問(wèn)題),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-07-07
  • Springboot集成MongoDB無(wú)認(rèn)證與開(kāi)啟認(rèn)證的配置方式

    Springboot集成MongoDB無(wú)認(rèn)證與開(kāi)啟認(rèn)證的配置方式

    本文主要介紹了Springboot集成MongoDB無(wú)認(rèn)證與開(kāi)啟認(rèn)證的配置方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • java 獲取冒號(hào)后面的參數(shù)(正則)實(shí)現(xiàn)代碼

    java 獲取冒號(hào)后面的參數(shù)(正則)實(shí)現(xiàn)代碼

    這篇文章主要介紹了java 獲取冒號(hào)后面的參數(shù)(正則)實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • java集合——Java中的equals和hashCode方法詳解

    java集合——Java中的equals和hashCode方法詳解

    本篇文章詳細(xì)介紹了Java中的equals和hashCode方法詳解,Object 類(lèi)是所有類(lèi)的父類(lèi),非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2016-10-10
  • SpringBoot整合Netty實(shí)現(xiàn)WebSocket的示例代碼

    SpringBoot整合Netty實(shí)現(xiàn)WebSocket的示例代碼

    本文主要介紹了SpringBoot整合Netty實(shí)現(xiàn)WebSocket的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Java中Map.entry的具體使用

    Java中Map.entry的具體使用

    Map.Entry?是Map中的一個(gè)接口,Map.Entry里有相應(yīng)的getKey和getValue方法,讓我們能夠從一個(gè)項(xiàng)中取出Key和Value,本文就詳細(xì)的介紹一下Map.entry的具體使用,感興趣的可以了解一下
    2023-05-05

最新評(píng)論