Spring Boot




Introduction

  • Framework that makes it very easy to create web applications using Spring
  • Enables rapid development by following the principle of convention over configuration
  • Reduces the tasks performed by developer thereby facilitating quicker development
  • Runs as a stand-alone application with embedded server such as Tomcat, Jetty, etc.
  • Provides various starter modules which simplifies dependency configuration
  • Does auto-configuration wherever possible
  • No requirement for XML configuration
  • Production-ready features such as metrics, health checks, and externalized configuration
  • Supports running from command line


Creating Spring Boot Application

  • Create simple spring boot project using maven
  • In eclipse create maven project using maven-archetype-quickstart
  • Project needs inherit from spring-boot-starter-parent
  • Configure the spring-boot-starter-parent artifact in the parent section of the pom.xml
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.3.1.RELEASE</version>
  </parent>
  • Dependency version is specified in a single place
  • All dependent libraries are added based on this version
  • Eliminates potential errors caused by incompatible library versions
  • Need to change only in one place for upgrading Boot version
  • Create a simple web application using web starter module
  • Set dependency for spring-boot-starter-web in pom.xml
    <dependency>
      <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  • No need to specify version as that is automatically decided based on starter parent version
  • Create startup main class using annotation @SpringBootApplication
  • @SpringBootApplication equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan
package com.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App 
{
    
public static void main( String[] args )
    {
       SpringApplication.run(App.
class, args);
    }
}
  • Runs an embedded server when you run startup class
  • By default it uses tomcat
  • Create a simple Rest Controller to test simple web application - http://localhost:8080/helloworld
package com.springboot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
  @RequestMapping(
"/helloworld")
  
public String helloworld() {
    
return "Hello World from spring boot!!";
  }
}