JavaBeat

  • Home
  • 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)
  • Privacy
  • Contact Us

@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

[code lang=”java”]
package javabeat.net.basic;

public interface Car {
public void print();
}

[/code]

Toyota.java

[code lang=”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");
}
}
[/code]

Volkswagen.java

[code lang=”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");
}
}
[/code]

JavaConfigA.java

[code lang=”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();
}
}
[/code]

JavaConfigB.java

[code lang=”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();
}
}
[/code]

ParentConfig.java

[code lang=”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
}
[/code]

ContextLoader.java

[code lang=”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();
}
}
[/code]

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.

Filed Under: Spring Framework Tagged With: 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.

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.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved