標籤:webx java spring 啟動
1 WebxContextLoaderListener Webx Framework 通過配置在web.xml中的WebxContextLoaderListener來初始化Spring
<!-- 裝載/WEB-INF/webx.xml, /WEB-INF/webx-*.xml --> <listener> <listener-class><strong>com.alibaba.citrus.webx.context.WebxContextLoaderListener</strong></listener-class> </listener>
WebxContextLoaderListener 是ContextLoaderListener的衍生類別。它定製了自己的ContextLoader——WebxComponentsLoader}
<span style="background-color: rgb(240, 240, 240);"> <pre name="code" class="html"> public class WebxContextLoaderListener extends ContextLoaderListener {</span>
@Override protected final ContextLoader createContextLoader() { <strong> return new WebxComponentsLoader()</strong> { @Override protected Class<? extends WebxComponentsContext> getDefaultContextClass() { Class<? extends WebxComponentsContext> defaultContextClass = WebxContextLoaderListener.this .getDefaultContextClass(); if (defaultContextClass == null) { defaultContextClass = super.getDefaultContextClass(); } return defaultContextClass; } }; } protected Class<? extends WebxComponentsContext> getDefaultContextClass() { return null; }}WebxComponentsLoader其與基類ContextLoader相比,定義了一些自己的欄位:
<pre name="code" class="html">public class WebxComponentsLoader extends ContextLoader { private final static Logger log = LoggerFactory.getLogger(WebxComponentsLoader.class); private String webxConfigurationName; private ServletContext servletContext; private WebApplicationContext componentsContext; private WebxComponentsImpl components;
...
}
之後,使用代理模式由WebxComponentsLoader調用initWebApplicationContext繼續初始化過程。實際上,initWebApplicationContext是其基類ContextLoader的方法,而WebxComponentsLoader重寫了此方法
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) throws IllegalStateException, BeansException { this.servletContext = servletContext; init(); return super.initWebApplicationContext(servletContext); }
這裡,WebxComponentsLoader擷取了servletContext, 通過init方法設定context中WebxConfiguration的名稱。然後繼續調用基類的同名方法,root application開始初始化。
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {throw new IllegalStateException("Cannot initialize context because there is already a root application context present - " +"check whether you have multiple ContextLoader* definitions in your web.xml!");}Log logger = LogFactory.getLog(ContextLoader.class);servletContext.log(<strong>"Initializing Spring root WebApplicationContext"</strong>);if (logger.isInfoEnabled()) {logger.info(<strong>"Root WebApplicationContext: initialization started"</strong>);}long startTime = System.currentTimeMillis();try {// Determine parent for root web application context, if any.ApplicationContext parent = loadParentContext(servletContext);// Store context in local instance variable, to guarantee that// it is available on ServletContext shutdown.<strong>this.context = createWebApplicationContext(servletContext, parent);</strong>servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);ClassLoader ccl = Thread.currentThread().getContextClassLoader();if (ccl == ContextLoader.class.getClassLoader()) {currentContext = this.context;}else if (ccl != null) {currentContextPerThread.put(ccl, this.context);}if (logger.isDebugEnabled()) {logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");}if (logger.isInfoEnabled()) {long elapsedTime = System.currentTimeMillis() - startTime;logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");}return this.context;}catch (RuntimeException ex) {logger.error("Context initialization failed", ex);servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);throw ex;}catch (Error err) {logger.error("Context initialization failed", err);servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);throw err;}}此方法中最重要的部分是對應用上下文WebApplicationContext的建立WebApplicationContext 在createWebApplicationContext方法中,定義了ConfigurableWebApplicationContext 對象wac. wac在返回給ContextLoader前,主要完成以下幾個過程:
wac.setParent(parent);wac.setServletContext(sc);wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));customizeContext(sc, wac);wac.refresh();
經過前幾個步驟的準備工作後,由refresh方法初始化所有bean。 而refresh方法使用原廠模式重新整理beans。
public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.prepareRefresh();// Tell the subclass to refresh the internal bean factory.<strong>ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();</strong>// Prepare the bean factory for use in this context.prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses.postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context.invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.registerBeanPostProcessors(beanFactory);// Initialize message source for this context.initMessageSource();// Initialize event multicaster for this context.initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses.onRefresh();// Check for listener beans and register them.registerListeners();// Instantiate all remaining (non-lazy-init) singletons.<strong>finishBeanFactoryInitialization(beanFactory);</strong>// Last step: publish corresponding event.finishRefresh();}catch (BeansException ex) {// Destroy already created singletons to avoid dangling resources.destroyBeans();// Reset 'active' flag.cancelRefresh(ex);// Propagate exception to caller.throw ex;}}}
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); 這一步驟擷取了/WEB-INF/ 目錄下所有設定檔。finishBeanFactoryInitialization(beanFactory); 完成整個beanfactory的初始化過程。
另外,此方法通過遞迴方式初始化了所有子應用。
初始化流程草圖如下:
Webx啟動流程