Spring IOCコアソース解析--refresh方法のソースコード解析
本文はspring 5.2.23.RELEASEバージョンの分析に基づいて、jarバッグspring-context:5.1.13.RELEASE.jar。
Spring IOCの核心的な方法は、AbstractAplication Contect.java類にある。
refresh()方法説明
これらの構成は、xmlファイル、属性ファイル、またはデータベースモードに関する設定情報である。これは起動方法であり、起動に失敗した場合は、作成済みのオブジェクトを廃棄し、資源の浪費を避ける。言い換えれば、この方法を呼び出した後、すべての単一の例またはすべての単一の例を実装するべきである。bean工場を初期化できないなら、BensException異常を投げます。初期化されていて、複数回の更新の試みがサポートされていない場合、何回かの更新はILL Station異常を投げます。
この方法の役割は、コンテキストのクラスキャリアおよびポストプロセッサなどの工場の標準コンテキスト特徴を構成することである。
アプレックスEvent Multicasterを初期化し、コンテキストで定義されていない場合は、SimpleAppliation Event Multicasterを使用します。
Application Listenerをモニターとして実現するbeanを追加します。他の聴解器に影響しません。これらの聴解器はbeanではなく追加できます。
Spring IOCの核心的な方法は、AbstractAplication Contect.java類にある。
refresh()方法説明
これらの構成は、xmlファイル、属性ファイル、またはデータベースモードに関する設定情報である。これは起動方法であり、起動に失敗した場合は、作成済みのオブジェクトを廃棄し、資源の浪費を避ける。言い換えれば、この方法を呼び出した後、すべての単一の例またはすべての単一の例を実装するべきである。bean工場を初期化できないなら、BensException異常を投げます。初期化されていて、複数回の更新の試みがサポートされていない場合、何回かの更新はILL Station異常を投げます。
/**
* Load or refresh the persistent representation of the configuration,
* which might an XML file, properties file, or relational database schema.
* As this is a startup method, it should destroy already created singletons
* if it fails, to avoid dangling resources. In other words, after invocation
* of that method, either all or no singletons at all should be instantiated.
* @throws BeansException if the bean factory could not be initialized
* @throws IllegalStateException if already initialized and multiple refresh
* attempts are not supported
*/
void refresh() throws BeansException, IllegalStateException;
refreshソースpublic void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// 1、
// ,
prepareRefresh();
// 2、 bean
// bean ,
// bean
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// 3、 bean
prepareBeanFactory(beanFactory);
try {
// 4、 bean .
postProcessBeanFactory(beanFactory);
// 5、 bean .
invokeBeanFactoryPostProcessors(beanFactory);
// 6、 bean bean
registerBeanPostProcessors(beanFactory);
// 7、
initMessageSource();
// 8、 .
initApplicationEventMulticaster();
// 9、 bean.
onRefresh();
// 10、beans 、 .
registerListeners();
// 11、 ( ) .
finishBeanFactoryInitialization(beanFactory);
// 12、 :
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
//
destroyBeans();
// 'active' .
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
prepare BenFactory()説明この方法の役割は、コンテキストのクラスキャリアおよびポストプロセッサなどの工場の標準コンテキスト特徴を構成することである。
/**
* Configure the factory's standard context characteristics,
* such as the context's ClassLoader and post-processors.
* @param beanFactory the BeanFactory to configure
*/
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// bean
beanFactory.setBeanClassLoader(getClassLoader());
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// bean
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
// BeanFactory
// MessageSource ( ) bean
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// , bean .
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
// LoadTimeWeaver ( )
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
//
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
// R bean.
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
}
initAplication Event Multicater説明アプレックスEvent Multicasterを初期化し、コンテキストで定義されていない場合は、SimpleAppliation Event Multicasterを使用します。
/**
* Initialize the ApplicationEventMulticaster.
* Uses SimpleApplicationEventMulticaster if none defined in the context.
* @see org.springframework.context.event.SimpleApplicationEventMulticaster
*/
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isTraceEnabled()) {
logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
else {
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isTraceEnabled()) {
logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
}
}
}
registersの説明Application Listenerをモニターとして実現するbeanを追加します。他の聴解器に影響しません。これらの聴解器はbeanではなく追加できます。
/**
* Add beans that implement ApplicationListener as listeners.
* Doesn't affect other listeners, which can be added without being beans.
*/
protected void registerListeners() {
// Register statically specified listeners first.
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let post-processors apply to them!
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// Publish early application events now that we finally have a multicaster...
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}