See the sample apps that come with the Spring distribution, in particular Petclinic and JPetStore: They illustrate the configuration strategy for web environments. Petclinic uses Hibernate and Spring web MVC; JPetStore uses iBATIS SQL Maps and features alternative Spring/Struts web tiers.
Basically, you keep your middle tier definitions in an XML file in the WEB-INF directory, typically "applicationContext.xml". This file gets loaded by a ContextLoaderServlet or ContextLoaderListener (prefer the latter in a Servlet 2.3/2.4 container, provided that listeners get invoked before load-on-startup servlets in your container).
From then on, you can access your root WebApplicationContext (and thus the beans defined there) in any web component, preferably via Spring's WebApplicationContextUtils.getWebApplicationContext(ServletContext) method: This works with Servlets, Struts, WebWork, even plain JSPs.
If you use Spring's web MVC framework on top, define DispatcherServlets with their own bean definition files. You can refer to your middle tier beans there, as a DispatcherServlet context has access to the beans in your root context. If all your controllers are Spring-managed, you typically don't need to use WebApplicationContextUtils at all.
Juergen
|