SpringBoot 创建容器的实现
spring容器的创建对应SpringApplication中run中调用的createApplicationContext方法。这里创建了一个web容器,接下就进去prepareContext容器准备阶段:
privatevoidprepareContext(ConfigurableApplicationContextcontext,ConfigurableEnvironmentenvironment, SpringApplicationRunListenerslisteners,ApplicationArgumentsapplicationArguments,BannerprintedBanner){ //为容器设置环境 context.setEnvironment(environment); //这里的空实现留给开发者扩展,设置数据转换的ConversionService postProcessApplicationContext(context); //执行容器中的Initializers的initialize方法 applyInitializers(context); listeners.contextPrepared(context); if(this.logStartupInfo){ logStartupInfo(context.getParent()==null); logStartupProfileInfo(context); } //Addbootspecificsingletonbeans ConfigurableListableBeanFactorybeanFactory=context.getBeanFactory(); beanFactory.registerSingleton("springApplicationArguments",applicationArguments); if(printedBanner!=null){ beanFactory.registerSingleton("springBootBanner",printedBanner); } if(beanFactoryinstanceofDefaultListableBeanFactory){ ((DefaultListableBeanFactory)beanFactory) .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } if(this.lazyInitialization){ context.addBeanFactoryPostProcessor(newLazyInitializationBeanFactoryPostProcessor()); } //Loadthesources Set
看一下这里的load方法,这里主要把我们的启动类作为Bean注册到了Spring的容器中。
protectedvoidload(ApplicationContextcontext,Object[]sources){ if(logger.isDebugEnabled()){ logger.debug("Loadingsource"+StringUtils.arrayToCommaDelimitedString(sources)); } BeanDefinitionLoaderloader=createBeanDefinitionLoader(getBeanDefinitionRegistry(context),sources); if(this.beanNameGenerator!=null){ loader.setBeanNameGenerator(this.beanNameGenerator); } if(this.resourceLoader!=null){ loader.setResourceLoader(this.resourceLoader); } if(this.environment!=null){ loader.setEnvironment(this.environment); } loader.load(); }
/** *Loadthesourcesintothereader. *@returnthenumberofloadedbeans */ intload(){ intcount=0; for(Objectsource:this.sources){ count+=load(source); } returncount; } privateintload(Objectsource){ Assert.notNull(source,"Sourcemustnotbenull"); if(sourceinstanceofClass>){ returnload((Class>)source); } if(sourceinstanceofResource){ returnload((Resource)source); } if(sourceinstanceofPackage){ returnload((Package)source); } if(sourceinstanceofCharSequence){ returnload((CharSequence)source); } thrownewIllegalArgumentException("Invalidsourcetype"+source.getClass()); } privateintload(Class>source){ if(isGroovyPresent()&&GroovyBeanDefinitionSource.class.isAssignableFrom(source)){ //AnyGroovyLoadersaddedinbeans{}DSLcancontributebeanshere GroovyBeanDefinitionSourceloader=BeanUtils.instantiateClass(source,GroovyBeanDefinitionSource.class); load(loader); } if(isEligible(source)){ this.annotatedReader.register(source); return1; } return0; }
再来看下contextLoaded方法,这里将上下文设置到监听器中,同时也把监听器添加到上下文中。最后发布了一个ApplicationPreparedEvent事件。
publicvoidcontextLoaded(ConfigurableApplicationContextcontext){ for(ApplicationListener>listener:this.application.getListeners()){ if(listenerinstanceofApplicationContextAware){ ((ApplicationContextAware)listener).setApplicationContext(context); } context.addApplicationListener(listener); } this.initialMulticaster.multicastEvent(newApplicationPreparedEvent(this.application,this.args,context)); }
到此这篇关于SpringBoot创建容器的实现的文章就介绍到这了,更多相关SpringBoot创建容器内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!