Issue
And most important, what's the reason behind the "Auto" prefix? Classes annotated with @Configuration rather than @AutoConfiguration are less automatic or something?
Solution
@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 in a spring-boot
project @EnableAutoConfiguration
is needed as well, to enable auto configuration.
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.
Answered By - Panagiotis Bougioukos
Answer Checked By - Mary Flores (JavaFixing Volunteer)