In my previous article I have explained about the JavaConfig in Spring 3.0. It is a new way of configuring the bean definitions without using the traditional XML files. Also I have listed the annotations defined as part of JavaConfig approach. If you are beginner in spring framework, we have huge collection of spring tutorials and spring 4 tutorials for your reference.
In the list, @Import is the one such annotation used for consolidating all the configurations defined in various configuration files using @Configuration annotation. It is much similar to how we import the different XML configuration files to a single file. @Import annotation will used for the same purpose. This tutorial explains how to use @Import for Importing JavaConfig Files in Spring Projects.
If you look at the below sample code, I have created two configuration files and then imported them to the main configuration file. At the end, I need to create contaxt using only the main configuration file. If you have any questions, please write it in the comments section.
Car.java
package javabeat.net.basic; public interface Car { public void print(); }
Toyota.java
package javabeat.net.basic; import org.springframework.stereotype.Component; @Component public class Toyota implements Car{ public void print(){ System.out.println("I am Toyota"); } }
Volkswagen.java
package javabeat.net.basic; import org.springframework.stereotype.Component; @Component public class Volkswagen implements Car{ public void print(){ System.out.println("I am Volkswagen"); } }
JavaConfigA.java
package javabeat.net.basic; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class JavaConfigA { @Bean(name="volkswagen") public Car getVolkswagen(){ return new Volkswagen(); } }
JavaConfigB.java
package javabeat.net.basic; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class JavaConfigB { @Bean(name="toyota") public Car getToyota(){ return new Toyota(); } }
ParentConfig.java
package javabeat.net.basic; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import({JavaConfigA.class,JavaConfigB.class}) public class ParentConfig { //Any other bean definitions }
ContextLoader.java
package javabeat.net.basic; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ContextLoader { public static void main (String args[]){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ParentConfig.class); Car car = (Toyota)context.getBean("toyota"); car.print(); car = (Volkswagen)context.getBean("volkswagen"); car.print(); context.close(); } }
If you run the above example,the printed output will be,
In this tutorial I have explained how to use @Import annotation in your spring applications. This is very convenient way for importing multiple configuration files into a single parent configuration file instead of declaring all the configurations in a single file. I hope this example helped you. If you have any questions, please write it in the comments section.