I have an existing web application that uses Spring in Tomcat, and I've added a
REST API to it using Restlets with RC4. I use the Noelios ServerServlet as the
entry point.
But when I create my root Restlet, I need to get at business objects that are
already created in a Spring (web) context. For that reason, the Noelios Spring
extension (SpringContext) doesn't really work for me. I can't load my own beans
from an XML file, because my own Restlets have dependencies on other domain
objects in the Spring world.
What I did to make this work in RC4 was to, in my Application.createRoot()
method, use this one line of evil code:
WebApplicationContext wac =
WebApplicationContextUtils.getWebApplicationContext(
((ServletContextAdapter)getContext()).getServletContext()
);
That uses Spring's WebApplicationContextUtils to get the existing Spring context
from the ServletContext. To get the Servlet context, I have to hack the current
RestContext by casting it to the ServletContextAdapter that it really is.
This is obviously not _right_, so naturally when I tried to upgrade to RC5 it
broke. I think that's because the Context setup by the new ServerServlet has
additional wrapper(s) or something.
So,
(1) at first glance I didn't even see how to port my hack. Is there a hackish
way to get the ServletContext in RC5?
(2) Any suggestions for the right way to do this? My first inclination would be
to subclass ServerServlet with something of my own that makes the ServletContext
available.
Sorry for the long post. I thought this might be a common use-case for Spring
users out there, so the answer(s) might benefit many.
Stokes.