SpringBoot 創(chuàng)建容器的實(shí)現(xiàn)
spring 容器的創(chuàng)建對(duì)應(yīng) SpringApplication 中 run 中調(diào)用的 createApplicationContext 方法。這里創(chuàng)建了一個(gè) web 容器,接下就進(jìn)去 prepareContext 容器準(zhǔn)備階段:
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) { //為容器設(shè)置環(huán)境 context.setEnvironment(environment); //這里的空實(shí)現(xiàn)留給開發(fā)者擴(kuò)展,設(shè)置數(shù)據(jù)轉(zhuǎn)換的ConversionService postProcessApplicationContext(context); //執(zhí)行容器中的 Initializers 的 initialize 方法 applyInitializers(context); listeners.contextPrepared(context); if (this.logStartupInfo) { logStartupInfo(context.getParent() == null); logStartupProfileInfo(context); } // Add boot specific singleton beans ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); beanFactory.registerSingleton('springApplicationArguments', applicationArguments); if (printedBanner != null) { beanFactory.registerSingleton('springBootBanner', printedBanner); } if (beanFactory instanceof DefaultListableBeanFactory) { ((DefaultListableBeanFactory) beanFactory) .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } if (this.lazyInitialization) { context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor()); } // Load the sources Set<Object> sources = getAllSources(); Assert.notEmpty(sources, 'Sources must not be empty'); load(context, sources.toArray(new Object[0])); listeners.contextLoaded(context); }
看一下這里的 load 方法,這里主要把我們的啟動(dòng)類作為 Bean 注冊(cè)到了 Spring 的容器中。
protected void load(ApplicationContext context, Object[] sources) { if (logger.isDebugEnabled()) { logger.debug('Loading source ' + StringUtils.arrayToCommaDelimitedString(sources)); } BeanDefinitionLoader loader = 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(); }
/** * Load the sources into the reader. * @return the number of loaded beans */ int load() { int count = 0; for (Object source : this.sources) { count += load(source); } return count; } private int load(Object source) { Assert.notNull(source, 'Source must not be null'); if (source instanceof Class<?>) { return load((Class<?>) source); } if (source instanceof Resource) { return load((Resource) source); } if (source instanceof Package) { return load((Package) source); } if (source instanceof CharSequence) { return load((CharSequence) source); } throw new IllegalArgumentException('Invalid source type ' + source.getClass()); } private int load(Class<?> source) { if (isGroovyPresent() && GroovyBeanDefinitionSource.class.isAssignableFrom(source)) { // Any GroovyLoaders added in beans{} DSL can contribute beans here GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class); load(loader); } if (isEligible(source)) { this.annotatedReader.register(source); return 1; } return 0; }
再來看下 contextLoaded 方法,這里將上下文設(shè)置到監(jiān)聽器中,同時(shí)也把監(jiān)聽器添加到上下文中。最后發(fā)布了一個(gè) ApplicationPreparedEvent 事件。
public void contextLoaded(ConfigurableApplicationContext context) { for (ApplicationListener<?> listener : this.application.getListeners()) { if (listener instanceof ApplicationContextAware) {((ApplicationContextAware) listener).setApplicationContext(context); } context.addApplicationListener(listener); } this.initialMulticaster.multicastEvent(new ApplicationPreparedEvent(this.application, this.args, context)); }
到此這篇關(guān)于SpringBoot 創(chuàng)建容器的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot 創(chuàng)建容器內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP新手必備的基礎(chǔ)知識(shí)2. asp文件用什么軟件編輯3. Docker 啟動(dòng)Redis 并設(shè)置密碼的操作4. CentOS郵箱服務(wù)器搭建系列——SMTP服務(wù)器的構(gòu)建( Postfix )5. PHP基礎(chǔ)之生成器4——比較生成器和迭代器對(duì)象6. JS中6個(gè)對(duì)象數(shù)組去重的方法7. vue+element開發(fā)一個(gè)谷歌插件的全過程8. Vue axios獲取token臨時(shí)令牌封裝案例9. 通過IEAD+Maven快速搭建SSM項(xiàng)目的過程(Spring + Spring MVC + Mybatis)10. 利用CSS制作3D動(dòng)畫

網(wǎng)公網(wǎng)安備