• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

@import annotation in Spring

June 9, 2017 By j2eereference Leave a Comment

@Import annotation  will be useful if we have multiple spring bean configurations for different modules like Audit ,Transaction etc in a project . In such scenario it is better to import all this configurations into a single configuration class so that there wont be any confusion on creating the beans by referring multiple configuration classes. @Import annotation in spring allows for loading bean definitions from another configuration class

It is very easy to import multiple configuration classes into a single application configuration. Lets understand the usage of @Import with the below example code.

Lets create two bean classes Audit and Transaction

Java
1
2
3
4
5
6
public class Audit {
Audit(){
System.out.println("Inside Audit constructor");
}
 
}

 

Java
1
2
3
4
5
public class Transaction {
public Transaction() {
System.out.println("Inside Transaction constructor"); }
 
}

Below class is to configure Audit related beans, There could be multiple beans . In  our example lets consider only one bean called audit.

Java
1
2
3
4
5
6
7
8
9
10
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class AuditConfig {
  @Bean(name="audit")
    public Audit audit() {
        return new Audit();
    }
}

The same way lets create another configuration class for configuring Transaction related beans.

Java
1
2
3
4
5
6
7
8
9
10
11
12
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class TransactionConfig {
 
    @Bean(name="transaction")
    public Transaction transaction() {
        return new Transaction();
    }
 
}

As of now we have two separate configuration files for Audit and Transaction . It will be difficult to manage if we are not consolidating all the different configuration classes into one. Here comes the use of @Import annotation .

In the below class ApplicationConfig we will import both Audit and Transaction configurations by using @Import annotation.

Java
1
2
3
4
5
6
7
8
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
 
@Configuration
@Import({AuditConfig.class,TransactionConfig.class})
public class ApplicationConfig {
 
}

Below program is to test the import configuration.

Java
1
2
3
4
5
6
7
8
9
10
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
public class ImportAnnotationDemo {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class);
Audit auditBean =(Audit) ctx.getBean("audit");
Transaction transactionBean =(Transaction) ctx.getBean("transaction");
}
}

Output :

Jun 06, 2017 4:19:56 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2c634b: startup date [Tue Jun 06 16:19:56 IST 2017]; root of context hierarchy
Inside Audit constructor
Inside Transaction constructor

Here you can notice that

  • We are creating single application context by providing the ApplicationConfig.class .
  • All the beans defined in Audit and transaction configurations classes are available with ApplicationConfig class.

Related Posts

  • Spring dependency injection with Java configuration
  • What is Spring Boot?
  • Configuring Spring Lifecycle Callbacks with java
  • Configuring Spring Bean Scope with java
  • Spring configuration using java
  • @Required annotation in spring
  • Event handling in Spring
  • Using MessageSource in Spring
  • Component annotation in spring
  • Spring JSR-250 Annotations

Filed Under: Spring

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.