@Import vs @ContextConfiguration in Spring
Solution 1
@Import
and @ContextConfiguration
are for different use cases and cannot be used interchangeability.
The @Import
is only useful for importing other @Configuration
files and is only useful (and afaik) and functional on @Configuration
classes. When putting the @Import
on a test class it will be no good as it won't be processed.
@Configuration
@Import(PersistenceConfig.class)
public class MainConfig {}
Using @Import
can be useful if for instance you have disabled component scanning for @Configuration
classes or you need an @Configuration
class from a package not covered by your component-scan.
Note: There is also @ImportResource
which does the same for older XML based configuration files.
The reverse is valid for @ContextConfiguration
as that is only useful on Spring based test classes (tests ran with the SpringRunner
for jUnit 4). It is used to supply the test with the configuration parameters to make up the test configuration. It can be a collection of XML, javaconfig (or a combination thereof).
@RunWith(SpringRunner.class)
@ContextConfiguration( classes = {MainConfig.class, TestConfig.class})
public MyTest {}
It also allows to specify what to use to load those configuration (amongst others).
Solution 2
in Spring Boot @Import(SomeConfiguration.class)
adds configuration class to existing context. It is useful with test slices:
@DataJpaTest
@Import(SomeConfiguration.class)
class TestSomething(){...}
Here you have access to repositories and your beans from SomeConfiguration class.
@ContextConfiguration(classes = SomeConfiguration.class)
mean use only this configuration , which could not work with eg. DataJpaTest
.
Dmitry Senkovich
I am a full stack developer with Java/JS stack having almost 5 years of experience working in hospitality and fintech primarily with Spring, Spring Boot, Hibernate, AngularJS, Angular. Nice additions: I've got a Master's degree in Computer Science and have some practical skills in Machine Learning.
Updated on June 06, 2022Comments
-
Dmitry Senkovich almost 2 years
Is there any difference in usage of the annotations? Both the annotations allow to use multiple
@Configuration
classes to create anApplicationContext
. From their docs@ContextConfiguration
seems to be more suitable for test configuration and comes from an artifact for tests (spring-test
), however, I haven't found the actual difference.Thank you for any thoughts!
-
M. Deinum over 6 years
@ContextConfiguration
is only usable in tests in other code it doesn't do anything.@Import
is useless on a test and will only be useful on@Configuration
classes to import other configuration classes (with@ImportResource
you can import XML configuration files). -
Dmitry Senkovich over 6 years@M. Deinum hi, thanks! but what is the purpose of it then? I may use
@Import
- and this is it -
M. Deinum over 6 yearsAs stated to
@Import
(aka include, load etc) other java configuration classes. For instance those not covered by component scanning (or maybe you disabled component scanning for@Configuration
files). Or to create a separate configuration for test which@Import
s the original configuration so that you can override beans ... -
Dmitry Senkovich over 6 years@M. Deinum ok, I got the first idea. but in the second I could simply put
@Import
on test class, why to use@ContextConfiguration
here? sorry if I'm being stupid:) -
M. Deinum over 6 yearsBecause as I also already stated,
@Import
is only useful (and functional) on@Configuration
classes. It won't do anything on a test class, the only thing it does is take up a line of code. -
Dmitry Senkovich over 6 years@M. Deinum awesome, now I see. thank you very much!
-
Torsten about 5 years@Deinum - Wow thanks - I got the point it's just the other way around. But I have to disagree, using @Import(TestConfig.class) on my Test will import that Beans, if they are not defined in my AppConfig. Which is picked up by SpringBootTest. And that made me crazy - because if my AppConfig has that Bean defined, the Imported is ignored. Makes no sense. So don't do that.
-
-
Dmitry Senkovich over 6 yearsa note:
ContextCongifuration
can be used either with XML or Java configurations: > As of Spring 3.1, context loaders may choose to support either path-based or class-based resources. -
Sam Brannen over 6 yearsWell... you can actually annotate a test class with
@Import
when using Spring Boot. ;-) docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/… -
davidcyp over 4 years@SamBrannen but does it behave exactly the same?