views:

129

answers:

2

Hi all,

I have code which is structured as Spring beans and dependencies among them. This is only a small part of the code since the rest is 'legacy' code.

At this point in time I would like to perform an action on one of these beans in an existing 'legacy' class that extends javax.servlet.ServletContextListener. This class initializes the application and its code is invoked only once.

What is my best course of action?

Thanks!

+2  A: 

There's not really any point in a Spring bean extending ServletContextListener, since the beans don't listen to the servlet context. If you want to use beans that are initialized and destroyed along with the application, then there are many ways to do this in Spring. See this part of the docs.

skaffman
please notice that I had rephrased my question differently...
Yaneeve
+2  A: 

You can obtain the Spring Context this way:

WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContextEvent.getSevletContext())

Then you can call ctx.getBean("name"); (and some more - check the javadocs of WebApplicationContext, and its parents)

Bozho