For a regular Servlet, I guess you could declare a context listener, but for Spring MVC would Spring make this any easier?
Furthermore, if I define a context listener and then would need to access the beans defined in my servlet.xml or applicationContext.xml, how would I get access to them?
Spring has some standard events which you can handle.
To do that, you must create and register a bean that implements the
ApplicationListenerinterface, something like this:You then register this bean within your
servlet.xmlorapplicationContext.xmlfile:and Spring will notify it when the application context is initialized.
In Spring 3 (if you are using this version), the
ApplicationListenerclass is generic and you can declare the event type that you are interested in, and the event will be filtered accordingly. You can simplify a bit your bean code like this: