本篇內(nèi)容介紹了“springboot怎么完善上下文加載器”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、成都網(wǎng)站制作、渦陽網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、渦陽網(wǎng)絡(luò)營(yíng)銷、渦陽企業(yè)策劃、渦陽品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供渦陽建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:muchs.cn源碼:
``` private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) { //注入環(huán)境屬性 context.setEnvironment(environment); //上下文后置處理 this.postProcessApplicationContext(context); //完善初始化類的屬性 this.applyInitializers(context); //發(fā)送監(jiān)聽事件 listeners.contextPrepared(context); //日志 if (this.logStartupInfo) { this.logStartupInfo(context.getParent() == null); this.logStartupProfileInfo(context); } //注冊(cè)傳入的配置參數(shù)為bean,這里將傳入的參數(shù)封裝成applicationArguments,內(nèi)部類似命令行 ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); beanFactory.registerSingleton("springApplicationArguments", applicationArguments); //banner打印 if (printedBanner != null) { beanFactory.registerSingleton("springBootBanner", printedBanner); } //這里默認(rèn)情況下bean定義不允許重復(fù) if (beanFactory instanceof DefaultListableBeanFactory) { ((DefaultListableBeanFactory)beanFactory).setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } //默認(rèn)不開啟延遲加載 if (this.lazyInitialization) { context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor()); } //獲取全部的資源 //這里獲取了啟動(dòng)類的資源和 當(dāng)前SpringApplication中的source資源。 //到目前來說實(shí)際上只有啟動(dòng)類資源 Set<Object> sources = this.getAllSources(); Assert.notEmpty(sources, "Sources must not be empty"); this.load(context, sources.toArray(new Object[0])); listeners.contextLoaded(context); } ```
老樣子進(jìn)行逐個(gè)分析。但看這個(gè)方法并不復(fù)雜,整體上對(duì)上下文和工廠類進(jìn)行配置的完善。
源碼:
protected void postProcessApplicationContext(ConfigurableApplicationContext context) { // 是否自定義bean名稱生成類 if (this.beanNameGenerator != null) { context.getBeanFactory().registerSingleton("org.springframework.context.annotation.internalConfigurationBeanNameGenerator", this.beanNameGenerator); } //是否指定類加載器 if (this.resourceLoader != null) { if (context instanceof GenericApplicationContext) { ((GenericApplicationContext)context).setResourceLoader(this.resourceLoader); } if (context instanceof DefaultResourceLoader) { ((DefaultResourceLoader)context).setClassLoader(this.resourceLoader.getClassLoader()); } } //是否添加數(shù)據(jù)轉(zhuǎn)換器 //在初始化環(huán)境對(duì)象的時(shí)候也有用到,這里可以直接通過 context.getEnvironment().getConversionService()獲取到 if (this.addConversionService) { context.getBeanFactory().setConversionService(ApplicationConversionService.getSharedInstance()); } }
完善與ApplicationContextInitializer接口相關(guān)的對(duì)象屬性。這些對(duì)象在this.initializers中,早在SpringApplication初始化的時(shí)候就已經(jīng)加載。通過已經(jīng)初始化好的上下文對(duì)相關(guān)類進(jìn)行完善。調(diào)用接口的initialize方法。
源碼:
protected void load(ApplicationContext context, Object[] sources) { if (logger.isDebugEnabled()) { logger.debug("Loading source " + StringUtils.arrayToCommaDelimitedString(sources)); } //構(gòu)建一個(gè)bean定義的加載器 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); } //將資源加載成bean loader.load(); } void load() { for (Object source : this.sources) { load(source); } } //按資源類型分別進(jìn)行加載, private void load(Object source) { Assert.notNull(source, "Source must not be null"); if (source instanceof Class<?>) { load((Class<?>) source); return; } if (source instanceof Resource) { load((Resource) source); return; } if (source instanceof Package) { load((Package) source); return; } if (source instanceof CharSequence) { load((CharSequence) source); return; } throw new IllegalArgumentException("Invalid source type " + source.getClass()); }
主要加載了SpringApplication內(nèi)初始化的資源,包括我們的啟動(dòng)類xxApplication將會(huì)被注冊(cè)成bean。
“springboot怎么完善上下文加載器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)頁名稱:springboot怎么完善上下文加載器-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://muchs.cn/article42/deggec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、App開發(fā)、網(wǎng)站建設(shè)、虛擬主機(jī)、搜索引擎優(yōu)化、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容