Spring @ImportResource Annotation Example

0

In this article “Spring @ImportResource Annotation Example”, we will show you how to make use of spring @ImportResource annotation to import one or more XML context files. @ImportResource provides similar functionality as that of <import/> an element. Based on the file extension the appropriate reader will be used to process the context files. For example, if it is a groovy file, a groovy reader GroovyBeanDefinitionReader will be used. If the context is an xml, an xml reader XmlBeanDefinitionReader will be used.

We will also see how one can provide their own custom reader in @ImportResource annotation’s reader attribute.

Let’s first define few beans and declare their bean definitions in XML context. @importresource

BeanA:

package com.javarticles.spring;

public class BeanA {
}

BeanB:

package com.javarticles.spring;

public class BeanB {
}

BeanC:

package com.javarticles.spring;

public class BeanC {
}
  1. BeanA is defined in context1.xml
  2. BeanB is defined in com/javarticles/spring
  3. BeanC will be defined a properties file

context1.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="beanA" class="com.javarticles.spring.BeanA" />

</beans>

context2.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.javarticles.spring.Beanb" />

</beans>

We define BeanC in a properties file.

context.properties:

beanC.(class)=com.javarticles.spring.BeanC

In the below example, we import the context files using @ImportResource. We need to pass in the location path of the files. In order to access the beans, we auto-wire them to the class members.

@ImportResource({ "context1.xml", "com/javarticles/spring/context2.xml" })

We can also load beans defined in properties file using an explicit reader. For example, in the below configuration bean we pass in the classpath:context.properties and use reader set to PropertiesBeanDefinitionReader.class.

    @Configuration
    @ImportResource(value="classpath:context.properties", reader=PropertiesBeanDefinitionReader.class)
    static class Config {
        
    }

Here is the complete example of @ImportResource.

ImportResourceAnnotationExample:

package com.javarticles.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource({ "context1.xml", "com/javarticles/spring/context2.xml" })
public class ImportResourceAnnotationExample {
    @Autowired
    private BeanA beanA;

    @Autowired
    private BeanB beanB;

    @Autowired
    private BeanC beanC;
    
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
                ImportResourceAnnotationExample.class);
        try {
            ImportResourceAnnotationExample importResourceAnnotationExample = (ImportResourceAnnotationExample) context
                    .getBean("importResourceAnnotationExample");
            System.out.println("BeanA member: "
                    + importResourceAnnotationExample.getBeanA());
            System.out.println("BeanB member: "
                    + importResourceAnnotationExample.getBeanB());            
            System.out.println("BeanC member: "
                    + importResourceAnnotationExample.getBeanC());
        } finally {
            context.close();
        }
    }

    public BeanA getBeanA() {
        return beanA;
    }

    public BeanB getBeanB() {
        return beanB;
    }
    
    public BeanC getBeanC() {
        return beanC;
    }
    
    @Configuration
    @ImportResource(value="classpath:context.properties", reader=PropertiesBeanDefinitionReader.class)
    static class Config {
        
    }
}

Output:

INFO: Loading XML bean definitions from class path resource [context1.xml]
Feb 17, 2016 11:11:38 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/javarticles/spring/context2.xml]
BeanA member: [email protected]
BeanB member: [email protected]
BeanC member: [email protected]
Feb 17, 2016 11:11:38 AM org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
INFO: Closing org.spring[email protected]7c3df479: startup date [Wed Feb 17 11:11:38 IST 2016]; root of context hierarchy

Download the source code “@importresource”

This was an example about spring @ImportResource annotation.

You can download the source code here: springImportResourceAnnotationExample.zip
Share.

Comments are closed.