Modularize Spring application

Spring DI types
December 9, 2012
Spring RESTful Web Service Tutorial
December 10, 2012

In real world, there are multiple modules in an enterprise Java EE application. For a fleet management application, these could include Fleet, People, Items and Vendor etc. When managing such an application in Spring, it is best practice to modularize the Spring configuration files too. So for fleet application, we should have config files with the name: fleet-context.xml, people-context.xml, items-context.xml and vendor-context.xml. Each such file shall contain the IoC definitions for the beans in that module.

One can also name the spring config files as per the technical requirements of the project. For example, we can create a config file with the name as excel-context.xml to configure all excel download beans. In this tutorial, I shall show how to create multiple spring config files and then use them as required. There could be some classes which are dependent from one module to another. I am sharing an example where Employees are assigned a Department in the company.

The Employee and Department Bean classes:

Employee.java

package com.javaexperience.employee;

import com.javaexperience.department.Department;

public class Employee {

	private int empId;
	private Department dept;

	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public Department getDept() {
		return dept;
	}
	public void setDept(Department dept) {
		this.dept = dept;
	}
}

Department.java

package com.javaexperience.department;

public class Department {

	int id;
	String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

The main class file:

App.java

package com.javaexperience.main;

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

import com.javaexperience.department.Department;
import com.javaexperience.employee.Employee;

public class App
{

    public static void main( String[] args )
    {
    	ApplicationContext empContext = new ClassPathXmlApplicationContext(
				"employee-module.xml");
    	ApplicationContext deptContext = new ClassPathXmlApplicationContext(
				"department-module.xml");

    	Employee emp1 = (Employee) empContext.getBean("empBean");
    	emp1.setEmpId(1);

		Employee emp2 = (Employee) empContext.getBean("empBean");
		emp2.setEmpId(2);

		Department dept1 = (Department) deptContext.getBean("deptBean");
		dept1.setId(1);
		dept1.setName("abc");

		Department dept2 = (Department) deptContext.getBean("deptBean");
		dept2.setId(1);
		dept2.setName("abc");

		emp1.setDept(dept1);
		emp2.setDept(dept2);
    }
}

The spring config files:

employee-module.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-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<bean id="empBean" class="com.javaexperience.employee.Employee" scope="prototype">
	</bean>
 </beans>

department-module.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-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 	<bean id="deptBean" class="com.javaexperience.department.Department" scope="prototype">
	</bean>
 </beans>

Note that the above code is using ApplicationContext for managing the beans from different modules which shouldn’t be the case with real world projects. There are many alternatives to load the beans in memory apart from using the Spring ApplicationContext or BeanFactory.

A sample Spring framework project showing the creation of multiple spring config files to manage multiple modules can be downloaded below: