View video tutorial

SPRING BOOT Auto Configuration

SPRING BOOT

Spring Boot auto-configuration is a process of configuring various settings in a Spring Boot application by examining the classpath and annotations.

Spring Boot Auto Configuration


➔ Spring Boot for application development became popular among developers because it reduced the configuration burden.

➔ Before Spring Boot came along, developers had to spend their time doing tedious application configuration.

➔ Most of the configuration happens automatically, developers only need to use some annotations to make each configuration work properly in the application.

➔ Moreover, Spring Boot autoconfiguration is non-invasive, so many configurations can be customized or disabled if necessary.

➔ So if developers want to customize autoconfiguration (although most of the time it's not necessary), they need to understand how autoconfiguration works.

Key points of auto configuration.


➔ Spring Boot uses the build tool to create projects.

Maven: is a popular build automation tool that is commonly used in most Java projects.

➔ In Spring Boot, all the dependencies of the project are specified in the pom.xml file and Maven downloads and resolves all the dependencies and includes them in the classpath.

Gradle: is also a popular build automation tool.

➔ In the case of Gradle, all dependencies are specified in the build.gradle file, then Gradle resolves the dependencies and includes them in the classpath.

➔ The classpath is a predefined location for the JVM where any libraries or user-defined classes are searched for when compiling or running a Java application.

Spring Boot Starters:


➔ Spring Boot starters are configurable dependency descriptors that simplify build configurations by bundling libraries required by a project for specific functionality.

➔ A starter like spring-boot-starter-parent can provide many libraries to the project by default.

➔ All libraries are then resolved by the build management tool and included in the classpath.

➔ The Spring Boot AutoConfiguration feature uses this library from the classpath and automatically registers dependencies and pre-configures application settings.

➔ This is where the core and important part of Spring Boot's autoconfiguration happens.

➔ For example, when we add spring-boot-starter-web dependency in pom.xml, it will resolve core dependencies related to web application such as Spring MVC, embedded Tomcat server, etc.

➔ Spring Boot AutoConfigure feature searches the classpath and resister DispatcherServlet class, starts Tomcat server on port 8080, and does many other settings and configurations for the application.

How Auto configuration works:

Spring Boot auto-configuration logic is implemented by classes packaged in the spring-boot-autoconfigure.jar file, which is present in external libraries.

This jar file does all the following necessary things to make the automatic configuration work.

Application Initialization:


➔ When a Spring Boot application is started, Spring Boot uses the class annotated with @SpringBootApplication as the main entry point.

➔ This annotation is combined with several other annotations such as @Configuration, @EnableAutoConfiguration, and @ComponentScan.

➔ So the @SpringBootApplication annotation does all the basic initialization work like enabling automatic configuration, enabling component scan and component configuration etc.

Creating application context


➔ An application context is created by Spring Boot which will be used as a container for beans and other dependencies.

➔ The application context will be initialized and registered by the dependencies created by scanning the classpath for libraries, components, configurations, and auto-configuration.

➔ Application context manages the lifecycle of all dependencies like beans, services, repositories, etc.

Apply Auto Configuration


➔ Spring Boot performs automatic configuration for beans and components found in the classpath or user-defined annotated components.

➔ Spring Boot uses conditional annotations such as @ConditionalOnClass, @ConditionalOnMissingClass, @ConditionalOnBean, @ConditionalOnMissingBean, @ConditionalOnProperty, @ConditionalOnBooleanProperty, @ConditionalOnResource, @ConditionalOnWebApplication, @ConditionalOnNotWebApplication, ConditionalOnWarDeployment, @ConditionalOnNotWarDeployment, @ConditionalOnExpression, etc. to conditionally configure beans when certain conditions are met.

Customize or override configuration


➔ Spring Boot auto configuration is often overridden or customized by external resources such as application.property files, yaml files, environment variables, and command line arguments.

Start the embedded web server.


➔ If the application is a Spring MVC web application, Spring Boot starts the embedded Tomcat web server (or Jetty, Undertow) based on the application's dependencies and default or custom configuration.

Start Spring Boot Application


➔ Spring Boot uses lifecycle callback methods like @PostConstruct and initializes all the beans and components of the ApplicationContext.

➔ After initialization and instantiation, dependencies are injected and application initialization is completed.

➔ Lazy loading components are injected at runtime.

Serve HTTP endpoint requests


➔ Once all initialization processes are completed, the application context is ready to process requests.

➔ The embedded web server is up and running to serve incoming HTTP endpoints.