SpringApplication中的prepareContext,其内容如下:
private void
prepareContext
(ConfigurableApplicationContext context
,
ConfigurableEnvironment environment
,
SpringApplicationRunListeners listeners
,
ApplicationArguments applicationArguments
,
Banner printedBanner) {
context.setEnvironment(environment)
;
postProcessApplicationContext(context)
;
applyInitializers(context)
;
listeners.contextPrepared(context)
;
if
(
this
.
logStartupInfo
) {
logStartupInfo(context.getParent() ==
null
)
;
logStartupProfileInfo(context)
;
}
// Add boot specific singleton beans
context.getBeanFactory().registerSingleton(
"springApplicationArguments"
,
applicationArguments)
;
if
(printedBanner !=
null
) {
context.getBeanFactory().registerSingleton(
"springBootBanner"
,
printedBanner)
;
}
// Load the sources
Set<Object> sources = getSources()
;
Assert.
notEmpty
(sources
,
"Sources must not be empty"
)
;
load(context
,
sources.toArray(
new
Object[sources.size()]))
;
listeners.contextLoaded(context)
;
}
一、context.setEnvironment(environment);
@Override
public void
setEnvironment
(ConfigurableEnvironment environment) {
super
.setEnvironment(environment)
;
this
.
reader
.setEnvironment(environment)
;
this
.
scanner
.setEnvironment(environment)
;
}
这个方法就是在父类AbstractContext,以及其属性AnnotatedBeanDefinitionReader和ClassPathBeanDefinitionScanner类型中设置environgment属性。
二、applyInitializers(context);
这个方法就是获取之前获取到的所有initializer类型的类,其接口类ApplicationContextInitializer类型的所需类型“interface org.springframework.context.ConfigurableApplicationContext”类。
三、listeners.contextPrepared(context);
EventPublishingRunLIstener发布contextPrepare事件,因为此事件为空,所以此次未做任何操作。紧接着的方法是做了日志打印的操作,也不多做介绍。
四、context.getBeanFactory().registerSingleton("springApplicationArguments",applicationArguments);
这个方法是将applicationArguments实例作为名为springApplicationArguments的spring的单例。后面的一个方法用同样的方式注册了printedBanner。
五、load(context, sources.toArray(new Object[sources.size()]));
这个方法先是创建了BeanDefinitionLoader类型,这个类的作用就是扫描并加载bean,我们看下创建方法,和其含有的属性便可看出其功能,如下:
BeanDefinitionLoader(BeanDefinitionRegistry registry, Object... sources) { Assert.notNull(registry, "Registry must not be null"); Assert.notEmpty(sources, "Sources must not be empty"); this.sources = sources; this.annotatedReader = new AnnotatedBeanDefinitionReader(registry); this.xmlReader = new XmlBeanDefinitionReader(registry); if (isGroovyPresent()) { this.groovyReader = new GroovyBeanDefinitionReader(registry); } this.scanner = new ClassPathBeanDefinitionScanner(registry); this.scanner.addExcludeFilter(new ClassExcludeFilter(sources)); }
然后在这个方法的后面调用loader.load()方法将启动类加入到ApplicationContext中。
六、listeners.contextLoaded(context);
这个方法进去后,具体内容如下:
@Override 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)); }此方法是将在SpringApplciation的Initialize()方法中注册的listener中属于ApplicationContextAware类型的bean都执行方法setApplicationContext,同时将这些Listener都加入到ApplicationContext中,并发布事件ApplicationPreparedEvent。