Using @Import for Modular Spring Boot Development
Spring Boot enables modular backend development by placing each feature in its own Maven module and using @Import (or custom annotations, ImportSelector, ImportBeanDefinitionRegistrar, and @ConditionalOnProperty) to load configuration classes, scan components, and conditionally register beans while keeping the application a single monolithic program.
When developing Spring Boot backend applications, a four‑layer architecture works for simple monoliths, but as features grow the project becomes bulky, with many xxxDAO and xxxService classes.
To keep a single‑module project manageable, we can split functionality into separate Maven modules and import them into the main module. This does not create a distributed micro‑service system; the application remains a single Spring Boot program, but each feature lives in its own module.
Spring Boot supports modularization via the @Import annotation. By placing a configuration class in a feature module and importing it in the main module, all beans defined in that module become part of the IoC container.
Typical steps:
Define a feature module with a class annotated with @ComponentScan (or @Configuration ) that serves as the scan entry point.
Write service classes in the feature module, e.g.:
package com.gitee.swsk33.functionone.service;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class FunctionOneService {
@PostConstruct
private void init() {
log.info("功能1,启动!");
}
}In the main module, add the feature module as a Maven dependency and create a configuration class that imports the entry point:
package com.gitee.swsk33.mainmodule.config;
import com.gitee.swsk33.functionone.FunctionOneApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(FunctionOneApplication.class)
public class FunctionImportConfig {
}When the application starts, Spring reads @Import , loads FunctionOneApplication , and scans its package, registering FunctionOneService as a bean.
Custom annotations can wrap @Import for a cleaner API, e.g. @EnableFunctionOne :
package com.gitee.swsk33.mainmodule.annotation;
import com.gitee.swsk33.functionone.FunctionOneApplication;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(FunctionOneApplication.class)
public @interface EnableFunctionOne {
}Advanced import mechanisms include implementing ImportSelector or ImportBeanDefinitionRegistrar to decide imports programmatically or register beans manually.
Example ImportSelector implementation:
package com.gitee.swsk33.mainmodule.selector;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public class DemoImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
System.out.println("被标注@Import的类名:" + importingClassMetadata.getClassName());
return new String[] {
"com.gitee.swsk33.functionone.FunctionOneApplication",
"com.gitee.swsk33.functiontwo.FunctionTwoApplication"
};
}
}And its usage:
package com.gitee.swsk33.mainmodule.config;
import com.gitee.swsk33.mainmodule.selector.DemoImportSelector;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(DemoImportSelector.class)
public class FunctionImportConfig {
}Similarly, ImportBeanDefinitionRegistrar can register bean definitions directly.
Conditional loading can be added with Spring Boot’s @ConditionalOnProperty so that a feature module is imported only when a configuration flag is true.
In summary, @Import together with @ComponentScan , custom annotations, and conditional mechanisms provides a powerful way to build modular Spring Boot back‑end applications.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
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.