Spring Boot

Spring boot is kind of a sub project which was also developed by the developers of spring framework – to create standalone production ready applications with least amount of configuration requirement. Spring boot packaging is usually as jars(can be fat or normal) based on the requirements also used to make microservices.

Advantages of Spring Boot

  1. Create stand-alone Spring applications that can be started using java -jar.
  2. Embed Tomcat, Jetty or Undertow directly. You don’t need to deploy WAR files.
  3. It provides opinionated ‘starter’ POMs to simplify your Maven configuration.
  4. It automatically configure Spring whenever possible.
  5. It provides production-ready features such as metrics, health checks and externalized configuration.
  6. Absolutely no code generation and no requirement for XML configuration.

Spring initializr ( spring.io ) – Damn easy to generate new application !

It is an online tool using which we can create a basic starter executable application adding all our required dependencies.

Follow the below steps with ease :

  • You can choose a build/dependency management tool among(Mavne/Gradle)
  • Choose desired language among(java/groovy/kotlin)
  • Search for dependencies like Web, Spring REST, Hibernate, etc if you want to add any.
  • Now you are good to go, HIT Generate Project button. This will download the project template in you machine, Now feel free to use it.

Spring boot example

When you open the downloaded zip project, you will see a default created java file like below to start a java based springboot application.

import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@SpringBootApplication  
public class SpringBootTestApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(SpringBootTestApplication.class, args);  
    }  
}

You can run the application just like you run a main program in you favourite IDE or as a jar in command line

A Spring REST application

The @RestController annotation – informs to the Spring to render the result back to the caller.The @RestController is a stereotype annotation. It adds @Controller and @ResponseBody annotations to the class.
The @RequestMappingannotation – is used to provide routing information.

import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  
@RestController  
public class TestController {  
    @RequestMapping(value = "/test", method = "GET")  
    public String display(){  
        return "Hello!";  
    }  
} }

Dependency Management

The dependency management in spring boot controls all the versions of the dependencies to be added so as to avoid discrepancies or version conflicts.

Spring boot manages configuration and dependencies automatically. If needed – overriding a dependency version also works in spring boot.

The spring boot project inherits the dependencies and other plugins from the spring boot starter parent – added by default from spring initialzr

<parent>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>2.0.0.BUILD-SNAPSHOT</version>  
</parent>  

To set the java version in the spring boot project, add the property below in pom.xml – added by default from spring initialzr

<properties>  
    <java.version>1.8</java.version>  
</properties>   

Spring boot maven plugin – added by default from spring initialzr

<build>  
    <plugins>  
        <plugin>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-maven-plugin</artifactId>  
        </plugin>  
    </plugins>  
</build>  

Spring Boot Application Properties

Spring boot provides default properties when we create the project, but these properties can be overriden according to our needs.

For example: changing the server port .

Considering the default port was 8080 and this port is already in use in your machine, and you want your spring boot application to run in port 7070 just in case, do this by simple writing
server.port=7070
in your application.properties file or server: port: 7070 application.yml file

Spring Boot Starters

Starters are a set of convenient dependency descriptors which we can include in our application.

For example, if we want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project. Starter should follow a naming pattern like: spring-boot-starter-*, where * is a particular type of application.
Few of the below application starters are provided by Spring Boot under the org.springframework.boot group:

spring-boot-starter-web-services
spring-boot-starter-data-redis
spring-boot-starter-web
spring-boot-starter-data-elasticsearch
spring-boot-starter-jdbc
spring-boot-starter-jersey
spring-boot-starter-aop
spring-boot-starter-security
production starter features:
spring-boot-starter-actuator
Technical starter – tomcat:
spring-boot-starter-tomcat

Spring Boot Actuator

Spring Boot provides actuator to monitor and manage our application. Actuator is a tool which has HTTP endpoints. when application is pushed to production, you can choose to manage and monitor your application using HTTP endpoints.

How to enable spring boot actuator in an application :
Add the actuator starter dependency in the project:

<dependencies>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-actuator</artifactId>  
    </dependency>  
</dependencies>

Leave a Comment