What is difference between @ImportAutoConfiguration and @Import

廉价感情. 提交于 2020-01-23 05:21:08

问题


Is it true that org.springframework.boot.autoconfigure.ImportAutoConfiguration is improved replacement for org.springframework.context.annotation.Import because does the same and additionally respects

@AutoConfigureBefore, @AutoConfigureAfter and @AutoConfigureOrder ?


回答1:


Is it true that org.springframework.boot.autoconfigure.ImportAutoConfiguration is improved replacement for org.springframework.context.annotation.Import?

No it is not a replacement since @ImportAutoConfiguration is a Spring Boot specific annotation, I might call it an enhancement. But eventhough it seems that you can use them interchangeably when using Spring Boot, I wouldn't suggest it. Use them as they were intended to be used.


You would use @ImportAutoConfiguration when you don't want to enable the default autoconfiguration with @EnableAutoConfiguration. As you probably know, @EnableAutoConfiguration attemps to configure beans that are located on your classpath eg tomcat-embedded.jar. Whereas @ImportAutoConfiguration only runs the configuration classes that you provided in the annotation.

This is an example of an Spring Boot application main 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);
  }
}

You might say that it is an alternative to using @EnableAutoConfiguration. And in this case to configure barebone embedded Tomcat and Spring WebMVC.


@Import is used to import a bean configuration class marked with @Configuration which contains your custom bean configurations.

来源:https://stackoverflow.com/questions/43653655/what-is-difference-between-importautoconfiguration-and-import

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!