# Quick Notes on Spring Boot

Recently I started reviewing Spring Boot. Even though I have been working on it for some time, my practical and theoretical knowledge were note aligned. And that was making me struggle during job interviews and exams. Hence I started typing some notes here and there.

This is just an edited version of my note. I want to use it as a quick reference and hope someone would use it too.

## So, what is Spring Boot?

Spring Boot is the result of the spring framework adopting itself to quick bootstrapping techniques.

*Spring Boot = Spring + Bootstrapping*

Here is the official definition.

> Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can "just run".

## But what is Spring

It is an enterprise Java-based application framework, with its core:-

- **Dependency injection, events, resources, i18n, validation, data binding, type conversion, SpEL, AOP.**

And support for:-

- Programming and configuration model,
- Testing: mocking, test 
- Infrastructure support at application level,
- Data access: transactional data, DAO, ORM, etc
- Integration: JMS, JMS, Scheduling, etc
- Language: Kotlin, Groovy, 

#### Spring Drawbacks

- It is a huge framework. Cost,
- Bloat,
- Lack of clear start point, 
- Multiple build and deploy steps.

#### Spring Boot

Abstraction of Spring with a common approach. Hence it is:-

- Opinionated,
- Conventional,
- Stand alone (Doesn't need servlet container)
- Production-ready

### Maven = Managing dependecies.

Maven is a build and dependency management tool that allows us to define all our dependencies in a single file.

#### Spring Boot with Maven

**pom.xml** file is used to configure a Spring Boot Application. In Spring Boot the file has a parent section with `org.springframework.boot` group.

````
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.5.2</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>
````
This parent section is an important section that pulls all the conventional JARs (dependencies) that Spring Boot assumes we need.

We can add other dependencies using the `dependencies` section.

We also have another convenience dependency `spring-boot-starter-web` in order to build web applications. 

### Spring Boot Main Class.

The main class & method is the starting point as Spring boot is a stand-alone app. Its main method is annotated with `@SpringBootApplication` annotation. This main method must call the `run` method of `SpringApplication` class, upon which the following is performed:-

- Sets up default configuration.
- Starts the Spring application context.
- Performs classpath scan.
- Starts Tomcat server.

## Spring Controller.

Once we bootstrapped our application and are able to run it, we have to receive requests coming from a user. Requests from users are handled by controllers.

Controllers are a simple java class annotated by `@controller` annotation or `@RestController` to annotate it as a REST controller. A controller has methods to accept requests coming from users. The request from a user can be a `GET`, or `POST` request. Or in a conventional RESTful API, the request would be like:-

  - **GET**         To request and access resources.
  - **POST**       To persist resource.
  - **PUT**         To update a persisted resource.
  - **DELETE**   To remove a persisted resource.




