How to load multiple bean configuration files in Spring


If you are planning to develop a large-scale Spring-based application that has many modules and stuck with the managing bean configurations file. To resolve this type of problem, you can create separate bean configuration file for every module.

For example, you have a school management project and have the different module like the principal, teacher, and student, create a separate bean configuration file for each module like principal.xml, teacher.xml, and student.xml respectively.

Now the problem is loading all these bean configurations at a single time. To do that you have two ways either define all bean configurations file in ClassPathXmlApplicationContext() separated by comma or import all bean configurations file in a one configuration file.

Define all the bean configurations file in String array.

String[] configFile = { "teacher-spring.xml", "student-spring.xml" };
ApplicationContext context = new ClassPathXmlApplicationContext(configFile);

Or you can directly pass the bean configurations file separated by a comma.

ApplicationContext contextNew = new ClassPathXmlApplicationContext("teacher-spring.xml", "student-spring.xml");

Or import all in one.

<beans>
	<import resource="teacher-spring.xml" />
	<import resource="student-spring.xml" />
</beans>

Note: The better way is the organize all spring bean configurations file in a single bean configuration file.

Project Structure in Eclipse IDE

Have a look at project structure and check where all the configurations file placed.

How to load multiple bean configuration files in Spring

Spring Bean

Create two classes Teacher and Student and display some message in the default constructor.

Teacher.java
package org.websparrow.beans;

public class Teacher {

	public Teacher() {
		System.out.println("I am default constructor of Teacher class.");
	}
}
Student.java
package org.websparrow.beans;

public class Student {

	public Student() {
		System.out.println("I am default constructor of Student class.");
	}
}

Spring Bean Configuration

Define all above classes in a separate configuration file.

teacher-spring.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
 "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 
<beans>
	<bean id="t" class="org.websparrow.beans.Teacher"/>
</beans>
student-spring.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
 "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id="j" class="org.websparrow.beans.Student" />
</beans>

and import all in one.

admin-spring.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
 "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<import resource="teacher-spring.xml" />
	<import resource="student-spring.xml" />
</beans>

Execution

Finally, load the bean configurations file and run it.

Admin.java
package org.websparrow.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Admin {

	public static void main(String[] args) {

		/*
		// Define all the bean configurations file in String array
		String[] configFile = { "teacher-spring.xml", "student-spring.xml" };
		ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
		
		// Or you can directly pass
		ApplicationContext contextNew = new ClassPathXmlApplicationContext("teacher-spring.xml", "student-spring.xml");
		*/
		
		ApplicationContext contextImport = new ClassPathXmlApplicationContext("admin-spring.xml");

	}
}
Output:

You will get the below message in your console log.

I am default constructor of Teacher class.
I am default constructor of Student class.

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.