Introduction to Spring Boot

In this tutorial you are going to learn what Spring Boot is and how you can start using it.

java-featured-image

Prerequisites

  • 10-20 minutes of your time
  • familiarity with Maven

What is Spring Boot?

Spring Boot makes the process of creating a stand-alone Spring based application very easy and simple. By using the Spring Boot Java-based framework, you will need a very insignificant amount of configuration.

If you are familiar with Spring in general, you know how massive and unmanageable the XML conifguration file in Spring becomes at some point. Well, thanks to Spring Boot, you won’t run into that issue.

Another benefit that comes when using Spring Boot is it reduces the development time significantly and provides an overall easier way of getting started with the application.

Advantages

  • Very easy to deploy
  • Easy to pick up
  • Saves time
  • Not much configuration needed
  • Provides dependency management
  • Provides annotation-based spring application

Now that you know what Spring Boot is and you are familiar with the advantages it provides, let me show you how you can get started using it right away.




Building with Maven

Within your project directory, create a subdirectory.

Example:

mkdir -p src/main/java/demo

This is how your pom.xml file should look like:

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

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

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


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

</project>

Please note that we are using the spring boot maven plugin. In a nutshell, it searches for the main() method, provides build-in dependency resolver which sets the version number match the Spring Boot dependencies and finally, it collects all the jars on the classpath and then build a runnable “uber-jar” which makes it more convenient to execute.

Let’s build a demo Web Application

Let’s create our controller. The path for the controller is:

src/main/java/demo/DemoController.java

DemoController.java

package demo;

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

@RestController
public class DemoController {
    @RequestMapping("/")
    public String handle() {
        return "Hello World!";
    }
}

For this controller class, we are using the @RestController annotation, which if you are not familiar with, it basically makes it ready for use by Spring MVC to be able to handle web requests. Therefore, all web requests to the “/” path, will be handled by our handle() method. The reason the web requests will be handled for this particular path (“/”) is because we are using the @RequestMapping annotation which maps it to this web path.

Now is time to create our Application class.

Let’s build an DemoApplication class

Let’s create our DemoApplication class. The path for the application class is:

src/main/java/demo/DemoApplication.java

DemoApplication.java

package demo;

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

@SpringBootApplication
public class Application {
	// that's the main method
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Thanks to the @SpringBootApplication annotation, we are initializing the entry point of the Spring Boot Application. Notice that we have our main method in this class.

The @SpringBootApplication annotation contains @EnableAutoConfiguration, @ComponentScan and @SpringBootConfiguration. When using the @SpringBootApplication annotation, we “invoke” all of the above-mentioned annotations.

Run the application

To run the application with maven, execute the following line:

mvn package && java -jar target/gs-spring-boot-0.1.0.jar

Leave a Reply

avatar