• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

@Import Annotation in Spring Framework

October 28, 2013 //  by Krishna Srinivasan//  Leave a Comment

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,
javaconfig spring import

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.

Category: Spring FrameworkTag: Spring Annotations

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.

Previous Post: «New Features in Spring Boot 1.4 @Configuration Annotation in Spring 3.0
Next Post: How to use Spring EL in XML configurations? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact