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

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

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

    posts - 120,  comments - 19,  trackbacks - 0

    前幾天不是一個同事使用OpenSessionInView pattern時,遇到Hibernate 3的mappinglazy="true"的問題,也不會想到它
    struts啟動spring的WebApplicationContext
    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-08-22 16:14 阿成 閱讀(344) 評論(0)  編輯  收藏 所屬分類: Struts
    主站蜘蛛池模板: 免费在线中文日本| 日本免费人成视频在线观看| 亚洲AV成人潮喷综合网| 成全在线观看免费观看大全| 2022年亚洲午夜一区二区福利| 日韩免费高清一级毛片在线| 中文在线观看免费网站| 亚洲欧洲另类春色校园网站| 亚洲精品视频在线观看你懂的| 1000部拍拍拍18免费网站| 免费一级做a爰片久久毛片潮| 亚洲天堂久久精品| 永久黄网站色视频免费观看| 亚洲AV成人潮喷综合网| 亚洲成A人片在线观看WWW| 亚洲一级毛片免费在线观看| 亚洲乱妇熟女爽到高潮的片| 午夜福利不卡片在线播放免费| 国产亚洲高清在线精品不卡| 亚洲精品国产情侣av在线| 日本中文一区二区三区亚洲| 国产免费丝袜调教视频| 一级中文字幕免费乱码专区| 33333在线亚洲| 亚洲国产成人一区二区精品区| 免费观看国产精品| 久久久久久精品成人免费图片| 四虎成人精品国产永久免费无码| 亚洲国产成人精品无码区在线网站| 亚洲自偷自偷在线制服| 在线观看视频免费国语| 最近中文字幕完整免费视频ww | 亚洲Aⅴ无码一区二区二三区软件| 91香蕉在线观看免费高清| av午夜福利一片免费看久久| 亚洲精品国产摄像头| 亚洲一区中文字幕久久| 永久免费毛片手机版在线看| 亚洲人成网址在线观看| 日本一卡精品视频免费 | 国产偷国产偷亚洲高清在线|