Spring Boot is a framework to simplify the bootstrapping and development of new Spring application. This result in rapid application development. Spring boot allows developers to add out-of-box functionality in an opinionated manner. Spring boot is perfect tool for building cloud based micro-services as well as creating RESTful webservice.
Basic configuration required for Spring Boot application.
1. POM
Adding out-of-box functionality in an opinionated manner is enabled through POM entries.
1.1. The first things we have to ensure is that we add starter-parent. This ensure that project get sensible default, filtering and plugin configuration.
1.2. We add dependency for any of the out-of-box functionality we intend to use. For example add JPA dependency if we intend to use JPA etc
2.Application class.
This the the entry point for processing of a Spring Boot application.
Note:
A. The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes
B. SpringApplication class creates ApplicationContext instance, register ommandLinePropertySource to expose command line argument as Spring properties, loading all singletons bean and trigger CommandLineRunner bean.
C. run() is a static method in SpringApplication class which is used for bootstrapping our application
Spring Boot database:
Spring Boot can auto-configure embedded H2, HSQL and Derby databases. We don’t need to provide any connection URLs, simply include a build dependency to the embedded database that you want to use. As seen is the first screenshot we have added the H2 dependency in the POM.
One of the important thing that we may like to configure while working with any database is to see the state of database. As H2 is embedded into the Spring Boot we need to do some configuration to to view the state of database.
The following Spring Configuration declares the servlet wrapper for the H2 database console and maps it to the path of /console.
Once you deploy the application we can view the database state in following url
To login to the database use the following connection url and credentials(no password is required )
Database can be queried once we login. As shown below.
Note: H2 is in-memory database, so every time we deploy the application database is recreated.
Spring Boot Resources
Sometime we may want to use some resource with Spring Boot application such as properties file, some sql files to initialize database etc. This can be done easily by adding files in the resource path. Nothing more needs to be done.
Creating webservice using Spring Boot
Controller
To create RESTful webservice we need a controller class as shown below.
@RestController annotation will indicate that this is a restful webservice controller and we use @RequestMapping to map url path
Service Layer:
This is a normal service layer indicated by stereotype @Service.
Dao Layer:
We can extend our interface with marker interface org.springframework.data.repository.Repository which provide captures the domain type to manage as well as the domain type's id type. General purpose is to hold type information as well as being able to discover interfaces that extend this one during classpath scanning for easy Spring bean creation.
Entity Class
We create an entity class but we have to ensure that there is a no argument constructor if we are adding any other constructor
Deploying and running Spring Boot application
Once we deploy Spring Boot application we can run it with java -jar command.



















