Table of Contents
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.
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.
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:
1 2 3 4 5 |
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-service.xml", "spring-businessObjects.xml"}); |
Using import:
You can do it using import tag too.
Lets say you have spring-service.xml as below:
1 2 3 4 5 |
<!--?xml version="1.0" encoding="UTF-8"?--> |
1 2 3 4 5 |
<!--?xml version="1.0" encoding="UTF-8"?--> |
1 2 3 4 5 |
<!--?xml version="1.0" encoding="UTF-8"?--> |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.