Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glassfish + Spring

I have a quick question, that is, how do I run applications which use Spring framework on Glassfish server? That is, how do I make it run under control of Spring containers? Do I need to extend the server or something, I can't find much information about that, stuff I read about OSGI modules, just confused me.

like image 255
foFox Avatar asked Feb 21 '23 20:02

foFox


2 Answers

Basically you use web.xml to fire up Spring with a listener and then you map one or more spring Dispatcher servlets. You define controller beans in the dispatcher-servlet.xml, inject beans you defined in the applicationContext, and it sort of cascades down from there.

<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>
like image 163
Affe Avatar answered Feb 23 '23 11:02

Affe


In deployment descriptor(web.xml), define Servlet Listener and context param.

Context param - file location for spring bean files. (wild char allowed and pickup bunch of files that's under that wild char selection.)

Listener - spring class which will listen request. Different classes are available for different purpose.

<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>
like image 45
neo Avatar answered Feb 23 '23 09:02

neo