Spring @Import
annotation provides functionality similar to <import/>
element in spring XML. Using @Import
annotation you can import one or more @Configuration
classes. It can also import classes containing at least one @Bean
method.
Declared Beans using @Configuration and @Bean
AppConfig
definitions is a @Configuration
annoated class containing bean definitions defined using @Bean
.
AppConfig:
package com.javarticles.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public BeanA beanA() { return new BeanA(); } @Bean public InnerBean innerBean() { return new InnerBean(); } static class InnerBean { } }
Here is a class that is not annotated with @Configuration
but still can be imported as it contains @Bean
methods.
BeanHolder:
package com.javarticles.spring; import org.springframework.context.annotation.Bean; public class BeanHolder { @Bean public BeanB beanB() { return new BeanB(); } @Bean public BeanC beanC() { return new BeanC(); } }
BeanA:
package com.javarticles.spring; public class BeanA { }
BeanB:
package com.javarticles.spring; public class BeanB { }
BeanC:
package com.javarticles.spring; public class BeanC { }
Import Beans using @Import
We use @Import
annotation and pass in the classes to be imported, AppConfig
and BeanHolder
. It not only import the classes passed in but also any beans declared using @Bean
methods.
Bean definitions can be accessed using the ApplicationContext
or by auto-wiring using @Autowired
annotation.
ImportAnnotationExample:
package com.javarticles.spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import({AppConfig.class, BeanHolder.class}) public class ImportAnnotationExample { @Autowired private AppConfig appConfig; @Autowired private BeanB beanB; public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( ImportAnnotationExample.class); printBean(context, "com.javarticles.spring.AppConfig"); printBean(context, "beanA"); printBean(context, "beanB"); printBean(context, "beanC"); printBean(context, "innerBean"); ImportAnnotationExample importAnnotationExample = (ImportAnnotationExample) context.getBean("importAnnotationExample"); System.out.println("AppConfig member: " + importAnnotationExample.getAppConfig()); System.out.println("BeanB member: " + importAnnotationExample.getBeanB()); } public static void printBean(ApplicationContext context, String beanId) { System.out.println(beanId + ": " + context.getBean(beanId)); } public AppConfig getAppConfig() { return appConfig; } public BeanB getBeanB() { return beanB; } }
Output:
Feb 10, 2016 10:22:25 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.spring[email protected]7c3df479: startup date [Wed Feb 10 10:22:25 IST 2016]; root of context hierarchy com.javarticles.spring.AppConfig: co[email protected]2a40cd94 beanA: [email protected] beanB: [email protected] beanC: [email protected] innerBean: [email protected] AppConfig member: co[email protected]2a40cd94 BeanB member: [email protected]
Download the source code
This was an example about spring @Import annotation.