Thursday 13 October 2016

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.

No comments:

Post a Comment