Load multiple Spring bean configuration file

  • Facebook
  • Google
  • LinkedIn
  • Twitter

In complex Spring-based applications, it is good practice you have to break Spring`s application context file into multiple files in order to modularize the application`s configuration into smaller pieces for easy maintenance. Spring allows this by providing the <import> element in the bean configuration file so you need to maintain different modules and different bean configuration file for specific module.

AdminUser.java

package com.javatutsworld;

public class AdminUser {
	
	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;
	}
}

adminUserConfig.xml

<bean name="adminUser" class="com.javatutsworld.AdminUser">
       <property name="firstName" value="Vinod" />
       <property name="lastName" value="Kumar" />
</bean>
package com.javatutsworld;

public class Employee {
	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 "EmployeeBean: firstName - "+firstName +", lastName -"+lastName +", Age - "+age;
	}

}

employeeConfig.xml

 <bean name="userEmployee" class="com.javatutsworld.Employee">
       <property name="firstName" value="Ravi" />
       <property name="lastName" value="Kumar" />
       <property name="age" value="30" />
</bean>

Now this is the main configuration file which import the multiple Spring bean configuration files using import tag.

applicationContext.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="adminUserConfig.xml"  />
    <import resource="employeeConfig.xml" />

</beans>

Now you will be loading applicationContext.xml that imports both the configuration files.

Main.java

package com.javatutsworld.test;

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

import com.javatutsworld.AdminUser;
import com.javatutsworld.Employee;

public class Main {

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

	}

}
      Advertisements