Java – Glassfish + Spring

glassfishjavaspring

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.

Best Solution

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>