2010-11-19 31 views
2

我在Spring MVC的Web應用程序類似,使用一個碼頭HTTP服務器,它包含一個WebAppContextJetty的Spring MVC - 應用程序servlet.xml文件中的內容是什麼?

<bean id="Server" class="org.mortbay.jetty.Server" init-method="start" 
    destroy-method="stop"> 
    <property name="connectors"> 
     <list> 
      <bean id="Connector" class="org.mortbay.jetty.nio.SelectChannelConnector"> 
       <property name="port" value="9531" /> 
      </bean> 
     </list> 
    </property> 

    <property name="handler"> 
     <bean id="handlers" class="org.mortbay.jetty.handler.HandlerCollection"> 
      <property name="handlers"> 
       <list> 
        <bean id="contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection"> 
         <property name="handlers"> 
          <list> 
           <bean class="org.mortbay.jetty.webapp.WebAppContext"> 
            <property name="contextPath" value="/" /> 
            <property name="war" value="/etc/WebContent" /> 
            <!-- <property name="copyWebDir" value="true" /> --> 
           </bean> 
          </list> 
         </property> 
        </bean> 
       </list> 
      </property> 
     </bean> 
    </property> 
</bean> 

我試圖找出其中豆類都應該被定義。基本上有兩種選擇:我的特定於應用程序的Spring配置文件(稱爲「myApp-context.xml」)或servlet Spring配置文件(稱爲「myApp-servlet.xml」)。 web.xml文件中的相關部分是:

<servlet-mapping> 
    <servlet-name>myApp</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:jeff/myApp-context.xml</param-value> 
</context-param> 

到目前爲止,這麼好。基於其他相關的問題/答案,我將諸如Spring MVC視圖解析器(與webapp直接相關)的東西放入myApp-servlet.xml以及其他特定於應用的bean(如DAO,權利實用程序等)放入myApp- context.xml(在web.xml中作爲「contextConfigLocation」給出)。這工作正常,應用程序啓動,一切都被正確注入,Spring MVC控制器工作正常,等等。

我注意到,但是,在myApp-servlet.xml中定義的任何bean(根據約定,必須存在於web根目錄中,因爲我的servlet名稱是「myApp」)由Spring加載兩次。具體來說,有一個日誌消息,如「初始化Spring FrameworkServlet'myApp'」,在此之後,再次加載myApp-servlet.xml中的所有bean。

如果我將所有這些bean從myApp-servlet.xml移動到myApp-context.xml,myApp-servlet.xml爲空(僅打開和關閉標記),則應用程序成功啓動(此時更快),並執行不要創建每個bean兩次。但是,Jetty的WebAppContext似乎堅持存在一個myApp-servlet.xml文件(如果其中一個不存在,應用程序無法啓動)。它還要求定義contextConfigLocation並指向一個有效的Spring配置文件。

任何人都可以幫我理清我的配置嗎?離開myApp-servlet.xml沒有任何bean定義感覺是對的,因爲應用程序啓動速度更快,沒有冗餘bean初始化,但錯誤,因爲web應用上下文似乎要求它存在。把一些bean放到myApp-servlet.xml裏感覺很對,因爲它會是空的,但是錯誤,因爲現在這些bean被初始化了兩次,啓動需要更長的時間,並且選擇放置哪些bean感覺是任意的。感謝您的幫助,並根據需要提供更多詳情/摘要。

編輯:現在我很清楚這兩個不同的原因。 myApp-servlet.xml是一個servlet特定的Spring配置文件。所以如果這個servlet是一個Spring MVC servlet,那麼應該把控制器等放在那個文件中。也可以有myApp2-servlet.xml和myApp3-servlet.xml,在同一個容器中定義另外兩個servlet。所有這些都會自然地使bean彼此獨立(因爲它們是完全獨立的web應用程序)。

另一方面,myApp-context.xml文件包含將提供給每個 servlet的bean定義。正如Neeme Praks所指出的那樣,這就是爲什麼數據庫應用程序,權利等應該進入該文件的原因。很有可能每個Web應用程序在呈現和功能上完全分離時都需要使用一些常用實用程序。這是定義那些「常見」豆的地方。

回答

1

在一般情況下,你有一個正確的設置:

  • myApp-servlet.xml應包含直接的web應用相關的豆類
  • myApp-context.xml應該包含業務邏輯豆類(如DAO中,權利公用事業)

至於原因,爲什麼你的webapp相關的bean被加載兩次 - 不知道。 他們不應該被加載兩次。也許你是閱讀日誌錯誤?也許有一些配置錯誤,這是從您提供的配置片段不明顯(完整的web.xml,myApp-servlet.xmlmyApp-context.xml會很有用)?

參見:

+0

謝謝 - 我能看到的問題是,我包括對myApp-servlet.xml中(連同對myApp-context.xml中)作爲web.xml中contextConfigLocation屬性的值。這顯然會導致這些bean被加載兩次。不幸的是,當我沒有在contextConfigLocation中包含myApp-servlet.xml時,我遇到了一個不同的問題(Spring MVC控制器無法接受請求),所以我需要更多地處理它。 – 2010-11-23 15:41:46

相關問題