Splitting applicationContext to multiple files

I find the following setup the easiest.

Use the default config file loading mechanism of DispatcherServlet:

The framework will, on initialization
of a DispatcherServlet, look for a
file named [servlet-name]-servlet.xml
in the WEB-INF directory of your web
application and create the beans
defined there (overriding the
definitions of any beans defined with
the same name in the global scope).

In your case, simply create a file intrafest-servlet.xml in the WEB-INF dir and don’t need to specify anything specific information in web.xml.

In intrafest-servlet.xml file you can use import to compose your XML configuration.

<beans>
  <bean id="bean1" class="..."/>
  <bean id="bean2" class="..."/>

  <import resource="foo-services.xml"/>
  <import resource="foo-persistence.xml"/>
</beans>

Note that the Spring team actually prefers to load multiple config files when creating the (Web)ApplicationContext. If you still want to do it this way, I think you don’t need to specify both context parameters (context-param) and servlet initialization parameters (init-param). One of the two will do. You can also use commas to specify multiple config locations.

Leave a Comment