欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot?spring.factories加載時(shí)機(jī)分析

 更新時(shí)間:2023年03月13日 15:30:06   作者:石臻臻的雜貨鋪  
這篇文章主要為大家介紹了SpringBoot?spring.factories加載時(shí)機(jī)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

spring.factories作用

這個(gè)類似于Java中的SPI功能,SpringBoot啟動(dòng)的時(shí)候會(huì)讀取所有jar包下面的META-INF/spring.factories文件;

并且將文件中的 接口/抽象類 對(duì)應(yīng)的實(shí)現(xiàn)類都對(duì)應(yīng)起來,并在需要的時(shí)候可以實(shí)例化對(duì)應(yīng)的實(shí)現(xiàn)類

下面我們來分析一下源碼看看spring.factories的使用場(chǎng)景

源碼解析

啟動(dòng)SpringApplication,看看構(gòu)造方法

	public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
		this.webApplicationType = WebApplicationType.deduceFromClasspath();
		setInitializers((Collection) getSpringFactoriesInstances(
				ApplicationContextInitializer.class));
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		this.mainApplicationClass = deduceMainApplicationClass();
	}

其中方法getSpringFactoriesInstances( ApplicationContextInitializer.class) 是用于獲取Spring中指定類實(shí)例用的;并且獲取的時(shí)候是根據(jù)讀取整個(gè)項(xiàng)目中文件路徑為META-INF/spring.factories 中的內(nèi)容實(shí)例化對(duì)應(yīng)的實(shí)例類的;

例如這里的ApplicationContextInitializer 是一個(gè)接口,那么應(yīng)該實(shí)例化哪些他的實(shí)現(xiàn)類呢?那就找META-INF/spring.factories文件 ; 那么我們?cè)?code>spring-boot:2.1.0jar包中找到了這個(gè)文件

讀取到需要實(shí)例化的實(shí)現(xiàn)類為

org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer

并且還在spring-boot-autoconfigure-2.1.0.RELEASE.jar中找到了這個(gè)文件

那么文件中的兩個(gè)實(shí)現(xiàn)類也會(huì)被實(shí)例化;加上上面4個(gè)總共有6個(gè)

org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener

可以看到不僅僅只是把org.springframework.context.ApplicationContextInitializer 的實(shí)例類解析了出來;而是所有的都解析了出來并且保存下來了.下次其他的類需要被實(shí)例化的時(shí)候就可以直接從內(nèi)存里面拿了;

上面過程拿到了實(shí)例類之后,接下來就是實(shí)例化的過程了

	private <T> Collection<T> getSpringFactoriesInstances(Class<T> type,
			Class<?>[] parameterTypes, Object... args) {
		ClassLoader classLoader = getClassLoader();
		// Use names and ensure unique to protect against duplicates
		Set<String> names = new LinkedHashSet<>(
				SpringFactoriesLoader.loadFactoryNames(type, classLoader));
		List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
				classLoader, args, names);
		AnnotationAwareOrderComparator.sort(instances);
		return instances;
	}

方法createSpringFactoriesInstances就是創(chuàng)建實(shí)例的過程;可以看到傳入了對(duì)應(yīng)的接口類org.springframework.context.ApplicationContextInitializer ;接下來就會(huì)實(shí)例化 上面找到了對(duì)應(yīng)的實(shí)現(xiàn)類;

	private <T> List<T> createSpringFactoriesInstances(Class<T> type,
			Class<?>[] parameterTypes, ClassLoader classLoader, Object[] args,
			Set<String> names) {
		List<T> instances = new ArrayList<>(names.size());
		for (String name : names) {
			try {
				Class<?> instanceClass = ClassUtils.forName(name, classLoader);
				Assert.isAssignable(type, instanceClass);
				Constructor<?> constructor = instanceClass
						.getDeclaredConstructor(parameterTypes);
				T instance = (T) BeanUtils.instantiateClass(constructor, args);
				instances.add(instance);
			}
			catch (Throwable ex) {
				throw new IllegalArgumentException(
						"Cannot instantiate " + type + " : " + name, ex);
			}
		}
		return instances;
	}

實(shí)例化的過程如果,沒有什么特別需要講解的;

上面有個(gè)方法 AnnotationAwareOrderComparator.sort(instances);是用來排序所有實(shí)例的; 實(shí)現(xiàn)類需要實(shí)現(xiàn) 接口Ordered ; getOrder返回的值越小,優(yōu)先級(jí)更高

用法

知道spring.factories的用法之后, 那么我們就可以利用這個(gè)特性實(shí)現(xiàn)自己的目的;

例如我們也可以寫一個(gè)接口類ApplicationContextInitializer的實(shí)現(xiàn)類;

等等之類的;

以上就是SpringBoot spring.factories加載時(shí)機(jī)分析的詳細(xì)內(nèi)容,更多關(guān)于spring.factories加載時(shí)機(jī)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論