user13314823
user13314823

Reputation:

What's the difference between Spring Boot's @Configuration and @AutoConfiguration?

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

Upvotes: 9

Views: 15546

Answers (3)

Anil Nivargi
Anil Nivargi

Reputation: 1717

@AutoConfiguration

Spring boot main application we can use @SpringBootApplication, this annotation internally uses three annotations like @ComponentScan, @Configuration and @EnableAutoConfiguration so third annotation i.e EnableAutoConfiguration internally uses @AutoConfiguration.

Spring Boot @AutoConfiguration attempts to automatically configure your Spring application based on the jar dependencies that you have added. In spring boot jar (spring-boot-autoconfigure jar) added many of the default configurations that are needed for the spring boot applications like spring data jpa and many others.

These default configurations enabled/disabled based on these conditions like @ConditionalOnBean or @ConditionalOnClass or @ConditionalOnMissingBean.

Let us see the below code for autoconfiguration of spring jpa,

@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TaskExecutionAutoConfiguration.class })
@ConditionalOnBean(DataSource.class)
@ConditionalOnClass(JpaRepository.class)
@ConditionalOnMissingBean({ JpaRepositoryFactoryBean.class, JpaRepositoryConfigExtension.class })
@ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "enabled", havingValue = "true",
        matchIfMissing = true)
@Import(JpaRepositoriesImportSelector.class)
public class JpaRepositoriesAutoConfiguration {

    //required all bean definations.

}

@Autoconfiguration internally uses configuration annotation with proxyBeanMethods= false,

@Configuration(proxyBeanMethods = false)

So developers directly can use jparepository no need to define required beans.

If you want to create your own framework/jar like spring boot then you need to define your own Configurations using AutoConfiguration annotation and that's part of the application start up.

@Configuration

Configuration annotation is to use to define the bean. At the class level we can use Configuration annotation and inside method at constructor or setter method need to use Bean annotation to define the bean. During the application start up configuration class execute and intialize all the defined beans.

Upvotes: 0

Toni
Toni

Reputation: 5115

@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() 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.

Upvotes: 6

Panagiotis Bougioukos
Panagiotis Bougioukos

Reputation: 18929

@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.

Upvotes: 14

Related Questions