Spring 3 Load Multiple Configuration using @Import

This example show you how to configure multiple Java-based configuration files with @Import. The @Import annotation allows for loading @Bean definitions from another configuration class.

XML with bean definitions:

<?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="EmployeeConfig.xml" />
	<import resource="DatabaseConfig.xml" />	

AppContext configuration class that import multiple configuration:

@Import ({EmployeeConfig.class,DatabaseConfig.class})
public class AppConfig {


You can load single file like this:

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

You can download the source code of this example here.


No comments yet.

Leave a Reply