How to load multiple Spring configuration files


In this post , we will see how to load multiple spring bean configuration files. Sometimes, you have multiple spring bean configuration file , you may have different configuration file for service, different for business object.
When you are working on complex application, it is not good practice to have only single xml configuration, you can split it into multiple file for better maintenance.
There are two ways to do it.

  • Using application context constructor
  • Using import in xml bean

Using application context constructor:

You can simply create a String array with name of multiple configuration file and pass it application context constructor.
For example:
You can load multiple files using below code:

Using import:

You can do it using import tag too.
Lets say you have spring-service.xml as below:

You have another spring configuration called spring-businessObjects.xml as below:

You can simply import above two files in another xml file called applicationcontext.xml. You need to load applicationcontext.xml to load all bean objects.


Add Comment