What is the difference between @ImportAutoConfiguration and @Import
1 answer
Is it true that
org.springframework.boot.autoconfigure.ImportAutoConfiguration
an improved replacement fororg.springframework.context.annotation.Import
?
No, this is not a substitute, as @ImportAutoConfiguration
is an annotation for Spring Boot, I would call it an improvement. But, while it seems like you can use them interchangeably when using Spring Boot, I wouldn't suggest it. Use them as they are intended to be used.
Would you use
@ImportAutoConfiguration
if you don't want to enable autoconfiguration by default with
@EnableAutoConfiguration
. As you probably know,
@EnableAutoConfiguration
trying to set up beans that are in your classpath, like tomcat-embedded.jar. While
@ImportAutoConfiguration
runs only the configuration classes you specified in the annotation.
This is an example of a basic Spring Boot method with @ImportAutoConfiguration
:
@ComponentScan("path.to.your.controllers")
@ImportAutoConfiguration({WebMvcAutoConfiguration.class
, DispatcherServletAutoConfiguration.class
, EmbeddedServletContainerAutoConfiguration.class
, ServerPropertiesAutoConfiguration.class
, HttpMessageConvertersAutoConfiguration.class})
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
We can say that this is an alternative to use @EnableAutoConfiguration
. And in this case, configure barebone embedded Tomcat and Spring WebMVC.
@Import
is used to import a bean config class labeled
@Configuration
which contains your custom bean config.
0
source to share