Splitting applicationContext to multiple files Splitting applicationContext to multiple files spring spring

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.


Mike Nereson has this to say on his blog at:

http://blog.codehangover.com/load-multiple-contexts-into-spring/

There are a couple of ways to do this.

1. web.xml contextConfigLocation

Your first option is to load them all into your Web application context via the ContextConfigLocation element. You’re already going to have your primary applicationContext here, assuming you’re writing a web application. All you need to do is put some white space between the declaration of the next context.

  <context-param>      <param-name> contextConfigLocation </param-name>      <param-value>          applicationContext1.xml          applicationContext2.xml      </param-value>  </context-param>  <listener>      <listener-class>          org.springframework.web.context.ContextLoaderListener      </listener-class>  </listener>

The above uses carriage returns. Alternatively, yo could just put in a space.

  <context-param>      <param-name> contextConfigLocation </param-name>      <param-value> applicationContext1.xml applicationContext2.xml </param-value>  </context-param>  <listener>      <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>  </listener>

2. applicationContext.xml import resource

Your other option is to just add your primary applicationContext.xml to the web.xml and then use import statements in that primary context.

In applicationContext.xml you might have…

  <!-- hibernate configuration and mappings -->  <import resource="applicationContext-hibernate.xml"/>  <!-- ldap -->  <import resource="applicationContext-ldap.xml"/>  <!-- aspects -->  <import resource="applicationContext-aspects.xml"/>

Which strategy should you use?

1. I always prefer to load up via web.xml.

Because , this allows me to keep all contexts isolated from each other. With tests, we can load just the contexts that we need to run those tests. This makes development more modular too as components stay loosely coupled, so that in the future I can extract a package or vertical layer and move it to its own module.

2. If you are loading contexts into a non-web application, I would use the import resource.


There are two types of contexts we are dealing with:

1: root context (parent context. Typically include all jdbc(ORM, Hibernate) initialisation and other spring security related configuration)

2: individual servlet context (child context.Typically Dispatcher Servlet Context and initialise all beans related to spring-mvc (controllers , URL Mapping etc)).

Here is an example of web.xml which includes multiple application context file

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">    <display-name>Spring Web Application example</display-name>    <!-- Configurations for the root application context (parent context) -->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>            /WEB-INF/spring/jdbc/spring-jdbc.xml <!-- JDBC related context -->            /WEB-INF/spring/security/spring-security-context.xml <!-- Spring Security related context -->        </param-value>    </context-param>    <!-- Configurations for the DispatcherServlet application context (child context) -->    <servlet>        <servlet-name>spring-mvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>                /WEB-INF/spring/mvc/spring-mvc-servlet.xml            </param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>spring-mvc</servlet-name>        <url-pattern>/admin/*</url-pattern>    </servlet-mapping></web-app>