Define Multiple DataSources in Spring

This tutorial will show you how we can get a list of data sources using Spring framework. For this tutorial we will create a standalone maven project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

Sometimes we need to define a list of datasources for the purpose of insert or update in different database servers. So in this case we can have different datasource bean ids configured into the spring context XMl file and we can use those datasources as per our needs in the different classes when required by accessing the bean id reference. We can also think of a situation where we need to create a datasource holder which will give us a list of all datasources that may be used for different purpose.

I will create here a DataSourceHolder class which will actually give a list of all datasources defined in the Spring context XMl file.


The following configurations are required in order to run the application

Eclipse Kepler
JDK 1.8
Have maven installed and configured
Spring dependencies in pom.xml

Now we will see the below steps how to create a maven based spring project in Eclipse

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.


Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : spring-multiple-datasource

Step 2. Modify the pom.xml file as shown below.

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Step 4. Create src/main/resources folder for putting the resource files.

Do right-click on the project and go New -> Source Folder. Give Folder name: as src/main/resources and click on Finish button.

Step 5. Create an XML file under src/main/resources.

Do right-click on src/main/resources in the project and go New -> file. Give File name: as spring-multiple-datasource-properties.xml and click on Finish button.

Step 6. Create spring-multiple-datasource.xml file under src/main/resources with the below content.

Step 7. Create DataSourceHolder class with the below source code.

Step 8. Create a main class for getting a list of datasources.

Step 10. Run the above class you will get the below output in the console.

That’s all. Thank you for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.