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
?
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.
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.
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.
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.
Is it true that
org.springframework.boot.autoconfigure.ImportAutoConfiguration
is improved replacement fororg.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.
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With