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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering Spring’s @Import: Three Powerful Ways to Load Configurations

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

static class B {}
static class A {
    @Bean
    public B b() {
        return new B();
    }
}
@Import({A.class})
@Configuration
static class ImportConfig {}

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

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"};
    }
}

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

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 + "]"; }
}

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javabackend-developmentConfigurationspringdependency-injectionImport Annotation
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.