publicSpringApplication(ResourceLoaderresourceLoader,Class<?>...primarySources){this.resourceLoader=resourceLoader;Assert.notNull(primarySources,"PrimarySources must not be null");this.primarySources=newLinkedHashSet<>(Arrays.asList(primarySources));// 1. 推测web应用类型(NONE、REACTIVE、SERVLET)this.webApplicationType=WebApplicationType.deduceFromClasspath();// 2. 从spring.factories中获取BootstrapRegistryInitializer对象this.bootstrapRegistryInitializers=newArrayList<>(getSpringFactoriesInstances(BootstrapRegistryInitializer.class));// 3. 从spring.factories中获取ApplicationContextInitializer对象setInitializers((Collection)getSpringFactoriesInstances(ApplicationContextInitializer.class));// 4. 从spring.factories中获取ApplicationListener对象setListeners((Collection)getSpringFactoriesInstances(ApplicationListener.class));// 5. 推测出Main类(main()方法所在的类)this.mainApplicationClass=deduceMainApplicationClass();}
private<T>Collection<T>getSpringFactoriesInstances(Class<T>type,Class<?>[]parameterTypes,Object...args){ClassLoaderclassLoader=getClassLoader();// Use names and ensure unique to protect against duplicatesSet<String>names=newLinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type,classLoader));List<T>instances=createSpringFactoriesInstances(type,parameterTypes,classLoader,args,names);AnnotationAwareOrderComparator.sort(instances);returninstances;}
privateClass<?>deduceMainApplicationClass(){try{StackTraceElement[]stackTrace=newRuntimeException().getStackTrace();for(StackTraceElementstackTraceElement:stackTrace){if("main".equals(stackTraceElement.getMethodName())){returnClass.forName(stackTraceElement.getClassName());}}}catch(ClassNotFoundExceptionex){// Swallow and continue}returnnull;}
privatevoidprepareContext(DefaultBootstrapContextbootstrapContext,ConfigurableApplicationContextcontext,ConfigurableEnvironmentenvironment,SpringApplicationRunListenerslisteners,ApplicationArgumentsapplicationArguments,BannerprintedBanner){// 将前面生成的Environment设置到Spring容器中context.setEnvironment(environment);// 将设置在SpringApplication上的beanNameGenerator、resourceLoader设置到Spring容器中postProcessApplicationContext(context);// 利用ApplicationContextInitializer初始化Spring容器applyInitializers(context);// 发布ApplicationContextInitializedEvent事件,表示Spring容器初始化完成listeners.contextPrepared(context);// Spring容器初始化好了,就关闭DefaultBootstrapContextbootstrapContext.close(context);if(this.logStartupInfo){logStartupInfo(context.getParent()==null);logStartupProfileInfo(context);}// 注册一些单例BeanConfigurableListableBeanFactorybeanFactory=context.getBeanFactory();beanFactory.registerSingleton("springApplicationArguments",applicationArguments);if(printedBanner!=null){beanFactory.registerSingleton("springBootBanner",printedBanner);}// 设置allowCircularReferences和allowBeanDefinitionOverriding给Spring容器if(beanFactoryinstanceofAbstractAutowireCapableBeanFactory){((AbstractAutowireCapableBeanFactory)beanFactory).setAllowCircularReferences(this.allowCircularReferences);if(beanFactoryinstanceofDefaultListableBeanFactory){((DefaultListableBeanFactory)beanFactory).setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);}}if(this.lazyInitialization){context.addBeanFactoryPostProcessor(newLazyInitializationBeanFactoryPostProcessor());}// 拿到启动配置类(run方法传进来的类)Set<Object>sources=getAllSources();Assert.notEmpty(sources,"Sources must not be empty");// 将启动配置类解析为BeanDefinition注册到Spring容器中load(context,sources.toArray(newObject[0]));// 发布ApplicationPreparedEvent事件,表示Spring容器已经准备好listeners.contextLoaded(context);}