Spring 4 - @Import and @ImportResource example

Spring 4 - @Import and @ImportResource example

Technologies used:   JDK 1.8.0_121 | Spring 4.3.5.RELEASE | Maven 3.3.9 | Eclipse Mars.2 (4.5.2)

The @Import annotation is used to import one or more @Configuration classes. This annotation provides the functionality equivalent to <import/> element in xml based configuration. 

The @ImportResource annotation is used to import one or more XML configuration files.

Let’s see how the @Import and @ImportResource annotations work in spring application.

 

Create three beans BeanA, BeanB and BeanC as follows.

BeanA.java

package com.boraji.tutorial.spring.bean;

public class BeanA {
   public void doSomething(){
      System.out.println("Inside doSomething() method of BeanA");
   }
}

BeanB.java

package com.boraji.tutorial.spring.bean;

public class BeanB {
   public void doSomething(){
      System.out.println("Inside doSomething() method of BeanB");
   }
}

BeanC.java

package com.boraji.tutorial.spring.bean;

public class BeanC {
   public void doSomething(){
      System.out.println("Inside doSomething() method of BeanC");
   }
}

 

Create two @Configuration classes and an XML configuration file as follows.

Import bean definitions from ConfigA class and configB.xml file to ConfigC class using the @Import and @ImportResource annotation respectively.

ConfigA.java

package com.boraji.tutorial.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.boraji.tutorial.spring.bean.BeanA;

/**
 * @author imssbora
 */
@Configuration
public class ConfigA {
   @Bean
   public BeanA getBeanA(){
      return new BeanA();
   }
}

configB.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="beanB" class="com.boraji.tutorial.spring.bean.BeanB"/>
</beans>

ConfigC.java

package com.boraji.tutorial.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

import com.boraji.tutorial.spring.bean.BeanC;

/**
 * @author imssbora
 */
@Configuration
@Import(value={ConfigA.class})
@ImportResource(locations={
      "classpath:/com/boraji/tutorial/spring/config/configB.xml"
     })
public class ConfigC {
   @Bean
   public BeanC getBeanC() {
      return new BeanC();
   }
}

 

Create a main class to test the above programs.

MainApp.java

package com.boraji.tutorial.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.boraji.tutorial.spring.bean.BeanA;
import com.boraji.tutorial.spring.bean.BeanB;
import com.boraji.tutorial.spring.bean.BeanC;
import com.boraji.tutorial.spring.config.ConfigC;

/**
 * @author imssbora
 */
public class MainApp {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = 
            new AnnotationConfigApplicationContext(ConfigC.class);

      BeanA beanA=context.getBean(BeanA.class);
      beanA.doSomething();
      
      BeanB beanB=context.getBean(BeanB.class);
      beanB.doSomething();
      
      BeanC beanC=context.getBean(BeanC.class);
      beanC.doSomething();
      
      context.close();
   }
}

Output

Inside doSomething() method of BeanA
Inside doSomething() method of BeanB
Inside doSomething() method of BeanC

 

You can download the complete example from the below download link.

 

Download Source: