Introduction to Spring Boot

[Social9_Share type="horizontal"]

Overview of Spring Boot

In many of your projects, you might have used spring in different flavors as Spring Core , spring MVC, spring with Hibernate, spring With REST and so on. While working in spring projects, adding the appropriate jars is the biggest challenge which everyone would have faced. The project management tools like MAVEN or GRADLE are of great support for developing enterprise application in spring. You can add the dependencies and Maven will download and add it to the classpath of your project. Still conflicts may occur because of version difference. Here comes Spring Boot for the rescue.

What is Spring Boot?

  • Spring Boot helps to develop spring based enterprise applications with great ease.
  • It bootstraps the spring applications with all necessary jars.
  • You can add third party jars to a spring enterprise application without great hassle.
  • Spring Boot uses a project management tool like MAVEN or GRADLE.
  • The purpose of Spring Boot is not for code generation, but to create ready to run spring applications.
  • No clumsy XML configuration is needed .
  • Spring Boot comes with a command line tool for running Groovy scripts and, to quickly run your spring applications.
  • Spring Boot also provides a set of Starter maven pom file / gradle build file to which you can add the required dependencies and allow auto configuration.

Now Let’s get started.I have divided this post into three parts.

Part-I : Creating a simple spring application using Spring Boot
Part-II : Creating a spring RESTful service using Spring Boot
Part-III : Packaging as WAR

Part-I : Creating a simple spring application using Spring Boot

Prerequisites :
  • A basic understanding of Maven
  • Spring MVC

What you will need:
  • JDK 1.7 or later
  • Maven3.0+
  • An IDE(I am using Eclipse Mars)
  • Spring Boot 1.3.0
  • Spring Tool Suite Integrated to eclipse(optional)

I am going to develop a spring application using Spring Boot. This can be done in many ways

  • Create a pom.xml file with the Spring Boot starter dependency added and then package and run it
  • Create a maven project and add the required dependencies to pom.xml
  • Create a basic project from Spring Initializr and then modify accordingly
  • If you have STS integrated to eclipse, then you can create it as Spring Boot Application from eclipse itself directly.
  • I am using the third option. I will go to the link specified and create a basic project of type MAVEN.


    Fill in the details for the project. The details are as below.

    • Select project as maven project
    • Spring Boot version is 1.3
    • package name is “com.training.spring”
    • project name is “SpringBootDemo”.
    • Since I am going to deploy this as a jar I am not adding any dependencies.

    Once done, click Generate Project. The maven project is generated and available as a zip file. Extract it in your preferred location. Next, lets import this as a maven project in eclipse.

    Open Eclipse. Goto File ->Import. A new window opens. Select Maven -> Existing Maven Projects. click Next.


    Now, select the maven project from the location where you have extracted it. You will see pom.xml file as selected. Now. click Finish.


    Now your maven project as a Spring Boot Application is ready. Now, let’s see the structure of the project. It is same as a maven project with a simple java file added inside the package.


    Let’s first discuss on pom.xml. You can see the dependency of Spring Boot starter added automatically.


    All the dependent jars related to spring as core, aop, context, logging are added automatically from spring boot starter. The dependency hierarchy of pom.xml is shown below.


    This project will be packaged as a jar file. You can also package it as a war file. We will discuss that later.Next, select your project and right click RunAs -> Maven install to download and install the dependencies.


    Next, let us explore the java class which is already there.This class has a main method. The main method calls the run method of Spring Application. This run method bootstraps the application starting spring which will run the embedded Tomcat Server.


    The first parameter specifies that this is a spring component/bean as this class is annotated with @SpringBootApplication . The second parameter is the command line argument .


    This annotation marks the class as a spring bean, configures the application by adding all the jars based on the dependencies and also scans the other packages for spring beans. This annotation is an alternate to @Configuration, @EnableAutoConfiguration, @ComponentScan all put together.

    @Configuration – Specifies this class as a spring bean
    @EnableAutoConfiguration – This tells how you want to configure Spring, based on the jar dependencies that you have added.
    @ComponentScan – is to scan other packages for spring beans.

    So, it is better to use @SpringBootApplication. I have added a simple print Statement. All Set. Let’s try running this. Select the file and right click RunAs – > Java Application or Spring Boot App(if have integrated STS with eclipse).


    You can see the spring boot starting the IOC container and initializing the beans. You can also see that the statement “Welcome to Spring Boot” printed.After completing the task, the IOC container (ApplicationContext) of spring gets closed.


    You can also run this application from the command line using the jar file that is generated. To get the jar file, select your pom.xml right click -> Run As -> Maven build(choose the second one).


    A new window opens and in Goals type package. Click Apply and Run.


    Now, you can see the jar file that is generated the target folder of your project as given below.


    Next, lets run the jar file from the command line.Open command line and go to the folder where your project is located. Next, move to the target folder and then type java -jar <<filename>>.jar.

    This is the output.The statement “Welcome to Spring Boot” is also printed in the command line. Here also you can see that the IOC container (ApplicationContext) of spring gets closed after completing the task.


    This is a very basic example. You can download the complete code here SpringBootDemo.zip
    [This is a safe ZIP file. you may get warnings saying that it is dangerous. but you can download and extract this file. you will get the complete project.Import this as Maven project in eclipse and run the application to get the result]

    In Part-II, lets convert this to a spring web application by adding the dependency and creating the controller. We will build a RESTful webservice using spring boot and invoke the service in the browser.

    Post a comment