Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between @ImportAutoConfiguration and @Import

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 ?

like image 928
michaldo Avatar asked Apr 27 '17 09:04

michaldo


People also ask

What does the @import annotation do?

The @Import annotation indicates one or more @Configuration classes to import. @Bean definitions declared in imported @Configuration classes should be accessed by using @Autowired injection. Either the bean itself can be autowired, or the configuration class instance declaring the bean can be autowired.

What is the difference between @configuration and AutoConfiguration?

AutoConfiguration classes are run last (meaning after all regular non-autoconfiguration classes) while the order in which Configuration classes are run is indeterminate (except if we use ordering annotations like @Ordered ) To declare a class as an AutoConfiguration they need to be specified as such in the spring.

What does ImportAutoConfiguration do?

Annotation Type ImportAutoConfiguration Applies the same ordering rules as @EnableAutoConfiguration but restricts the auto-configuration classes to the specified set, rather than consulting ImportCandidates . Can also be used to exclude() specific auto-configuration classes such that they will never be applied.

What is spring boot Autoconfigure?

Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. For example, If HSQLDB is on your classpath, and you have not manually configured any database connection beans, then we will auto-configure an in-memory database.


1 Answers

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.
like image 79
Yoshua Nahar Avatar answered Sep 18 '22 12:09

Yoshua Nahar