Home > Software design >  What's the difference between Spring Boot's @Configuration and @AutoConfiguration?
What's the difference between Spring Boot's @Configuration and @AutoConfiguration?

Time:10-19

And most important, what's the reason behind the "Auto" prefix? Classes annotated with @Configuration rather than @AutoConfiguration are less automatic or something?

CodePudding user response:

@Configuration is a spring framework annotation and not strictly bound to spring-boot. It was introduced when spring started to allow programmatic creation of spring-beans as to move forward from xml definitions of beans.

@AutoConfiguration is a spring-boot specific annotation not commonly available in spring framework. The reason it exists, is for external providers that cooperate with spring-boot to be able to mark some classes in some libraries they provide with this annotation as to inform spring-boot that those classes could be parsed and make some initializations during start up of spring application automatically.

So if some regular programmer that develops some application happens to have kafka in dependencies then some beans will automatically be created and added in application context and will be ready for the programmer to use, although he has not defined any configuration for them. Spring-boot already knows this as the kafka provider has already informed by marking some class in the jar they provide with the annotation @AutoConfiguration.

For this reason @AutoConfiguration has some more powerful configurations available as before, after, beforeName, afterName as to allow the provider to specify when the configuration is applied during application startup if some order is necessary.

So this annotation is not to be used from some regular programmer that develops an application using spring-boot. It is for someone that develops a library that other users might use with spring-boot. One such example is kafka library.

For this to work the spring-boot needs @EnableAutoConfiguration.

From spring documentation

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 Spring Boot auto-configures an in-memory database.

@Configuration instead is to be used from some regular programmer that develops an application using spring-boot or spring-framework as to inform the framework for which beans should be created and how.

CodePudding user response:

@AutoConfiguration was introduced in 2.7 with the idea to mark all auto-configurations with its dedicated annotation and move away from spring.factories for auto-configuration imports in 3.0 as described in Github issue.

According to Spring documentation:

[@AutoConfiguration] indicates that a class provides configuration that can be automatically applied by Spring Boot. Auto-configuration classes are regular @Configuration with the exception that Configuration#proxyBeanMethods() proxyBeanMethods is always false.

Usually, @AutoConfiguration classes automatically configure an application based on the dependencies that are present on the classpath. Those classes are generally marked as @ConditionalOnClass and @ConditionalOnMissingBean annotations that detect the presence or absence of specific classes.

Additionally, if a configuration needs to be applied in a specific order, you can use the before, beforeName, after, and afterName attributes on the @AutoConfiguration, unlike @Configuration which doesn't provide those attributes.

  • Related