Use @Import for Importing JavaConfig Files in Spring Projects

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. In the list, @Import is the one such annotation used for consolidating all the configurations. 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,
javaconfig spring import

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*