Spring Boot | Getting Started

Spring Boot hello world example

Spring Boot

Spring boot is a framework which enables us to produce flexible and high quality software components. 

Why should we go for Spring Boot

There are a lot of reasons to use Spring Boot instead of the traditional Spring framework. Some of them are :

  1. Lots of Spring Boot starter templates for a quick start.
  2. Embedded Tomcat/Jetty support.
  3. Automatic configuration.
  4. Starter POM configuration for maven configuration.
  5. Standalone services.
  6. Web.xml configuration is discarded due to embedded tomcat.
  7. More features like metrics, health checks.

Prerequisites

Spring Boot framework is not for the absolute beginners. You should at least have basic understanding of web development, spring framework and Maven build process. However, if you are a beginner and don’t have any experience in spring framework or hibernate, check out the below tutorials : 

Spring : Spring Tutorials
Hibernate : Hibernate Tutorials


Environment Configuration

In order to create a Spring Boot application, you need to setup your local machine. Below are the tools you can use for Spring Boot application development :

Spring Tool Suite – It is the best IDE for Spring, Spring Boot application development with additional spring application development support.

Java Version 1.8 – We will be using java development kit version 1.8. So, install the latest java version on your machine.

Maven – Don’t forget to configure maven on your machine as well. Add all the environment variables related to maven.

That’s it, you are now ready to develop your first spring boot application. So let’s get started.


What are the Spring Boot starter templates

Spring comes up with over 40 different starter modules. It provides ready to use integration libraries like Spring framework, hibernate, JPA and other frameworks.

Starter modules provide a ready made platform. You don’t have to make it from the scratch like configuring maven, adding libraries and managing all of the dependencies or server configurations. Spring Boot starter templates do it for you so that you can only focus on your development instead of configuration.

We will discuss some of the main starter modules in this tutorial as below.

Module Name

Description

Spring-boot-starter

This module is the base of spring boot application which provides all basic functionalities.

Spring-boot-starter-actuator

This starter provides you with a functionality to monitor, manage an application, and audit it.

Spring-boot-starter-jdbc

This starter provides you with support to connect and use JDBC databases, connection pools, and so on.

Spring-boot-starter-data-jpa

The JPA starter provides you with needed libraries in order to use Java Persistence API such as Hibernate, and others.

Spring-boot-starter-data-*

This module provides support for a number of data stores such as MongoDB, Data-Rest, or Solr.

Spring-boot-starter-security

Modules to provides support for sprint security in boot application.

Spring-boot-starter-social-*

Social module starter which is used to integrate social plugin to your application easily.

Spring-boot-starter-test

This is a starter that contains the dependencies for spring-test and assorted testing frameworks such as JUnit and Mockito among others.

Spring-boot-starter-web

This gives you all the needed dependencies for web application development.


Let’s now create a basic spring boot starter application. We will be using starter template for rapid development in this tutorial.


Creating a basic starter project

Now that you have basic understanding of spring boot starter modules, we can create a basic application using Spring IO Start.

Follow the below basic steps in order to create a sample starter project :


Spring Boot hello world example



  1. Choose Maven project with Java.
  2. Select the Spring Boot version 1.4.7.
  3. Provide group name like com.sample.
  4. Specify artifact name Sample or anything you want.
  5. Select the H2, JDBC, and JPA starters from the Project Dependencies selection so that we can get the needed artifacts in our build file in order to connect to an H2 database
  6. Click Generate Project to download the project archive.


Import the download maven project into Spring tool suit IDE

Extract the downloaded starter project “Sample.zip” to some folder in your system and start the spring IDE to import the project.

Step 1 – Select file -> Import




Step 2 – Select the extracted starter project directory and click finish.





Now you should be able to see your project structure as below.




Look at the POM.xml file

Starter module automatically adds all required dependencies for you so that you don’t have to add them manually. So let’s look our pom.xml, how it looks like.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.sample</groupId>
 <artifactId>Sample</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>Sample</name>
 <description>Demo project for Spring Boot</description>

 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.4.7.RELEASE</version>
 <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 <java.version>1.8</java.version>
 </properties>

 <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-jdbc</artifactId>
 </dependency>

 <dependency>
 <groupId>com.h2database</groupId>
 <artifactId>h2</artifactId>
 <scope>runtime</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>

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


</project>

We have already discussed most of the starter modules which is being used in this file. But one thing needs attention here.
com.h2database - runtime
Why does this dependency has run time scope ? – We really don’t know to which database we will connect at compile time. Spring Boot will autoconfigure the needed settings and create appropriate beans, once it detects the presence of the org.h2.Driver class in the classpath when the application is launched.


Spring Boot starter Java file

Spring boot automatically creates a starter application java file which is ready to run. So let’s see what’s inside this java file :

package com.sample.Sample;

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

@SpringBootApplication
public class SampleApplication {

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


What @SpringBootApplication actually is ?

Well, @SpringBootApplication annotation is nothing but combination of three frequently used annotation @Configuration@EnableAutoConfiguration and @ComponentScan with their default attributes. A single annotation was made by combining the three because they were being used so frequently in almost every application.

@Configuration – used to configure spring core related annotations.
@ComponentScan – tells Spring that we want to scan our application packages.
@EnableAutoConfiguration – it is a part of the Spring Boot annotation to enable auto configuration of components.


Run the applicaiton

The application is now ready to run. This is the basic implementation of the starter projects. The purpose of this tutorial was to introduce the spring boot starter templates and how to get started.

Right Click on Project -> Run as -> Spring Boot App

Your project should run now.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *