How to load multiple Spring bean configuration file

Problem

In a large project structure, the Spring’s bean configuration files are located in different folders for easy maintainability and modular. For example, Spring-Common.xml in common folder, Spring-Connection.xml in connection folder, Spring-ModuleA.xml in ModuleA folder…and etc.

You may load multiple Spring bean configuration files in the code :


	ApplicationContext context = 
    	new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml",
              "Spring-Connection.xml","Spring-ModuleA.xml"});

Put all spring xml files under project classpath.


	project-classpath/Spring-Common.xml
	project-classpath/Spring-Connection.xml
	project-classpath/Spring-ModuleA.xml

Solution

The above ways are lack of organizing and error prone, the better way should be organized all your Spring bean configuration files into a single XML file. For example, create a Spring-All-Module.xml file, and import the entire Spring bean files like this :

File : Spring-All-Module.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<import resource="common/Spring-Common.xml"/>
        <import resource="connection/Spring-Connection.xml"/>
        <import resource="moduleA/Spring-ModuleA.xml"/>
	
</beans>

Now you can load a single xml file like this :


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

Put this file under project classpath.


	project-classpath/Spring-All-Module.xml
Note
In Spring3, the alternative solution is using JavaConfig @Import.

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
reddynr
Guest
reddynr

Hi,

I have a requirement to load applicationContext-local.xml dynamically [based on parameter]. here is the sample

applicationContext_local_IND.xml
applicationContext_local_MX.xml

any advice. Thank you

baba
Guest
baba

What is the result in case that resources are imported into an applicationContext.xml file? Do I get a one large applicationConext file, I mean all the imported stuff, and the stuff in the importing file act like they were all in the same file?
Or each file has its own context?
An example that I can think of is when a bean is configured as singleton in one of the files – is it going to be the same instance across all the imported and importing files?

Mayank
Guest
Mayank

Can u send ? all three spring xml file so that everything clear because still confuse in module spring xml fille.

shareef
Guest
shareef

please check mkyong work on my github account

https://github.com/shareefhiasat/mkyong

chitta
Guest
chitta

if suppose there are two spring config file one is for service and one is for DAO if I want to get a ref of service bean in to dao bean so how can i refer in spring xml file?

abhishek
Guest
abhishek

good

Ashis pandey
Guest
Ashis pandey

Thanks Really very clear cut example.for how we configure multiple configuration

Thanks Keep it Up!!!!

van helsing
Guest
van helsing

this is bean.xml file. whenever i change property name like if i give name of property is “song” then it will show exceptions why?????

saran
Guest
saran

i have seen above type of usage, what does it mean?

cherry
Guest
cherry

i have seen above type of usage, what does it mean?

Narasim
Guest
Narasim

Its really helped me in fixing multiple outgoing emails generation in quartz spring email schedule . It suppose to generate single mail on loading the applicationcontext.xml to generate emails. I created new configuration.xml as mentioned in this blog fixed my issue.

Your blog is Very useful, togo and informative to many people. Thanks and please keep it up.

Steve
Guest
Steve

He left out the quotes when creating the ApplicationContext, it should be:

ApplicationContext context = new ClassPathXmlApplicationContext("Spring-All-Module.xml");
Jay
Guest
Jay

I really do enjoy your blogs. Very useful and informative. Please keep it up.

Kumar Saurabh
Guest
Kumar Saurabh
Please let me know what should be the scenario in case, common/Spring-Common.xml is already loaded by some ClassPathXmlApplicationContext before it is again loaded through Spring-All-Module.xml. In my case, it is failing with org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to register bean definition with name ‘packageAdmin’ Offending resource: URL [jar:file:/home/pg/partygaming/lib/inhouse/pg-serviceregistry-non_osgi-extra.jar!/META-INF/spring/serviceregistry-nonosgi-be an-context.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name ‘packageAdmin ‘ defined in URL [jar:file:/home/pg/partygaming/lib/inhouse/pg-serviceregistry-non_osgi-extra.jar!/META-INF/spring/serviceregistry-nonosgi-bean-cont ext.xml]: Cannot register bean definition [Generic bean: class [com.partygaming.serviceregistry.packageadmin.PackageAdminImpl]; scope=; abstract=fal se; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMeth odName=null; destroyMethodName=null; defined in URL [jar:file:/home/pg/partygaming/lib/inhouse/pg-serviceregistry-non_osgi-extra.jar!/META-INF/sprin g/serviceregistry-nonosgi-bean-context.xml]] for bean ‘packageAdmin’: There is already [Generic bean: class [com.partygaming.serviceregistry.package… Read more »
Asraf
Guest
Asraf

Simple and clear

Srilatha
Guest
Srilatha

Simple and clear examples..thanks

paul
Guest
paul

You dont mention where these directories should exist. Are they inside WEB-INF/classes??

Anurag @ cisco
Guest
Anurag @ cisco

What an Example !! excellent ….. truelly awesome

papas
Guest
papas

It helps a Lot to learn.

trackback
Spring 3 JavaConfig @Import example

[…] example Published: June 20, 2011 , Updated: June 20, 2011 , Author: mkyongprintNormally, you will split a large Spring XML bean files into multiple small files, group by module or category, to make things more maintainable and […]

trackback
Spring Tutorials | Tutorials

[…] Load multiple Spring Bean configuration files Developers always categories different modules to different bean configuration files, here’s a tip to show how to load multiple Spring bean configuration files. […]