Thursday 13 October 2016

What is Spring Boot Initializr ?

The Spring Initializr is ultimately a web application (at “http://start.spring.io/”) that can generate a Spring Boot project structure for you. It doesn’t generate any application code, but it will give you a basic project structure and either a Maven or a Gradle build specification to build your code with.

Spring Initializr can be used in several ways:
  • Through a web-based interface
  • Via Spring Tool Suite
  • Via IntelliJ IDEA
  • Using the Spring Boot CLI
Why we need Spring Boot Initializr?
Sometimes the hardest part of a project is getting started. You need to set up a directory structure for various project artifacts, create a build file, and populate the build file with dependencies. The Spring Boot CLI removes much of this setup work, but if you favor a more traditional Java project structure, you’ll want to look at the Spring Initializr.

USING SPRING INITIALIZR’S WEB INTERFACE
Now we are starting with the web-based interface.

The Spring Team has provided a Web Interface for Spring Boot Initializr at “http://start.spring.io/”. We can use it to create our new Project’s base structure for Maven/Gradle build tools.

Creating Maven Example with Spring Boot Initializr Web Interface
There are following steps to create new Spring Boot Web Application for Maven Build tool and Spring STS Suite IDE.

Step 1: Go to Spring Boot Initializr at “http://start.spring.io/”.

Step 2: Once you’ve filled in the form and made your dependency selections, click the Generate Project button to have Spring Initializr generate a project for you.
Step 3: Now click on “Generate Project” Button, it creates and downloads a Maven Project as “myapp.zip” file into our local file system.

Step 4: Move “myapp.zip” to our Spring STS Suite Workspace and Extract this zip file
Step 5: Import this “myapp” Maven project into Spring STS IDE.

Step 6: We’d have a project structure similar.

If you observe this project files, it generates pom.xml file, two Spring Boot Java files and one JUnit Java file.
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.  <modelVersion>4.0.0</modelVersion>
  5.  <groupId>com.dineshonjava</groupId>
  6.  <artifactId>myapp</artifactId>
  7.  <version>0.0.1-SNAPSHOT</version>
  8.  <packaging>jar</packaging>
  9.  <name>myapp</name>
  10.  <description>Demo project for Spring Boot</description>
  11.  <parent>
  12.   <groupId>org.springframework.boot</groupId>
  13.   <artifactId>spring-boot-starter-parent</artifactId>
  14.   <version>1.3.5.RELEASE</version>
  15.   <relativePath/> <!-- lookup parent from repository -->
  16.  </parent>
  17.  <properties>
  18.   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19.   <java.version>1.8</java.version>
  20.  </properties>
  21.  <dependencies>
  22.   <dependency>
  23.    <groupId>org.springframework.boot</groupId>
  24.    <artifactId>spring-boot-starter-data-jpa</artifactId>
  25.   </dependency>
  26.   <dependency>
  27.    <groupId>org.springframework.boot</groupId>
  28.    <artifactId>spring-boot-starter-web</artifactId>
  29.   </dependency>
  30.  
  31.   <dependency>
  32.    <groupId>org.springframework.boot</groupId>
  33.    <artifactId>spring-boot-starter-test</artifactId>
  34.    <scope>test</scope>
  35.   </dependency>
  36.  </dependencies>
  37.  
  38.  <build>
  39.   <plugins>
  40.    <plugin>
  41.     <groupId>org.springframework.boot</groupId>
  42.     <artifactId>spring-boot-maven-plugin</artifactId>
  43.    </plugin>
  44.   </plugins>
  45.  </build>
  46.  
  47. </project>

MyappApplication.java

  1. package com.dineshonjava;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class MyappApplication {
  6.  public static void main(String[] args) {
  7.   SpringApplication.run(MyappApplication.class, args);
  8.  }
  9. }


MyappApplicationTests.java
  1. package com.dineshonjava;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.boot.test.SpringApplicationConfiguration;
  5. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  6. import org.springframework.test.context.web.WebAppConfiguration;
  7. @RunWith(SpringJUnit4ClassRunner.class)
  8. @SpringApplicationConfiguration(classes = MyappApplication.class)
  9. @WebAppConfiguration
  10. public class MyappApplicationTests {
  11.  @Test
  12.  public void contextLoads() {
  13.  }
  14. }

Summary

Whether you use Initializr’s web-based interface, create your projects from Spring Tool Suite, or use the Spring Boot CLI to initialize a project, projects created using the Spring Boot Initializr have a familiar project layout, not unlike other Java projects you may have developed before.

Spring Boot is an exciting new way to develop Spring applications with minimal friction from the framework itself. Auto-configuration eliminates much of the boilerplate configuration that infests traditional Spring applications. Spring Boot starters enable you to specify build dependencies by what they offer rather than use explicit library names and version. The Spring Boot CLI takes Spring Boot’s frictionless development model to a whole new level by enabling quick and easy development with Groovy from the command line. And the Actuator lets you look inside your running application to see what and how Spring Boot has done.


Why to use Spring Boot CLI ? (Command Line Interface)

The Spring Boot CLI is a command line tool that can be used if we want to quickly develop with Spring. It allows us to run Groovy scripts, which means that we have a familiar Java-like syntax, without so much boilerplate code. We can also bootstrap a new project or write your own command for it.

It is Spring Boot software to run and test Spring Boot applications from command prompt. When we run Spring Boot applications using CLI, then it internally uses Spring Boot Starter and Spring Boot AutoConfigurate components to resolve all dependencies and execute the application.

It internally contains Groovy and Grape (JAR Dependency Manager) to add Spring Boot Defaults and resolve all dependencies automatically.


Quick start Spring Boot CLI Hello World Example
Here we are using same example provided in Spring Boot Documentation.
Please follow the following steps to develop a Spring Boot HelloWorld Example:
Step 1: Create an “app” Folder in our Local FileSystem to place our groovy scripts.
Step 2: Create a file called app.groovy
@RestController
class app{
    @RequestMapping("/")
    String home() {
        "Hello World!"
    }
}
To compile and run the application type:
$ spring run app.groovy

To pass command line arguments to the application, you need to use a -- to separate them from the “spring” command arguments, e.g.
$ spring run app.groovy -- --server.port=9000

To set JVM command line arguments you can use the JAVA_OPTS environment variable, e.g.
$ JAVA_OPTS=-Xmx1024m spring run app.groovy

Observation on Code:
If we observe our app.groovy, we can find the following important points.

  • No imports
  • No other XML configuration to define Spring MVC Components like Views,ViewResolver etc.
  • No web.xml and No DispatcherServlet declaration
  • No build scripts to create our Application war file
  • No need to build war file to deploy this application

Now for running the application
Open command prompt at “app” Folder in our Local FileSystem.

Execute the following command
spring run app.groovy

when we execute “spring run app.groovy”, it starts Embedded Tomcat server at Default port number: 8080.

Now our Spring Boot HelloWorld Example application is up and running. It’s time to test it now.


In the above example we can observe following points.
  • There is no Import
  • There is no XML or JAVA configuration
  • There is no DispatcherServlet and web.xml
  • There is no build file for creating war like Maven or Gradle

It is the responsibility of Spring Boot Core Components, Groovy Compiler (groovyc) and Groovy Grape (Groovy’s JAR Dependency Manager).

Spring Boot Components uses Groovy Compiler and Groovy Grape to provide some Defaults lime adding required imports, providing required configuration, resolving jar dependencies, adding main() method etc. As a Spring Boot Developer, We don’t need to worry all these things. Spring Boot Framework will take care of all these things for us.

That’s the beauty of Spring Boot Framework.

Default import statements
To help reduce the size of your Groovy code, several import statements are automatically included. Notice how the example above refers to @Component,@RestController and @RequestMapping without needing to use fully-qualified names or import statements.

Automatic main method
Unlike the equivalent Java application, you do not need to include a public static void main(String[] args) method with your Groovy scripts. ASpringApplication is automatically created, with your compiled code acting as the source.


Summary
The Spring Boot CLI takes the simplicity offered by Spring Boot auto-configuration and starter dependencies and turns it up a notch. Using the elegance of the Groovy language, the CLI makes it possible to develop Spring applications with minimal code noise.