This Spring tutorial guides you how to configure Spring to use multiple XML beans configuration files.

For large and complex Spring-based applications, it’s very common to break Spring’s application context file into multiple files in order to modularize the application’s configuration (beans definitions) into smaller pieces for easy maintenance. Spring allows this by providing the <import> element. Here’s a quick example:

<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">

	<import resource="daoBeans.xml" />
	<import resource="businessBeans.xml" />
	<import resource="servicesBeans.xml" />

</beans>
This Spring’s application context configuration file includes three resources: daoBeans.xml, businessBeans.xml and servicesBeans.xml with each must be a complete, fully valid XML Spring’s beans definition file (including the root element <beans> and its xmlns attributes). Following are example code of each imported file above:

daoBeans.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-3.0.xsd">
	
	<bean id="myDaoBean" class="net.codejava.spring.MyDao" />
	
</beans>
 

businessBeans.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-3.0.xsd">
	
	<bean id="myBusinessBean" class="net.codejava.spring.MyBusiness">
		<property name="daoBean" ref="myDaoBean" />
	</bean>
	
</beans>
 

servicesBeans.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-3.0.xsd">

	<bean id="myServicesBean" class="net.codejava.spring.MyServices">
		<property name="businessBean" ref="myBusinessBean" />
	</bean>

</beans>
Spring container will load all the imported files and merge them to produce a final application configuration, so beans in one file can refer to other beans declared in other files. For example, in the above example, the myServiceBean (declared in servicesBeans.xml file) refers to the myBusinessBean which is declared in the businessBeans.xml file, and the myBusinessBean refers to the bean myDaoBean which is declared in the daoBeans.xml file. The imported files are considered to be relative to the parent file. The following screenshot shows how these XML files are place in an Eclipse project:



SpringMultipleConfigs eclipse project

Of course the imported file can import another file also, as shown in the following modified version of servicesBeans.xml file:

<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-3.0.xsd">
	
	<bean id="myServicesBean" class="net.codejava.spring.MyServices">
		<property name="businessBean" ref="myBusinessBean" />
		<property name="productServiceBean" ref="productServicesBean" />
	</bean>
	
	<import resource="productServices.xml" />
		
</beans>
And the Spring container will throw a BeanDefinitionStoreException if it could not find a specified imported file.

You can download a sample Spring MVC application (in Eclipse project) that demonstrates using multiple beans configuration files, in the attachments section below.

 

Related Spring Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (SpringMultipleConfigs.zip)SpringMultipleConfigs.zip[Eclipse project]3447 kB

Add comment

   


Comments 

#1Miguel2019-12-13 09:17
just by defining the beans in the .xml config files we do not load them into the spring IOC container correct? How do we actually register the beans? can these .xml files be auto scanned if we use @SpringBootApplication? or do we also have to import the parent file into some @Configuration file? i think those are registered by component scanning
Quote