Load Multiple Spring Bean Configuration Files

When you have large size project, its good approach to keep all spring bean definition in separate files.

You can put all files into a single XML file like:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<import resource="Spring-Bean-Employee.xml" />
	<import resource="Spring-Database.xml" />	

You can load single file like this:

ApplicationContext context = new ClassPathXmlApplicationContext("Spring-All-Modules.xml");

There is another approach, rather than importing “Spring-Bean-Employee.xml” and “Spring-Database.xml” into “Spring-All-Modules.xml”, We can directly load both the configuration files. Just pass the String array which contains the file name.

Note: “Spring-Bean-Employee.xml” and “Spring-Database.xml” should be available under class path.

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Bean-Employee.xml",

You can download the source code of this example here.

One Response to Load Multiple Spring Bean Configuration Files

  1. vimalraj March 12, 2013 at 12:59 pm #

    in previous post the ‘beanfactory’ object is created using ‘ClassPathXmlApplicationContext()’ .but in this post ‘ApplicationContext’ used instead of ‘beanfactory’.

    i have 2 question.

    1.why we use ApplicationContext instead of using BeanFactory
    2.How the ClassPathXmlApplicationContext returns 2 differend object.

    awaiting response …

Leave a Reply