2012-04-10 43 views
4

我有一個簡單的問題,那就是如何運行在Glassfish服務器上使用Spring框架的應用程序?也就是說,如何讓它在Spring容器的控制下運行?我是否需要擴展服務器或其他什麼東西,我找不到有關這方面的很多信息,我讀到的關於OSGI模塊的東西,只是困惑了我。Glassfish + Spring

回答

7

基本上你使用web.xml來啓動一個監聽器,然後映射一個或多個Spring Dispatcher servlet。您在dispatcher-servlet.xml中定義控制器bean,注入您在applicationContext中定義的bean,並從那裏向下級聯。

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/applicationContext.xml 
     etc etc 
    </param-value> 
</context-param> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

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

在部署描述符(web.xml),定義的Servlet監聽器和上下文PARAM。

上下文參數 - spring bean文件的文件位置。 (野生字符允許和皮卡一堆下的文件,這是野生字符選擇。)

聽者 - 春天班,將聽取請求。不同的類可用於不同的目的。

<context-param> 
    <param-name>contextConfigLocation</param-name> 
      <!-- All file ends with Context.xml under web-inf folder --> 
    <param-value>WEB-INF/*Context.xml</param-value> 
</context-param> 


<listener> 
    <display-name>Spring context loader</display-name> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    <!-- use following if you want to use request scope --> 
    <!-- org.springframework.web.context.request.RequestContextListener --> 
</listener> 

<servlet> 
    <servlet-name>servlet name</servlet-name> 
    <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> 
</servlet> 

<servlet-mapping> 
    <servlet-name>name</servlet-name> 
    <url-pattern>/URLName</url-pattern> 
</servlet-mapping>