Auto-configuration in SpringBoot

SpringBoot auto-configuration attempts to automatically configure your application based on the jars that you have added.

As you know we use spring-boot-starter-parent in springboot to minimize managing the dependencies and it’s versions ourselves. For every dependency you have in your pom.xml file, springboot will try to automatically configure all beans in these dependencies. To make this work you just need to use @EnableAutoConfiguration in a class using @Configuration so that springboot knows that it needs to add all the beans required. You can also use @SpringBootApplication since it includes @EnableAutoConfiguration by default.

For example if HSQLDB is on your classpath or pom, then SpringBoot will automatically configure an in-memory database without you explicitly creating beans.

You can always override these auto-configured beans by providing your own bean definitions.

If you need to check which auto-configuration is being applied, then just use — — debug with your command.

Example :

mvn spring-boot:run --debug 

To disable a specific class from auto-configuration, use exclude property inside @EnableAutoConfiguration.

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MainClass {
}

In Spring, before SpringBoot there was no scope of auto-configuration, we had to override the methods and configure the beans ourselves.

Autoconfiguration is a very important aspect of SpringBoot since it helps developers to reduce the time and effort needed to bootstrap and get the application running so that developers can focus on the business use case.

That’s it.

You can also check my article on SpringBoot annotations explained.

Please reach out to me at LinkedIn .Or email me at vivek.sinless@gmail.com.

Happy Leaning! Cheers.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store