国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Spring Boot啟動過程完全解析(二)

2019-11-26 12:31:04
字體:
供稿:網(wǎng)友

上篇給大家介紹了Spring Boot啟動過程完全解析(一),大家可以點擊參考下

  該說refreshContext(context)了,首先是判斷context是否是AbstractApplicationContext派生類的實例,之后調(diào)用了強轉(zhuǎn)為AbstractApplicationContext類型并調(diào)用它的refresh方法。由于AnnotationConfigEmbeddedWebApplicationContext繼承自EmbeddedWebApplicationContext,所以會執(zhí)行EmbeddedWebApplicationContext的refresh方法,繼而執(zhí)行其中的super.refresh。這個refresh也就是AbstractApplicationContext的refresh方法了,它內(nèi)部是一個synchronized鎖全局的代碼塊,同樣的加鎖方法還有這個類里的close和registerShutdownHook方法。

  同步代碼塊中第一個方法prepareRefresh,首先會執(zhí)行AnnotationConfigEmbeddedWebApplicationContext的prepareRefresh方法:

 protected void prepareRefresh() {  this.scanner.clearCache();  super.prepareRefresh(); }

  這個super也就是AbstractApplicationContext,它的prepareRefresh方法邏輯是:生成啟動時間;設置closed狀態(tài)為false;active狀態(tài)為true;initPropertySources方法主要是調(diào)用了AbstractEnvironment的getPropertySources方法獲取了之前SpringApplication的prepareEnvironment方法中g(shù)etOrCreateEnvironment方法準備的各種環(huán)境變量及配置并用于初始化ServletPropertySources。具體的servletContextInitParams這些是在環(huán)境對象初始化時由各集成級別Environment的customizePropertySources方法中初始化的。

   接著的getEnvironment().validateRequiredProperties()方法實際執(zhí)行了AbstractEnvironment中的this.propertyResolver.validateRequiredProperties(),主要是驗證了被占位的key如果是required的值不能為null。prepareRefresh的最后是初始化this.earlyApplicationEvents = new LinkedHashSet<ApplicationEvent>()。*****

  只夠是獲取BeanFactory實例的方法obtainFreshBeanFactory(),首先在refreshBeanFactory方法中用原子布爾類型判斷是否刷新過,BeanFactory實例是在createApplicationContext創(chuàng)建Context實例時被創(chuàng)建的,如果沒有刷新則設置一個用于序列化的id,id是ContextIdApplicationContextInitializer初始化設置的(如未配置該初始化器,是有一個默認ObjectUtils.identityToString(this)生成的),這個id的生成規(guī)則是spring.config.name截取的+":"+server.port的占位截取。設置序列化id時,同時保存了一個id和弱引用DefaultListableBeanFactory實例映射。

  得到了beanFactory后就是prepareBeanFactory(beanFactory)了,邏輯是注冊了BeanClassLoader用于注入的bean實例的創(chuàng)建;StandardBeanExpressionResolver用于EL表達式,比如配置文件或者@Value("#{...}")等使用;用ResourceEditorRegistrar注冊屬性轉(zhuǎn)換器,比如xml配置的bean屬性都是用的字符串配置的要轉(zhuǎn)成真正的屬性類型;addBeanPostProcessor(new ApplicationContextAwareProcessor(this))注冊ApplicationContextAwareProcessor,它的invokeAwareInterfaces方法會對實現(xiàn)指定接口的bean調(diào)用指定的set方法;ignoreDependencyInterface忽略對這些接口的自動裝配,比如Aware這些是要做獨立處理的,不適合通用的方法;然后是有幾個類型直接手動注冊,比如BeanFactory,這個很好理解;接著注冊一個后置處理器ApplicationListenerDetector的實例,addBeanPostProcessor注冊的會按照注冊先后順序執(zhí)行;這個方法的最后判斷了特定的4個bean名字,如果存在會做相應注冊,包括loadTimeWeaver、environment、systemProperties和systemEnvironment。補充一點,在最開始創(chuàng)建實例的時候還執(zhí)行過ignoreDependencyInterface(BeanNameAware.class);ignoreDependencyInterface(BeanFactoryAware.class);ignoreDependencyInterface(BeanClassLoaderAware.class)。

 protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {  // Tell the internal bean factory to use the context's class loader etc.  beanFactory.setBeanClassLoader(getClassLoader());  beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));  beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));  // Configure the bean factory with context callbacks.  beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));  beanFactory.ignoreDependencyInterface(EnvironmentAware.class);  beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);  beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);  beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);  beanFactory.ignoreDependencyInterface(MessageSourceAware.class);  beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);  // BeanFactory interface not registered as resolvable type in a plain factory.  // MessageSource registered (and found for autowiring) as a bean.  beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);  beanFactory.registerResolvableDependency(ResourceLoader.class, this);  beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);  beanFactory.registerResolvableDependency(ApplicationContext.class, this);  // Register early post-processor for detecting inner beans as ApplicationListeners.  beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));  // Detect a LoadTimeWeaver and prepare for weaving, if found.  if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {   beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));   // Set a temporary ClassLoader for type matching.   beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));  }  // Register default environment beans.  if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {   beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());  }  if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {   beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());  }  if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {   beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());  } }

   之后到了refresh的postProcessBeanFactory方法,首先是會走到AnnotationConfigEmbeddedWebApplicationContext的Override,需要注意的一點是,這是web環(huán)境,如果不是是不會加載這個上下文的,也就不會這么走。它重寫的第一步是先走super也就是EmbeddedWebApplicationContext的postProcessBeanFactory,這里又注冊了個后置處理器WebApplicationContextServletContextAwareProcessor的實例,構(gòu)造參數(shù)是this,也就是當前上下文,同時忽略ServletContextAware接口,這個接口是用于獲取ServletContext的,為什么要忽略呢,我猜應該是因為我們既然有了web應用并且內(nèi)嵌servlet的上下文實例,還要ServletContext的實現(xiàn)就沒什么用了,還有可能出現(xiàn)沖突的問題,有空我再確認下。然后是配置的basePackages和annotatedClasses:

@Override protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {  super.postProcessBeanFactory(beanFactory);  if (this.basePackages != null && this.basePackages.length > 0) {   this.scanner.scan(this.basePackages);  }  if (this.annotatedClasses != null && this.annotatedClasses.length > 0) {   this.reader.register(this.annotatedClasses);  } }

  到了invokeBeanFactoryPostProcessors方法,這個方法就是執(zhí)行之前注冊的BeanFactory后置處理器的地方。代碼一目了然,PostProcessorRegistrationDelegate的invokeBeanFactoryPostProcessors中只是有些排序的邏輯,我就不說了:

 /**  * Instantiate and invoke all registered BeanFactoryPostProcessor beans,  * respecting explicit order if given.  * <p>Must be called before singleton instantiation.  */ protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {  PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());  // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime  // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)  if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {   beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));   beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));  } }

   BeanFactory后置處理器執(zhí)行之后是注冊Bean的后置處理器方法registerBeanPostProcessors。例如new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount)會在Bean沒有合適的后置處理器時記條info級日志。ApplicationListenerDetector也注冊了一個。

  initMessageSource這個方法在我這沒什么用,都說是國際化的,隨便百度一下一堆一堆的,而且其實嚴格來說這篇多數(shù)不屬于spring boot的部分,這方法我就不細寫了。

  initApplicationEventMulticaster方法主要也就是初始化并注冊applicationEventMulticaster的這兩句代碼:          

