1、前言java
以前寫過幾篇關於spring boot 的使用相關的文章。咱們用一項技術,不能只停留到表面,要深究其源碼是如何實現,這樣咱們才能在關鍵時刻遊刃有餘。因此從本章開始,將會寫一些關於springboot springcloud的源碼解讀文章,但願對你們有所幫助。web
2、從一個簡單的啓動主類,開始分析spring
//啓動主類兩要素 //1.@springBootApplication 註解。 //2.SpringAppication.run(主類名稱,參數); @SpringBootApplication public class StudyApplication { @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(StudyApplication.class, args); } }
首先從 SpringApplication.run方法,入手。springboot
//使用默認配置運行應用, //primarySource : 啓動的主類 //args:用戶自定義的參數 public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) { return run(new Class<?>[] { primarySource }, args); }
//使用默認的和用戶指定的配置進行加載應用 //primarySource : 啓動的主類 //args:用戶自定義的參數 public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { return new SpringApplication(primarySources).run(args); }
//從指定的主要的來源進行建立SpringApplication實例 public SpringApplication(Class<?>... primarySources) { this(null, primarySources); }
//另外一個構造,與上面構造方法相似 //primarySource : 啓動的主類 //resourceLoader:用到的resourceLoader,此時咱們這個應用 默認是null 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 = deduceWebApplicationType();//web應用類型 setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class));//往applicationContext配置一些配置項 setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));//設置將應用於SpringApplication並註冊到ApplicationContext的ApplicationListener。 this.mainApplicationClass = deduceMainApplicationClass(); }
下面咱們看一下getSpringFactoriesInstances(ApplicationContextInitializer.class) 這個方法app
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) { return getSpringFactoriesInstances(type, new Class<?>[] {}); }
這個方法沒什麼特殊,他內部調用了一下重載的其餘的getSpringFactoriesInstances方法,咱們繼續深刻研究源碼分析
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) { ClassLoader classLoader = Thread.currentThread().getContextClassLoader();//獲取當前ClassLoader // 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; }
咱們能夠從上面代碼看出,這個方法主要是用來建立並獲取實例用的,那麼他是怎麼獲取實例的呢,實例名稱從哪裏來,確定有個配置的地方吧。看看SpringFactoriesLoader.loadFactoryNames,從方法名能夠看出,他是進行獲取工廠名稱的,咱們繼續深挖,看看他是如何實現的this
//用指定的classLoader加載 "META-INF/spring.factories"配置,獲得工廠實例類名稱列表 public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) { String factoryClassName = factoryClass.getName(); return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());//從加載完全部的工廠實例名稱列表中獲取factoryClassName類型的工廠實例列表 }
咱們能夠從上面代碼看出,loadFactoryNames他內部又調用了一下他的重載方法,咱們繼續往下深挖url
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories"; //加載工廠實例類名列表 private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) { MultiValueMap<String, String> result = cache.get(classLoader);//MultiValueMap 是Spring本身實現的一個Map,這裏用來獲取cache中的工廠實例類名列表。這個MultiValueMap很是有意思,他的內部繼承了extends Map<K, List<V>> if (result != null) { return result; } //這裏他會加載Spring Boot 下全部的META-INF/spring.factories下的工廠實例名稱。 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); } }
若是感興趣,請繼續閱讀下一篇rest
spring boot 源碼分析(二) 配置文件加載code