Spring boot 第六章 SpringApplication源碼分析

一、概述

Spring boot 在進行SpringApplication對象實例化會加載 META/spring.factories文件,將該配置文件中的配置載入到Spring容器。web

META/spring.factories 文件位置spring

輸入圖片說明

二、SpringApplication 源碼分析

輸入圖片說明

輸入圖片說明

/**
	 * The location to look for factories.
	 * <p>Can be present in multiple JAR files.
	 */
	public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";


/**
	 * Load the fully qualified class names of factory implementations of the
	 * given type from {@value #FACTORIES_RESOURCE_LOCATION}, using the given
	 * class loader.
	 * @param factoryClass the interface or abstract class representing the factory
	 * @param classLoader the ClassLoader to use for loading resources; can be
	 * {@code null} to use the default
	 * @see #loadFactories
	 * @throws IllegalArgumentException if an error occurs while loading factory names
	 */
	public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
		String factoryClassName = factoryClass.getName();
		return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
	}

	private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
		MultiValueMap<String, String> result = cache.get(classLoader);
		if (result != null)
			return result;
		try {
			Enumeration<URL> urls = (classLoader != null ?
					classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
					ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
			result = new LinkedMultiValueMap<>();
			while (urls.hasMoreElements()) {
				URL url = urls.nextElement();
				UrlResource resource = new UrlResource(url);
				Properties properties = PropertiesLoaderUtils.loadProperties(resource);
				for (Map.Entry<?, ?> entry : properties.entrySet()) {
					List<String> factoryClassNames = Arrays.asList(
							StringUtils.commaDelimitedListToStringArray((String) entry.getValue()));
					result.addAll((String) entry.getKey(), factoryClassNames);
				}
			}
			cache.put(classLoader, result);
			return result;
		}
		catch (IOException ex) {
			throw new IllegalArgumentException("Unable to load factories from location [" +
					FACTORIES_RESOURCE_LOCATION + "]", ex);
		}
	}

三、條件註解

@ConditionalOnBean : 當容器裏有指定的bean的條件下jvm

@ConditionalOnClass :當類路徑下有指定的類的條件下源碼分析

@ConditionalOnExpression : 基於SpEL表達式做爲判斷條件url

@ConditionalOnJava : 基於jvm版本做爲判斷條件code

@ConditionalOnJndi : 在 JNDI存在的條件下查找指定的位置對象

@ConditionalOnMissingBean : 當容器裏沒有指定的bean的狀況下圖片

@ConditionalOnMissingClass : 當類路徑下沒有指定的類的條件下ip

@ConditionalOnNotWebApplication : 當前項目不是web項目的條件下get

@ConditionalOnProperty : 指定的屬性是否有指定的值

@ConditionalOnResource : 類路徑是否有指定的值

@ConditionalOnSingleCandidate : 當指定bean 在容器中只有一個,或者雖然有多個可是指定首選的bean

@ConditionalOnWebApplication : 當前項目時web項目的條件下

相關文章
相關標籤/搜索