Spring中@Import与@ContextConfiguration的用法差异咨询
@Import and @ContextConfiguration Great question! Even though both annotations let you use multiple @Configuration classes to create a Spring ApplicationContext, they’re designed for distinct purposes and live in different parts of the Spring ecosystem. Let’s break down their key differences:
Core Purpose & Use Cases
@Import(from spring-context)
This is a core Spring annotation meant for production and general configuration assembly. Its job is to combine multiple@Configurationclasses into a single logical configuration, letting Spring load all their beans into the container. You’ll use it in your application’s main configuration code to build out your core bean ecosystem, though it works in tests too—it’s not limited to any environment.@ContextConfiguration(from spring-test)
This is a test-only annotation, built exclusively for setting up test environments. Its sole purpose is to tell Spring’s testing framework exactly which configurations (classes or XML files) to use when creating theApplicationContextfor your tests. It’s never used in production code; it’s all about making test contexts predictable and tailored to your test needs.
Usage Patterns
@Importusage
You annotate a@Configurationclass with@Importto pull in other configuration classes as part of its definition:@Configuration @Import({PaymentConfig.class, InventoryConfig.class}) public class AppMainConfig { // Additional bean definitions go here }When Spring loads
AppMainConfig, it automatically includes all beans fromPaymentConfigandInventoryConfig.@ContextConfigurationusage
You annotate your test class directly to define the context for testing. It even supports loading XML configuration files (via thelocationsattribute), something@Importcan’t do:@SpringJUnitConfig @ContextConfiguration(classes = {TestPaymentConfig.class, TestInventoryConfig.class}) public class PaymentServiceIntegrationTest { // Test methods go here }
Extended Capabilities
@Importflexibility
Beyond importing@Configurationclasses,@Importsupports advanced use cases like importingImportSelectororImportBeanDefinitionRegistrarimplementations. This lets you dynamically register beans based on environment variables, profiles, or other runtime conditions—super useful for flexible production configurations.@ContextConfigurationtest-specific features
It’s optimized for testing: you can specify a customcontextLoaderto tweak how the test context is loaded, pair it with@TestPropertySourceto load test-specific properties, or useinheritLocations/inheritInitializersto control whether test contexts inherit settings from parent classes.
Dependency Requirements
@Importis part of the corespring-contextmodule—you’ll have it available as long as you’re using Spring’s core framework, no extra dependencies needed.@ContextConfigurationrequires thespring-testmodule, which you’d only include in your test scope (e.g., in Maven/Gradle test dependencies).
In short: @Import is your go-to for assembling application configurations, while @ContextConfiguration is a test-specific tool for spinning up controlled, isolated contexts for your tests. They overlap in loading multiple config classes, but their design goals are worlds apart.
内容的提问来源于stack exchange,提问作者Dmitry Senkovich

