<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    spring有三種啟動方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.
    看一下ContextLoaderListener的源碼,這是一個ServletContextListener
    /**
    ? * Initialize the root web application context.
    ? */
    ?public void contextInitialized(ServletContextEvent event) {
    ? this.contextLoader = createContextLoader();
    ? this.contextLoader.initWebApplicationContext(event.getServletContext());
    ?}
    ?
    ? /**
    ? * Create the ContextLoader to use. Can be overridden in subclasses.
    ? * @return the new ContextLoader
    ? */
    ?protected ContextLoader createContextLoader() {
    ? return new ContextLoader();
    ?}

    ?contextLoader的源碼
    ?public WebApplicationContext initWebApplicationContext(ServletContext servletContext)
    ?? throws BeansException {

    ? long startTime = System.currentTimeMillis();
    ? if (logger.isInfoEnabled()) {
    ?? logger.info("Root WebApplicationContext: initialization started");
    ? }
    ? servletContext.log("Loading Spring root WebApplicationContext");

    ? try {
    ?? // Determine parent for root web application context, if any.
    ?? ApplicationContext parent = loadParentContext(servletContext);

    ?? WebApplicationContext wac = createWebApplicationContext(servletContext, parent);
    ?? servletContext.setAttribute(
    ???? WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

    ?? if (logger.isInfoEnabled()) {
    ??? logger.info("Using context class [" + wac.getClass().getName() +
    ????? "] for root WebApplicationContext");
    ?? }
    ?? if (logger.isDebugEnabled()) {
    ??? logger.debug("Published root WebApplicationContext [" + wac +
    ????? "] 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 wac;
    ? }
    ? 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.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,這里面放了WebApplicationContext,需要使用時從ServletContext取出
    ?可以使用WebApplicationContextUtils得到WebApplicationContext
    ?public static WebApplicationContext getWebApplicationContext(ServletContext sc) {
    ? Object attr = sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    ? if (attr == null) {
    ?? return null;
    ? }
    ? if (attr instanceof RuntimeException) {
    ?? throw (RuntimeException) attr;
    ? }
    ? if (attr instanceof Error) {
    ?? throw (Error) attr;
    ? }
    ? if (!(attr instanceof WebApplicationContext)) {
    ?? throw new IllegalStateException("Root context attribute is not of type WebApplicationContext: " + attr);
    ? }
    ? return (WebApplicationContext) attr;
    ?}
    ?關鍵的問題在于struts如何啟動的spring的,ContextLoaderPlugIn的源碼
    ?
    ?// Publish the context as a servlet context attribute.
    ? String attrName = getServletContextAttributeName();
    ? getServletContext().setAttribute(attrName, wac);
    ?
    ?public String getServletContextAttributeName() {
    ? return SERVLET_CONTEXT_PREFIX + getModulePrefix();
    ?}
    ?不同加載的Key竟然不同,原因就是WebApplicationContext放在那里的問題,可spring調用的時候會根據WebApplicationContext里面定義的那個名字去找的,問題出在這里


    ?在struts-config.xml中配置
    ??? <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    ????? <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
    ??? </plug-in>

    ??? <controller>
    ??????? <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" />
    ??? </controller>


    ?原理是這樣的,Struts雖然只能有一個ActionServlet實例,但是對于不同的子應用分別能有自己的RequestProcessor實例每個RequestProcessor實例分別對應不同的struts配置文件。
    ?? 子應用的ProcessorClass類必須重寫一般就是繼承RequestProcessor類,然后再其配置文件的controller元素中的<processorClass>屬性中作出修改。那么當
    ? getRequestProcessor(getModuleConfig(request)).process(request,response);就能根據request選擇相應的moduleconfig,再根據其<processorClass>屬性選擇相應的RequestProcessor子類來處理相應的請求了。

    ?

    posted on 2006-11-09 10:40 jackstudio 閱讀(13350) 評論(3)  編輯  收藏 所屬分類: javahibernatestrutsspring
    主站蜘蛛池模板: 亚洲精品成人久久| 亚洲国产精品无码久久一线| 亚洲国产日韩在线一区| 久久精品一本到99热免费| 亚洲AV中文无码字幕色三| 免费福利在线视频| 亚洲高清国产AV拍精品青青草原 | 又大又硬又爽免费视频| 久久精品国产亚洲AV电影网 | 亚洲精品老司机在线观看| 黄色毛片免费观看| 亚洲片国产一区一级在线观看| 亚洲国产免费综合| 亚洲午夜未满十八勿入网站2| 免费一级毛片无毒不卡| 亚洲成AV人片久久| 午夜老司机免费视频| 国产99久久亚洲综合精品| 亚洲一区二区视频在线观看| a毛片在线免费观看| 亚洲春色在线观看| 国产禁女女网站免费看| 好男人看视频免费2019中文| 国产精品亚洲аv无码播放| 日本在线看片免费| 亚洲另类小说图片| 国产又长又粗又爽免费视频| 丰满人妻一区二区三区免费视频| 亚洲精品亚洲人成在线观看| 57pao一国产成视频永久免费| 自拍日韩亚洲一区在线| 亚洲国产精品毛片av不卡在线| 国产成人AV免费观看| 久久精品国产亚洲AV蜜臀色欲 | 亚洲乱妇熟女爽到高潮的片| 亚洲高清国产拍精品青青草原| 久久久久久久99精品免费观看| 亚洲av专区无码观看精品天堂| 亚洲国产成人久久综合野外| 特级无码毛片免费视频尤物| 亚洲色大成网站www久久九|