Spring: Loading multiple configuration XML files

In this article, we will discuss what should be the best solution if we got more than one spring configuration XML files in a large enterprise application

Typically, in an ideal enterprise application we tend to get more than one configuration files to maintain its modularity and re-useable features. In these scenarios, while on server startup all the configuration needs to be loaded

Now the question is how exactly we can achieve this as in the case of earlier examples we have got just one configuration file

There are two possible solutions to come out of this situation.

Initially, let’s concentrate on how we can leverage the ‘ApplicationContext’ to load multiple files later on detailing the best solution with ‘import’ resources tag in the Spring configuration XML file

ApplicationContext

If we got below modules for our large enterprise application and one configuration XML file for each module

  • Network Service    – NetworkService.xml
  • Customer Service  – CustomerService.xml
  • Inventory Service  – InventoryService.xml

Then using ApplicationContext we can load all these files at one go, let’s see an example


// loading spring context xml from classpath
ApplicationContext appCtx = new ClassPathXmlApplicationContext(
                                 new String[] {
                                  "NetworkService.xml",
                                  "CustomerService.xml",
                                  "InventoryService.xml"
                             });

Assumption: files listed in the string array are in classpath

Best Solution

Above approach is bit cumbersome and error-prone in all likelihood, if we needs to add new configuration files in the ApplicationContext as it requires making our hands dirty

Instead, we need to move on with the configuration files rather than touching the code

Use import resources in the root context XML file to import all other configuration files thereby not touching the code anytime even if we got a new requirement to load to new configuration files

Let’s see a Spring root context files importing other configuration files

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<import resource="classpath:com/spring/series/NetworkService.xml" />
	<import resource="classpath:com/spring/series/CustomerService.xml" />
	<import resource="classpath:com/spring/series/InventoryService.xml" />

</beans>

Now instead of loading each and every files using ApplicationContext, we can load just one root context file where we have configured to import all other configuration files from the classpath

// loading spring context xml from classpath
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                           "com/spring/series/SpringRootContext.xml"
                       );

Happy Coding !!
Happy Learning !!

Spring Collections
Spring Inner Bean