Springbootcli Tutorial PDF
Springbootcli Tutorial PDF
i
Spring Boot CLI
Audience
This tutorial will be useful for most Java developers, starting from beginners to experts.
After completing this tutorial, you will find yourself at a moderate level of expertise in
Spring Boot CLI, from where you can take yourself to next levels.
Prerequisites
Knowledge of basic Java programming language and Spring is the only prerequisite for
learning the concepts explained in this tutorial.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected]
i
Spring Boot CLI
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
Features ........................................................................................................................................................... 1
ii
Spring Boot CLI
Include/Exclude ............................................................................................................................................. 62
iii
1. Spring Boot CLI — Overview Spring Boot CLI
The Spring Boot CLI is a Command Line Interface for Spring Boot. It can be used for a
quick start with Spring. It can run Groovy scripts which means that a developer need not
write boilerplate code; all that is needed is focus on business logic. Spring Boot CLI is the
fastest way to create a Spring-based application.
Features
In this section, we will look at the different features of Spring Boot CLI −
It provides an interface to run and test Spring Boot Application from the Command
Prompt.
It internally uses Spring Boot Starter and Spring Boot AutoConfigurate components
in order to resolve all dependencies and execute the application.
1
2. Spring Boot CLI — Environment Setup Spring Boot CLI
Spring is a Java-based framework; hence, we need to set up JDK first. Following are the
steps needed to setup Spring Boot CLI along with JDK installation.
If you are running Windows and have installed the JDK in C:\jdk1.6.0_15, you would
have to put the following line in your C:\autoexec.bat file −
set PATH=C:\jdk1.6.0_15\bin;%PATH%
set JAVA_HOME=C:\jdk1.6.0_15
On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk1.6.0_15 and you
use the C shell, you will have to put the following into your .cshrc file −
Make sure you set your CLASSPATH variable on this directory properly otherwise, you will
face a problem while running your application.
Or set the path in Command Prompt temporarily to run the Spring Boot application as
shown below −
2
Spring Boot CLI
3
3. Spring Boot CLI — Hello World Example Spring Boot CLI
In this example, we will create a Spring Boot + MVC + Rest-based Web application.
@RestController
class FirstApplication {
@RequestMapping("/")
String welcome() {
"Welcome to TutorialsPoint.Com"
}
}
Now Spring Boot CLI will come into action, download the required dependencies, run the
embedded tomcat, deploy the application and start it. You can see the following output on
console –
Resolving
dependencies..........................................................
...............................................................................
.
........................
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _> | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
4
Spring Boot CLI
5
Spring Boot CLI
6
Spring Boot CLI
Hello World
Important points
Consider the following points to understand how Spring CLI works.
All dependency JARs are downloaded for the first time only.
Finally after the compilation of the code, deploy the war file on an embedded tomcat
and start embedded tomcat server on the default port 8080.
7
4. Spring Boot CLI — "grab" Dependency Spring Boot CLI
Deduction
Grab Hints
The following table lists down the hints that Spring Boot uses to download third party
libraries –
JDBC Application
2 @EnableJms
JMS Application
3 @EnableCaching
Caching abstraction
4 @Test
JUnit
5 @EnableRabbit
RabbitMQ
6 @EnableReactor
Project Reactor
7 extends Specification
Spock test
8 @EnableBatchProcessing
8
Spring Boot CLI
Spring Batch
9 @MessageEndpoint, @EnableIntegrationPatterns
Spring Integration
10 @EnableDeviceResolver
Spring Mobile
12 @EnableWebSecurity
Spring Security
13 @EnableTransactionManagement
9
5. Spring Boot CLI — "grab" Co-ordinates Spring Boot CLI
Deduction
We can specify a dependency using the @Grab annotation even without specifying group
or version. For example,
@Grab('antlr')
Now Spring Boot CLI will download 2.7.7 version of antlr as it is present in Spring Boot's
default dependency metadata for 1.5.8 version. Spring Boot maintains all dependency
versions by default, which are provided in its CLI, Maven dependency management and
Gradle plugin. Whenever we declare a dependency of any of those artifacts present in
default dependency metadata without declaring a version, the version listed in its table
will be used.
The following table shows all the dependencies and their versions included in the default
metadata for Spring Boot CLI 1.5.8 version.
10
Spring Boot CLI
11
Spring Boot CLI
12
Spring Boot CLI
13
Spring Boot CLI
14
Spring Boot CLI
com.h2database h2 1.4.196
15
Spring Boot CLI
16
Spring Boot CLI
17
Spring Boot CLI
18
Spring Boot CLI
19
Spring Boot CLI
20
Spring Boot CLI
21
Spring Boot CLI
22
Spring Boot CLI
23
Spring Boot CLI
24
Spring Boot CLI
25
Spring Boot CLI
26
Spring Boot CLI
27
Spring Boot CLI
28
Spring Boot CLI
29
Spring Boot CLI
30
Spring Boot CLI
31
Spring Boot CLI
32
Spring Boot CLI
33
Spring Boot CLI
34
Spring Boot CLI
35
Spring Boot CLI
36
Spring Boot CLI
37
Spring Boot CLI
38
Spring Boot CLI
39
Spring Boot CLI
40
Spring Boot CLI
41
Spring Boot CLI
42
Spring Boot CLI
43
Spring Boot CLI
44
Spring Boot CLI
45
Spring Boot CLI
46
Spring Boot CLI
47
Spring Boot CLI
48
Spring Boot CLI
49
Spring Boot CLI
50
Spring Boot CLI
51
Spring Boot CLI
52
Spring Boot CLI
53
Spring Boot CLI
54
6. Spring Boot CLI — Default Statements Spring Boot CLI
In this chapter, we will learn about the Default Statements in Spring Boot CLI. To begin
with, we will learn about the Default Imports.
Default Imports
Spring CLI automatically imports many libraries by default so that explicit imports are not
required. Let us now consider the following groovy script to understand Default Imports.
@RestController
class FirstApplication {
@RequestMapping("/")
String welcome() {
"Welcome to TutorialsPoint.Com"
}
}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _> | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.8.RELEASE)
...
2017-11-07 19:22:17.310 INFO 4824 --- [ runner-0]
o.s.boot.SpringApplication
: Started application in 3.405 seconds (JVM running for 7.021)
55
Spring Boot CLI
56
7. Spring Boot CLI — Starter Thymeleaf Project Spring Boot CLI
Step Description
3 Compile and run the application to verify the result of the implemented logic.
TestApplication/message.groovy
@Controller
@Grab('spring-boot-starter-thymeleaf')
class MessageController {
@RequestMapping("/message")
String getMessage(Model model) {
String message = "Welcome to TutorialsPoint.Com!";
model.addAttribute("message", message);
return "message";
}
}
TestApplication/templates/message.html
<!DOCTYPE HTML>
<html xmlns:th = "http://www.thymeleaf.org">
<head>
<title>Spring Boot CLI Example</title>
57
Spring Boot CLI
<body>
<p th:text = "'Message: ' + ${message}" />
</body>
</html>
TestApplication/static/index.html
<!DOCTYPE HTML>
<html>
<head>
<title>Spring Boot CLI Example</title>
<meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"
/>
</head>
<body>
<p>Go to <a href = "/msg">Message</a></p>
</body>
</html>
Now Spring Boot CLI will come into action, download the required dependencies, run the
embedded tomcat, deploy the application and start it. You can see the following output on
console –
Resolving dependencies.............................
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _> | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
58
Spring Boot CLI
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.8.RELEASE)
...
2017-11-08 16:27:28.300 INFO 8360 --- [ runner-0]
s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080
(http)
2017-11-08 16:27:28.305 INFO 8360 --- [ runner-0]
o.s.boot.SpringApplication : Started application in 4.203 seconds
(JVM running for 38.792)
Go to Message
Click on the Message link and you will see the following output –
Important points
Consider the following points to understand the actions taken by Spring CLI –
Spring CLI automatically detects the version using its metadata, as we have not
specified any group id or version id here.
Finally after code compilation, deploy the war on a embedded tomcat, start
embedded tomcat server on the default port 8080.
59
8. Spring Boot CLI — Testing Application Spring Boot CLI
In this chapter, we will test the sample project created in Hello World Example Chapter to
demonstrate the testing capabilities of Spring CLI. Follow the steps listed in the table below
to test the sample project −
Steps Description
2 Compile and run the application to verify the result of the implemented logic.
FirstApplication/FirstApplication.groovy
@RestController
class FirstApplication {
@RequestMapping("/")
String welcome() {
"Welcome to TutorialsPoint.Com"
}
}
FirstApplication/TestFirstApplication.groovy
class TestFirstApplication {
@Test
void welcomeTest() {
assertEquals("Welcome to TutorialsPoint.Com", new
FirstApplication().welcome())
}
}
60
Spring Boot CLI
Now Spring Boot CLI will come into action, download the required dependencies, compile
the source and test file and unit test the code. The following output will be generated on
console –
Resolving dependencies........................................................
.
Time: 0.457
OK (1 test)
Important points
Consider the following points to understand the actions taken by Spring CLI –
Spring CLI automatically detects the version using its metadata, as we have not
specified any dependency.
61
9. Spring Boot CLI — Packaging Application Spring Boot CLI
Spring Boot CLI provides jar command in order to package an application as jar file. Let
us test the sample project created in Starter Thymeleaf Project chapter to demonstrate
the packaging capabilities of Spring CLI.
Output
The command will print the following output −
Output
Now you can see two new files created in the TestApplication folder.
Include/Exclude
By default, following directories are included along with their contents −
public
resources
static
templates
META-INF
repository
build
target
*.jar files
62
Spring Boot CLI
*.groovy files
Using --include, we can include directories excluded otherwise. Using --exclude, we can
exclude directories included otherwise.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _> | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.8.RELEASE)
...
2017-11-08 16:27:28.300 INFO 8360 --- [ runner-0]
s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080
(http)
2017-11-08 16:27:28.305 INFO 8360 --- [ runner-0]
o.s.boot.SpringApplication : Started application in 4.203 seconds
(JVM running for 38.792)
Go to Message
Click on Message link and you will see the following output −
63
10. Spring Boot CLI — Creating Project Spring Boot CLI
Spring Boot CLI can be used to create a new project with maven as default build tool using
init command. Maven will use https://start.spring.io service. In the following example, we
will create a web application using thymeleaf. Go to E:\Test folder and type the following
command –
64
11. Spring Boot CLI — Using Shell Spring Boot CLI
Spring Boot CLI provides a Shell interface to run the commands in which we can directly
run the commands as shown below. Go to E:\Test folder and type the following command
−
version
Spring CLI v1.5.8.RELEASE
You can press Tab to auto complete the commands and type Exit to finish the shell console.
E:\Test\FirstApplication>spring shell
?[1mSpring Boot?[m?[2m (v1.5.8.RELEASE)?[m
Hit TAB to complete. Type 'help' and hit RETURN for help, and 'exit' to quit.
$ test FirstApplication.groovy TestFirstApplication.groovy
.
Time: 0.347
OK (1 test)
$ exit
E:\Test\FirstApplication>
65