this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);   beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);

   onRefresh也是根據(jù)環(huán)境不同加載的上下文不同而不同的,用于支持子類擴展出來的上下文特定的邏輯的。EmbeddedWebApplicationContext的onRefresh首先依然是super.onRefresh,邏輯就是初始化了主題;

createEmbeddedServletContainer方法名我就不翻譯了,一般情況下是使用getBeanFactory .getBeanNamesForType方法找到EmbeddedServletContainerFactory類型的實例,這也就是我之前那個問題解決過程中,為什么只要排除掉tomcat引用,引入jetty引用就可以自動換成jetty的原因。創(chuàng)建容器的過程中初始化方法selfInitialize注冊了filter和MappingForUrlPatterns等,代碼在AbstractFilterRegistrationBean等onStartup,這里就不細說了,如果能抽出時間說說之前查問題的時候查的容器代碼再說。然后初始化PropertySources,servletContextInitParams和servletConfigInitParams:

 public static void initServletPropertySources(   MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig) {  Assert.notNull(propertySources, "'propertySources' must not be null");  if (servletContext != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) &&    propertySources.get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {   propertySources.replace(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,     new ServletContextPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, servletContext));  }  if (servletConfig != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) &&    propertySources.get(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {   propertySources.replace(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,     new ServletConfigPropertySource(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME, servletConfig));  } }

  registerListeners首先注冊靜態(tài)監(jiān)聽:

 @Override public void addApplicationListener(ApplicationListener<?> listener) {  synchronized (this.retrievalMutex) {   this.defaultRetriever.applicationListeners.add(listener);   this.retrieverCache.clear();  } }

  接著是:

 

 registerListeners的最后,初始化過的earlyApplicationEvents如果有事件,這時候會被發(fā)布。

  finishBeanFactoryInitialization結(jié)束BeanFactory的初始化并初始化所有非延遲加載的單例。事實上我們自定義的單例Bean都是在這里getBean方法初始化的,所以如果注冊的Bean特別多的話,這個過程就是啟動過程中最慢的。初始化開始前先設置configurationFrozen為true,并this.frozenBeanDefinitionNames = StringUtils.toStringArray ( this. beanDefinitionNames )。如果有bean實例實現(xiàn)了SmartInitializingSingleton會有后置處理觸發(fā),不包括延遲加載的。例如:org.springframework.context.event. internalEventListenerProcessor會觸發(fā)EventListenerMethodProcessor的afterSingletonsInstantiated方法對所有對象(Object的子類)處理。

  finishRefresh:Refresh的最后一步,發(fā)布相應事件。同樣先執(zhí)行EmbeddedWebApplicationContext中對應方法的super(EmbeddedWebApplicationContext)的對應方法:

 /**  * Finish the refresh of this context, invoking the LifecycleProcessor's  * onRefresh() method and publishing the  * {@link org.springframework.context.event.ContextRefreshedEvent}.  */ protected void finishRefresh() {  // Initialize lifecycle processor for this context.  initLifecycleProcessor();  // Propagate refresh to lifecycle processor first.  getLifecycleProcessor().onRefresh();  // Publish the final event.  publishEvent(new ContextRefreshedEvent(this));  // Participate in LiveBeansView MBean, if active.  LiveBeansView.registerApplicationContext(this); }

   初始化生命周期處理器,邏輯是判斷beanFactory中是否已經(jīng)注冊了lifecycleProcessor,沒有就new一個DefaultLifecycleProcessor并setBeanFactory(beanFactory),然后將它賦值給私有LifecycleProcessor類型的this變量。然后執(zhí)行生命周期處理器的onRefresh,其中先startBeans,被start的beans是通過getBeanNamesForType(Lifecycle.class, false, false)從beanFactory中取出來的,例如endpointMBeanExporter和lifecycleProcessor,會去調(diào)用bean的start方法,endpointMBeanExporter的start中執(zhí)行 locateAndRegisterEndpoints方法并設置running屬性為true,這個過程加了ReentrantLock鎖。bean都啟動完會設置處理器的running為true。刷新完會發(fā)布ContextRefreshedEvent事件,這個事件除了都有的記錄時間還執(zhí)行了ConfigurationPropertiesBindingPostProcessor的freeLocalValidator方法,我這的邏輯是實際上執(zhí)行了ValidatorFactoryImpl的close方法。這個邏輯的最后會檢查一個配置spring.liveBeansView.mbeanDomain是否存在,有就會創(chuàng)建一個MBeanServer:

static void registerApplicationContext(ConfigurableApplicationContext applicationContext) {  String mbeanDomain = applicationContext.getEnvironment().getProperty(MBEAN_DOMAIN_PROPERTY_NAME);  if (mbeanDomain != null) {   synchronized (applicationContexts) {    if (applicationContexts.isEmpty()) {     try {      MBeanServer server = ManagementFactory.getPlatformMBeanServer();      applicationName = applicationContext.getApplicationName();      server.registerMBean(new LiveBeansView(),        new ObjectName(mbeanDomain, MBEAN_APPLICATION_KEY, applicationName));     }     catch (Throwable ex) {      throw new ApplicationContextException("Failed to register LiveBeansView MBean", ex);     }    }    applicationContexts.add(applicationContext);   }  } }

  finishRefresh最后會啟動前面創(chuàng)建的內(nèi)嵌容器,并發(fā)布EmbeddedServletContainerInitializedEvent事件,啟動這一部分算是容器的邏輯了,有機會整理容器邏輯再細寫,我這里是Tomcat的:

@Override public void start() throws EmbeddedServletContainerException {  try {   addPreviouslyRemovedConnectors();   Connector connector = this.tomcat.getConnector();   if (connector != null && this.autoStart) {    startConnector(connector);   }   checkThatConnectorsHaveStarted();   TomcatEmbeddedServletContainer.logger     .info("Tomcat started on port(s): " + getPortsDescription(true));  }  catch (ConnectorStartFailedException ex) {   stopSilently();   throw ex;  }  catch (Exception ex) {   throw new EmbeddedServletContainerException(     "Unable to start embedded Tomcat servlet container", ex);  }  finally {   Context context = findContext();   ContextBindings.unbindClassLoader(context, getNamingToken(context),     getClass().getClassLoader());  } }

  然后是resetCommonCaches:

 /**  * Reset Spring's common core caches, in particular the {@link ReflectionUtils},  * {@link ResolvableType} and {@link CachedIntrospectionResults} caches.  * @since 4.2  * @see ReflectionUtils#clearCache()  * @see ResolvableType#clearCache()  * @see CachedIntrospectionResults#clearClassLoader(ClassLoader)  */ protected void resetCommonCaches() {  ReflectionUtils.clearCache();  ResolvableType.clearCache();  CachedIntrospectionResults.clearClassLoader(getClassLoader()); }

  refreshContext的最后是注冊shutdown的鉤子:

 if (this.registerShutdownHook) {   try {    context.registerShutdownHook();   }   catch (AccessControlException ex) {    // Not allowed in some environments.   }  } /**  * Register a shutdown hook with the JVM runtime, closing this context  * on JVM shutdown unless it has already been closed at that time.  * <p>Delegates to {@code doClose()} for the actual closing procedure.  * @see Runtime#addShutdownHook  * @see #close()  * @see #doClose()  */ @Override public void registerShutdownHook() {  if (this.shutdownHook == null) {   // No shutdown hook registered yet.   this.shutdownHook = new Thread() {    @Override    public void run() {     synchronized (startupShutdownMonitor) {      doClose();     }    }   };   Runtime.getRuntime().addShutdownHook(this.shutdownHook);  } }

咱最近用的github:https://github.com/saaavsaaa

以上所述是小編給大家介紹的Spring Boot啟動過程完全解析(二),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 房产| 上蔡县| 凌海市| 岳普湖县| 乌鲁木齐县| 新化县| 乌海市| 中阳县| 龙州县| 眉山市| 颍上县| 肃宁县| 钟祥市| 柞水县| 弋阳县| 兴仁县| 张掖市| 永清县| 嵊州市| 卢氏县| 廉江市| 银川市| 巨鹿县| 江都市| 乌兰县| 云南省| 揭阳市| 青岛市| 台湾省| 北海市| 平利县| 铜山县| 石泉县| 策勒县| 南汇区| 亳州市| 平陆县| 高台县| 玉山县| 陵水| 西林县|