publicvoidrefresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// ...try {// ...// Initialize other special beans in specific context subclasses.// 9. 子类的多态onRefreshonRefresh();// ...// Last step: publish corresponding event.// 12. 完成容器的创建工作finishRefresh(); }catch (BeansException ex) {// ... }finally {// Reset common introspection caches in Spring's core, since we// might not ever need metadata for singleton beans anymore...// 13. 清除缓存resetCommonCaches(); } }}
12. finishRefresh:完成容器的创建工作
protectedvoidfinishRefresh() {// Clear context-level resource caches (such as ASM metadata from scanning).// 清除资源缓存(如扫描的ASM元数据)clearResourceCaches();// Initialize lifecycle processor for this context.// 初始化生命周期处理器initLifecycleProcessor();// Propagate refresh to lifecycle processor first.// 将刷新传播到生命周期处理器getLifecycleProcessor().onRefresh();// Publish the final event.// 发布容器刷新完成的事件,让监听器去回调各自的方法publishEvent(new ContextRefreshedEvent(this));// Participate in LiveBeansView MBean, if active.LiveBeansView.registerApplicationContext(this);}
A common interface defining methods for start/stop lifecycle control. The typical use case for this is to control asynchronous processing. NOTE: This interface does not imply specific auto-startup semantics. Consider implementing SmartLifecycle for that purpose. Can be implemented by both components (typically a Spring bean defined in a Spring context) and containers (typically a Spring ApplicationContext itself). Containers will propagate start/stop signals to all components that apply within each container, e.g. for a stop/restart scenario at runtime. Can be used for direct invocations or for management operations via JMX. In the latter case, the org.springframework.jmx.export.MBeanExporter will typically be defined with an org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler, restricting the visibility of activity-controlled components to the Lifecycle interface. Note that the present Lifecycle interface is only supported on top-level singleton beans. On any other component, the Lifecycle interface will remain undetected and hence ignored. Also, note that the extended SmartLifecycle interface provides sophisticated integration with the application context's startup and shutdown phases.
An extension of the Lifecycle interface for those objects that require to be started upon ApplicationContext refresh and/or shutdown in a particular order. The isAutoStartup() return value indicates whether this object should be started at the time of a context refresh. The callback-accepting stop(Runnable) method is useful for objects that have an asynchronous shutdown process. Any implementation of this interface must invoke the callback's run() method upon shutdown completion to avoid unnecessary delays in the overall ApplicationContext shutdown.
protectedServletWebServerFactorygetWebServerFactory() {// Use bean names so that we don't consider the hierarchy//获取IOC容器中类型为ServletWebServerFactory的BeanString[] beanNames =getBeanFactory().getBeanNamesForType(ServletWebServerFactory.class);if (beanNames.length==0) {thrownewApplicationContextException("Unable to start ServletWebServerApplicationContext due to missing "+"ServletWebServerFactory bean."); }if (beanNames.length>1) {thrownewApplicationContextException("Unable to start ServletWebServerApplicationContext due to multiple "+"ServletWebServerFactory beans : "+StringUtils.arrayToCommaDelimitedString(beanNames)); }returngetBeanFactory().getBean(beanNames[0],ServletWebServerFactory.class);}
Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple CommandLineRunner beans can be defined within the same application context and can be ordered using the Ordered interface or @Order annotation. If you need access to ApplicationArguments instead of the raw String array consider using ApplicationRunner.
Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple ApplicationRunner beans can be defined within the same application context and can be ordered using the Ordered interface or @Order annotation.