PDA

View Full Version : Deploying servlets from Spring config


emmby
Dec 9th, 2006, 09:17 PM
I have a warfile with several servlets in it, and at application runtime I'd like to be able to dynamically choose which servlets will be deployed. In particular, I'd like to be able to do it from my spring configuration files since that's where my other dynamic configuration goes (I keep at least some of my spring files outside of the warfile so the apps can be dynamically reconfigured)

I can imagine it would look something like defining each servlet as a bean in my spring config, and having a single servlet dispatcher in my web.xml that would dispatch to whatever servlets I want deployed.

Has anyone seen any solutions out there to do this? I checked the Spring 1.x and 2.x documentation but don't see anything like this.

Thanks
Mike

cwash5
Dec 9th, 2006, 10:09 PM
If I understand you correctly. I don't believe this is possible. All servlets need to be specified in the web.xml file. This is outside the scope of springs configuration. You could specify a generic servlet in the web.xml and have spring inject or you can lookup in the web application context that servlet specific classes in order to get the configurable behavior you are looking for.

emmby
Dec 10th, 2006, 03:48 AM
Hi cwash5,

Yup, you've understood my question correctly. And your proposals are more or less what I was thinking might work, but I haven't tried it myself yet and was hoping someone else might have a little experience with it.

It would be nice because I think it's the last frontier of things I can't configure dynamically in the application. Everything can be modified from the spring files except these servlets that have to go into web.xml at build time.

Seems like at the very least i could write a dispatcher servlet that dispatches to other servlets, but it would be cooler if I could write a servlet or listener that would instead dynamically deploy other servlets in tomcat based on the spring config.

Mike

Dave Syer
Dec 10th, 2006, 10:19 AM
I think this is exactly what Spring's ServletWrappingController is intended for. The javadocs are pretty complete.

emmby
Dec 10th, 2006, 10:05 PM
Brilliant! Worked like a charm once I fixed an amusing bug in a subclass of XmlWebApplicationContext (someone had declared a member variable named servletContext that was shadowing the real servletContext in some situations)

Thank you david_syer and cwash5 for your help

Mike

Dave Syer
Dec 11th, 2006, 03:21 AM
If you initialise your application context in the usual (ContextLoaderListener) way, the servlet context property should be *the* servlet context, not a "shadow" of it. What made you think it was a bug?

emmby
Dec 12th, 2006, 08:11 PM
We have our own subclass of XmlWebApplicationContext that we use as the context in our applications. It knows how to initialize itself with some configuration information particular to our app.

Not saying it's the best way to do things but it's the way things are currently done.

So anyway, that subclass was returning null when asked for the servletContext, which caused all kinds of confusion for a little while.