When we are working with Spring Framework, we starts wiring all beans into one spring configuration file. Later we realize it is the time to separate them into multiple files based on their behavior like moving DAOs into applicaitonContext-dao.xml and services into applicationContext-service.xml etc. Then next question that pops up will be, how to load multiple spring bean configuration files.
Here is the simple example that shows how to load multiple spring bean configurations.

Step 1: Define Beans and configure them in corresponding configuration files

Lets define Bean named Administrator and configure it in “application-adminConfig.xml” configuration file.

package com.codesimplify;

public class Administrator {
	
	private String firstName;
	private String lastName;
	
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String toString(){
		return "FirstName: "+firstName +" , LastName: "+lastName;
	}
}

Configure the above bean in application-adminConfig.xml file.

<bean name="adminBean" class="com.codesimplify.Administrator">
       <property name="firstName" value="john" />
       <property name="lastName" value="smith" />
  </bean>

Now, lets define User Bean and configure it in “application-userConfig.xml” file.

package com.codesimplify;

public class User {
	private String firstName;
	private String lastName;
	private String age;

	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	
	public String toString(){
		return "UserBean: firstName - "+firstName +", lastName -"+lastName +", Age - "+age;
	}

}

Configure the above bean in application-userConfig.xml file.

 <bean name="userBean" class="com.codesimplify.User">
       <property name="firstName" value="user" />
       <property name="lastName" value="test" />
       <property name="age" value="35" />
  </bean>

Step 2: Import the multiple spring bean configuration files into single configuration

Using import tag , we can import multiple Spring bean configuration files into single file application-context.xml

<?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.1.xsd">

    <import resource="application-adminConfig.xml"  />
	<import resource="application-userConfig.xml" />

</beans>

Step 3: Test Class to load beans from different configuration files.

In the below class, instead of loading multiple Spring bean configuration files individually, we will be loading application-context.xml that imports both the configuration files.

package com.codesimplify.spring.core;

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

import com.codesimplify.Administrator;
import com.codesimplify.User;

public class MultipleConfigApp {

	public static void main(String[] args) {
		// Load Beans into Spring Application Context
		ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
		// Retrieve loaded Bean from application context
		Administrator admin=(Administrator)context.getBean("adminBean");
		
		System.out.println("Administrator Bean:"+admin);
		User userBean=(User)context.getBean("userBean");
		
		System.out.println("User Bean:"+userBean);

	}

}

Output:

22:44:50.496 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'adminBean'
Administrator Bean:FirstName: john , LastName: smith
22:44:50.496 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'userBean'
User Bean:UserBean: firstName - user, lastName -test, Age - 35
How to Load multiple Spring bean configuration files

Leave a Reply

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