Spring Framework Series — Spring Boot

devcat
4 min readNov 10, 2023

--

Welcome to the spring framework series, In this article, we will understand what is Spring Boot and how it plays a role in simplifying application development. In the previous part of this blog post series, we went through what is spring framework, beans, configuration and how spring uses configuration metadata to create IOC container. In this article, we will learn what is Spring Boot and how it creates production grade standalone application that are configured and ready to use. Spring Boot does this with minimum effort.

Spring Boot adds some extra features to spring framework to make the application development easier. we will first look at the goals of spring boot ,then we will understand how spring boot uses auto-configuration to initialise and setup some beans.

Goals of Spring Boot:

  • Auto-Configuration and Starters
  • Spring Boot provides extra non-functional features such as embedded servers, security, metrics, health checks, externalised configuration.
  • No requirement of XML configuration or code generation.

Spring Boot auto-configuration works by automatically configuring spring application based on the jars that are specified in the classpath. How spring boot does that?. Let’s first understand how we built spring application without Spring Boot.We created the applicationContext and then we scanned the classpath to detect all the beans and finally we customized the application context to add any configuration.

But, What if there is a framework that does auto-configuration and adds extra features to build spring application. Spring Boot does this and It is part of the spring framework. In the following section, we will understand what are starters and how Spring Boot uses auto-configuration to automatically load properties from multiple places.

Starters are set of dependency descriptors that are used in pom.xml to specify the list of dependencies that will be added. If you read the pom.xml and you can find dependency that starts with spring-boot-starter-*. There are variety of starters available in spring. For example, In order to build web application It is necessary to add spring-boot-starter-web in the pom.xml, To write tests dependency spring-boot-starter-test has to be added.

  <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

Auto-Configuration

Spring Boot loads the configuration from multiple places to configure and set up beans while the application starts. The following are the list of configuration loaded in some predefined order to configure spring boot. Configuration may be stored in yaml, properties and environment variable..etc. Spring will look for properties in the below order.

Spring application that is annotated with @EnableAutoConfiguration ensures that all these properties are loaded automatically when the application starts.

1. Default Properties
2. @PropertySource annotation on your Configuration classes.
3. config data such as application.properties
4. RandomValuePropertySource that has properties only in random.*
5. OS environment variables
6. System properties()
7. Servlet Context Init
8. Servlet Config parameters
9. @TestPropertyResource annotation

ComponentScan

When spring boot application starts, It will first search for all the beans that are in the classpath and load them to register with container. We previously saw how IOC container is created and configured with ApplicationContext. Here, In spring boot we do not need to register the beans manually, Spring Boot would automatically load them using component scanning. component scan would find all the beans that are annotated at source level and register them with container..

Let’s look at simple example to understand how all these work together to create spring boot application.

For example, if we have to configure HSQLDB as datasource, Spring Boot would load and configure if it finds HSQLDB on the classpath. This is the default db setup that comes with spring boot. If we want to deviate from the default, for example, To configure MYSQL we have to add mysql dependencies in the classpath and specify configuration in application.properties. Spring will setup the mysql datasource based on the jars and configuration.

In order for all these things to work, we need to add single @SpringBootApplication annotation to the main class of application.This is equivalent of adding the following annotations such as @EnableAutoConfiguration, @ComponentScan, @SpringBootConfiguration.

  • @EnableAutoConfiguration — annotation that enables auto configuration of spring application.
  • @ComponentScan — annotation that enable component scan on where the application resides.
  • @SpringBootConfiguration — annotation that helps to register extra beans in the context.
@SpringBootApplication
public class MyApplication {

public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}

}

If you want to exclude some configuration that you don’t want to be loaded or registered with container, then you can use exclude to specify the name of the class.

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class SpringApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}

Conclusion

In this article, we have understood what is spring boot, why it is part of the spring framework and what kind of problem it solves. Then we understood how auto-configuration and starters play a role in setting up beans automatically. Then, we went through 3 annotations such as @ComponentScan, @EnableAutoConfiguration, @SpringBootConfiguration and how it is forms @SpringBootApplication. We also saw how to exclude certain configuration from SpringBoot application. The main takeaway from this article is how spring boot automatically setting up beans while @SpringBootApplication annotation is added.

--

--

No responses yet