Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the EnableAutoConfiguration spring annotation work?

Tags:

I am no fan of gross over abstractions, And i think Spring has committed a major felony.

But I'm willing to overlook it this time if someone can explain the algorithm behind the 'auto' configuration.

Having a look at spring's own javadocs, It doesn't give much away other than saying that it will intelligently guess what you need and something to do about conditional beans.

Does someone know what algorithm is used to determine what needs to be loaded?

like image 639
coderatchet Avatar asked Jun 22 '14 13:06

coderatchet


People also ask

What is difference between @configuration and EnableAutoConfiguration?

SpringBootApplication combines of 3 annotations: @Configuration, used for Java-based configuration on Spring framework, @ComponentScan to enable component scanning of components, and @EnableAutoConfiguration itself, which is used to allow for auto-configuration in Spring Boot application.

Can we use both @SpringBootApplication and @EnableAutoConfiguration?

A single @SpringBootApplication annotation can be used to enable those three features, that is: @EnableAutoConfiguration : enable Spring Boot's auto-configuration mechanism.

How does spring boot auto-configuration magic work?

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.

What exactly @SpringBootApplication annotation does?

Spring Boot @SpringBootApplication annotation is used to mark a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. It's same as declaring a class with @Configuration, @EnableAutoConfiguration and @ComponentScan annotations.


1 Answers

In my experience as a Spring Boot user the basic factors for Spring Boot to decide on what auto-configurations will be enabled are:

1) The classes present on the classpath. For example if RabbitMQ and Spring AMQP classes are present, then the RabbitAutoConfiguration will be enabled. The corresponding annotation is @ConditionalOnClass,

2) The presence or not of user defined beans. For example, if all the Spring Data JPA is present on the classpath, Spring Boot will register a LocalContainerEntityManagerFactoryBean bean only if the user has not already done so. The beans registered by the user will 'override' the default ones. The relevant annotation is @ConditionalOnMissingBean

As @DaveSyer mentions, you can of course use Spring Boot without @EnableAutoConfiguration if you want to include the relevant configuration on your own. Or you could use the less drastic solution of the exclude field of @EnableAutoConfiguration. If for example you want Spring Boot to autoconfigure everything except ActiveMQ, you would use @EnableAutoConfiguration(exclude=ActiveMQAutoConfiguration.class)

In my opinion, there is absolutely no felony here! You can use what you want from Spring Boot. When you don't want something it has to offer, you can easily opt out partially or completely!

Also if you want to get a look under the covers, you can add the property

logging.level.org.springframework.boot=DEBUG

to application.properties and Spring Boot will gladly give a detailed report of what was auto-configured and what wasn't

like image 106
geoand Avatar answered Oct 25 '22 11:10

geoand