0
votes

We need to load the spring application context in our web application after one of our servlets is initialized, so I wonder what is the best way to do it?

I know that it's recommended to use the listener in web.xml, but it's obviously not good for us because in this case the context will be loaded before the first servlet. I saw that there was this class - ContextLoaderServet - in Spring 2.5, but it's absent in Spring 3.0. So I guess we should write some dummy servlet ourselves with the sole purpose of loading the context? Is there any better way?

Thanks.

1
ContextLoaderSerlvet just delegated to ContextLoaderListener anyway. Why do you need to do this? If you explained the reasons, maybe we could suggest an alternative. - skaffman
The reasons are - one of the legacy servlets performs some initialization routine, creating and configuring some data structures that we want to use in the upper layer that uses Spring. Thus Spring context has to be loaded later so it can reference these structures in its beans initialization. - Stas

1 Answers

0
votes

OK, so if you have this legacy servlet that sets stuff up, then you will need to persuade the Spring servlet to load after it.

This is straightforward - use Spring's DispatcherServlet to load the Spring context, and use the standard <load-on-startup> in web.xml to dictate the startup order, e.g.

<servlet>
  <servlet-name>LegacyServlet</servlet-name>
  <servlet-class>com.xy.LegacyServlet</servlet-class>
  <load-on-startup>0</load-on-startup>
</servlet>


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