SpringBoot 创建容器的实现

网友投稿 305 2022-11-17


SpringBoot 创建容器的实现

spring 容器的创建对应 SpringApplication 中 run 中调用的 createApplicationContext 方法。这里创建了一个 web 容器,接下就进去 prepareContext 容器准备阶段:

private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,

SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {

//为容器设置环境

context.setEnvironment(environment);

//这里的空实现留给开发者扩展,设置数据转换的ConversionService

postProcessApplicationContext(context);

//执行容器中的 Initializers 的 initialize 方法

applyInitializers(context);

listeners.contextPrepared(context);

if (this.logStartupInfo) {

logStartupInfo(coJISuCKQGWntext.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 sources = getAllSources();

Assert.notEmpty(sources, "Sources must not be empty");

load(context, sources.toArray(new Object[0]));

listeners.contextLoaded(context);

}

看一下这里的 load 方法,这里主要把我们的启动类作为 Bean 注册到了 Spring 的容器中。

protected void load(ApplicationContext context, Object[] sources) {

if (logger.isDebugEnabled()) {

logger.debug("Loading source " + StringUtils.arrayToCommaDelimitedString(sJISuCKQGWources));

}

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);

http:// load(loader);

}

if (isEligible(source)) {

this.annotatedReader.register(source);

return 1;

}

return 0;

}

再来看下 contextLoaded 方法,这里将上下文设置到监听器中,同时也把监听器添加到上下文中。最后发布了一个 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));

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring如何处理表单提交
下一篇:如何基于java实现Gauss消元法过程解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~