Andrew Barton wrote:
The WebApplicationContext is the way that I go. I find that you can get to the ServletContext wherever it matters (even in the Filter, as you no doubt found out).We had the same problem. I have come up with a solution, but it may be a bit of a hack. If anyone else has a better solution, I would love to hear it.
My solution involves the use of a singleton, initialized by a servlet that loads at application start up. I then use a utility to simplify the access to the singleton.
I have included the source to the pieces.
My spring configuration in web.xml is as follows (the last item calls the servlet that initializes the singleton):
<!-- spring configuration -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</liste
ner-class>
</listener>
<servlet>
<servlet-name>ContextServlet</servlet-name>
<servlet-class>eblox.commons.spring.ContextServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
--
Once this is all done, getting at a spring managed bean can be done anywhere
and is as easy as: SpringUtils.getBean("myBean");
I split up applicationContexts into many different ones for discrete purposes (like modelled objects in hibernate, DAOs, Services, application-specific) and then load them sequentially (so that you get a root context that has access to all of the chained contexts):
web.xml:
<!-- Spring application context locations --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/org/ognl/model/applicationContext-model.xml, classpath:/org/ognl/dao/applicationContext-dao.xml, classpath:/org/ognl/service/applicationContext-service.xml, /WEB-INF/applicationContext.xml </param-value> </context-param>
<!-- Listeners -->
<!-- Spring application context loader; loads when context is initialized -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
This makes it much more configurable, especially since I learned the classpath: prefix trick for the config locations.
Of course you can use WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()) [whew!] to get the ApplicationContext for the entire application.
Just a tip. Use it together. Use it in peace.
- Drew
-- +---------------------------------+ < Drew Davidson | OGNL Technology > +---------------------------------+ | Email: [EMAIL PROTECTED] / | Web: http://www.ognl.org / | Vox: (520) 531-1966 < | Fax: (520) 531-1965 \ | Mobile: (520) 405-2967 \ +---------------------------------+
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]