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

	<import resource="EmployeeConfig.xml" />
	<import resource="DatabaseConfig.xml" />	
	
</beans>

AppContext configuration class that import multiple configuration:

@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