Mastering Spring’s @Import: Three Powerful Ways to Load Configurations
This guide explains Spring’s @Import annotation, covering three import strategies—direct class array, ImportSelector, and ImportBeanDefinitionRegistrar—through detailed code examples, usage scenarios, and important version notes, helping developers improve configuration management, code maintainability, and readability in backend Java applications.
1. Introduction
In the Spring framework, the @Import annotation allows developers to import configuration classes, dependencies, or custom configurations into the Spring container, improving maintainability and readability.
2. Use Cases of @Import
Import configuration classes that define beans.
Import dependencies by importing a class into another.
Import custom configurations using @ImportResource .
3. Examples
Directly importing a regular class array
<code>static class B {}
static class A {
@Bean
public B b() {
return new B();
}
}
@Import({A.class})
@Configuration
static class ImportConfig {}
</code>Both A and B are managed by the Spring IoC container. Multiple classes can be imported. Note: before Spring 4.2 the imported class had to be annotated with @Configuration ; from 4.2 onward a plain class is sufficient.
Importing via ImportSelector
<code>static class B {}
static class A {}
@Import({C.class})
@Configuration
static class ImportConfig {}
static class C implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[] {"com.pack.main.import_class.Import_02.B", "com.pack.main.import_class.Import_02.A"};
}
}
</code>The returned strings must be fully qualified class names. The ImportSelector itself is not registered as a bean. An empty array is allowed, but null is not.
Importing via ImportBeanDefinitionRegistrar
<code>static class A {
private String name;
public void setName(String name) { this.name = name; }
@Override
public String toString() { return "A [name=" + this.name + "]"; }
}
static class B implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) {
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(A.class);
beanDefinition.getPropertyValues().addPropertyValue("name", "张三");
registry.registerBeanDefinition("a", beanDefinition);
}
}
@Import({B.class})
@Configuration
static class ImportConfig {}
static class D {
@Resource private A a;
@Override public String toString() { return "D [a=" + a + "]"; }
}
</code>The ImportBeanDefinitionRegistrar implementation itself is not registered as a bean.
Conclusion: The @Import annotation offers flexible ways to load configuration classes, dependencies, and custom beans, helping developers organize and manage application configuration more effectively.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.