Introduction to Spring Boot

SHARE & COMMENT :

Spring is the most popular and innovative community for the Java developers. They have changed the way how to build the enterprise applications and how to manage them. In the recent announcement, they come up with another surprising project which is called as “Spring Boot“. They are claiming to be get rid of all the fuss involved on configuring the dependencies for building the enterprise applications. Here as we know that configurations are fully loaded with bunch of XML files (Spring was first introduced the full fledged XML configurations), now they don’t like it. They don’t want anything to be configured or maintained in a file which is extra work for the developers.

Future Spring projects would not have any XML configurations as part of it, everything will be handled by the project Spring Boot. This article provides basic insights on what is all about Spring Boot and how it will benefit the developers. I am working on few examples (as provided in their blog) on this concept, in my next articles I will be coming up with series of Spring Boot examples.

The final version is not yet released. It is in the snapshot release. The documentation work is in-progress. You may not get the complete idea of the project with the available reference documentation.

Spring Boot shipped with command line tool (Groovy) for building the applications and another one is Java implementation for Java developers. However, Groovy follows the Java syntax, which is easy for the Java developers to use either of the tool. If you want to quick start with an example, using the Groovy’s CLI tool is the easy one.

Spring Boot CLI

It is the easiest and quickest way to start using the Spring Boot. It is a command line tool used for executing the groovy scripts. In summary, you can install this tool by following these steps:

  1. Download the binary distributions for this project from here. Spring Boot CLI requires Java JDK v1.6 or above in order to run. Groovy v2.1 is packaged as part of this distribution, and therefore does not need to be installed (any existing Groovy installation is ignored)
  2. If you unpack the the zip file, you will find spring.bat which will check the  all the settings. This script can be found under the directory /bin.

The summary of what is provided in the installation instructions file inside the package.


SPRING BOOT CLI - INSTALLATION
==============================
Thank you for downloading the Spring Boot CLI tool. Please follow these instructions
in order to complete your installation.
Prerequisites
-------------
Spring Boot CLI requires Java JDK v1.6 or above in order to run. Groovy v2.1 is packaged
as part of this distribution, and therefore does not need to be installed (any existing
Groovy installation is ignored).
The CLI will use whatever JDK it finds on your path, to check that you have an appropriate
version you should run:
java -version
Alternatively, you can set the JAVA_HOME environment variable to point an suitable JDK.
Environment Variables
---------------------
No specific environment variables are required to run the CLI, however, you may want to
set SPRING_HOME to point to a specific installation.  You should also add SPRING_HOME/bin
to your PATH environment variable.
Checking Your Installation
--------------------------
To test if you have successfully install the CLI you can run the following command:
spring --version

If you are working with Linux distributions like Ubuntu, then run the following command to install through GVM

$ gvm install springboot
$ spring --version
Spring Boot v0.5.0.M4</code>

Hello Word Example Using Spring Boot

I have used the same example provided in the spring boot’s official documentation.

Create the app.groovy with the following lines of code.

@Controller
class ThisWillActuallyRun {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!"
    }

}

Once the above file is created, you can run the application by using the following command:

$spring run app.grrovy

You can invoke the http://localhost:8080 in your browser and you will see the result “Hello World!”.The above command can invoke the application and run it in the web server. If any dependencies are required like web server, etc. can be resolved by the Spring Boot itself. If we have more than one web server in the class path, how it decides to choose the web server to run the application?. The details are yet to be addressed by the final documentation, but it looks the initial impression on the project is very good and developers are eagerly waiting for the final launch of this project to the public.Spring Boot With Java

There is another implementation of the same project for the Java developers. If you don’t want to use the above implementation, you can choose the Java implementation with Maven build (Read : How to Install Maven on Windows / Ubuntu ?). As a first step, you have to create create the pom.xml with the following artifacts.


<?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.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>

<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>0.5.0.M4</version>
</parent>

<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

<!-- Package as an executable JAR -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<!-- Allow access to Spring milestones and snapshots -->
<!-- (you don't need this if you are using anything after 0.5.0.M2) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.springsource.org/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.springsource.org/milestone</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.springsource.org/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.springsource.org/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>

Once maven is in the class path, it is very easy to create a maven project and run the example. Just create a Java file with the below code snippet:


import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}

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

Once you have created the above Java file and the pom.xml in the same directory, please run the following command:


$ mvn package
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar

First statement invokes the pom.xml and builds the complete package by downloading all the dependencies. The second statement makes the spring application and bundle with the web server. As said in the above groovy example, you can invoke the application in the browser.

I hope this article have provided the basic steps to build the applications using Spring Boot, how ever, we have lot of questions open to the spring boot team about the complex projects how it manages all the dependencies. In my future articles, I would come up with few more complex examples which will be helpful to understand it better. If you have problems in understanding this, please  write it in the comments section.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better