本篇內(nèi)容介紹了“springboot如何完善上下文加載器”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)建站總部坐落于成都市區(qū),致力網(wǎng)站建設(shè)服務(wù)有成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷策劃、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站維護(hù)、公眾號(hào)搭建、微信小程序開(kāi)發(fā)、軟件開(kāi)發(fā)等為企業(yè)提供一整套的信息化建設(shè)解決方案。創(chuàng)造真正意義上的網(wǎng)站建設(shè),為互聯(lián)網(wǎng)品牌在互動(dòng)行銷領(lǐng)域創(chuàng)造價(jià)值而不懈努力!
源碼:
```
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)聽(tīng)事件
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)不開(kāi)啟延遲加載
if (this.lazyInitialization) {
context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
}
//獲取全部的資源
//這里獲取了啟動(dòng)類的資源和 當(dāng)前SpringApplication中的source資源。
//到目前來(lái)說(shuō)實(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í)候也有用到,這里可以直接通過(guò) context.getEnvironment().getConversionService()獲取到
if (this.addConversionService) {
context.getBeanFactory().setConversionService(ApplicationConversionService.getSharedInstance());
}
}完善與ApplicationContextInitializer接口相關(guān)的對(duì)象屬性。這些對(duì)象在this.initializers中,早在SpringApplication初始化的時(shí)候就已經(jīng)加載。通過(guò)已經(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。
繼上下文創(chuàng)建完成后,又對(duì)其進(jìn)行了一些屬性的初始化,并加載了一些配置資源。
“springboot如何完善上下文加載器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
當(dāng)前文章:springboot如何完善上下文加載器
URL標(biāo)題:http://chinadenli.net/article48/jigshp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、網(wǎng)站導(dǎo)航、網(wǎng)站內(nèi)鏈、定制網(wǎng)站、企業(yè)建站、網(wǎng)站收錄
聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)