In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use CLI in Spring Boot, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Introduction to Spring Boot CLI:
Official website address:
Https://docs.spring.io/spring-boot/docs/current/reference/html/cli.html#cli.
Installation official website address: https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.installing.cli.
Spring Boot CLI is a command-line tool that you can use if you want to quickly develop spring applications. It allows you to run Groovy scripts, which means you have a familiar java syntax and don't have so much boilerplate code. You can also start a new project or write your own commands for it.
Spring Boot CLI is a command line interface for rapid development of Spring applications. CLI is short for Command Line Interface, and CLI takes advantage of Spring Boot's initial dependence and automatic configuration, allowing people to focus on the code itself without having to complete tedious configuration. In fact, the essence of Spring Boot is to simplify tedious Spring configuration, but the use of CLI really makes it very fast to develop Spring programs.
CLI can detect the classes used in the code and know which start-up dependencies need to be added to Classpath to get the program running. And when the dependencies are added, the automatic configuration is done, ensuring that DispatcherServlet and Spring MVC are enabled so that you can respond to HTTP requests. So it's very fast to write a Spring "HelloWorld" in CLI. Introduce the installation of Windows and Linux respectively.
two。 Install CLI:
Four installation methods: here we use a general manual installation.
Manual installation:
Manually installed on Windows, Mac, Linux systems can be used
You can download the Spring CLI installation package from the spring software repository:
(1) download:
Widows version:
Spring-boot-cli-2.5.6-bin.zip: https://repo.spring.io/ui/native/release/org/springframework/boot/spring-boot-cli/2.5.6.
Linux version:
Spring-boot-cli-2.5.6-bin.tar.gz: https://repo.spring.io/ui/native/release/org/springframework/boot/spring-boot-cli/2.5.6.
The INSTALL.txt installation documentation is as follows:
Prerequisites-Spring Boot CLI requires Java JDK v1.8 or above to run. Groovy v ${groovy.version} is packaged as part of this distribution, so no installation is required (ignoring the existing Groovy installation). CLI will use any JDK it finds on the path to check if you have the right JDK which version you should run: java-version or you can set the JAVA_HOME environment variable to point to the appropriate JDK. Environment variable: (after download, extract to a directory, and then add the extracted bin directory to the Path environment variable.) Check your installation-- to test whether the command line is installed successfully, you can execute the following command: spring-- version
(2) decompression:
When you have finished downloading the required archives to your local computer, unzip them to the directory without Chinese on the c / d disk.
(3) configure system environment variables:
Then add the extracted bin directory to the Path environment variable:
(4) Test whether the installation is successful:
Enter cmd after win+r: windows console appears, enter: spring-- version
3. Run the Groovy script
You can compile and run the Groovy source code using the command. Springboot CLI is completely self-contained, so you don't need any external Groovy installation.
After completing the installation of Spring Boot CLI, let's try to use it to quickly build a Spring Boot application. The method is very simple, just do the following:
Step 1: create a new Groovy script, hello.groovy, as follows:
@ RestControllerclass ThisWillActuallyRun {@ RequestMapping ("/") String home () {"Hello Worldwide -"}}
Or the Java version. The hello.java file is as follows:
@ RestControllerpublic class Hello {@ RequestMapping ("/ hello") public String hello () {return "Hello World Java!";}}
As shown in the figure, the location of my hello.groovy file is: (enter cmd in the directory where the hello.groovy file is located)
Step 2: run the Groovy script using the spring command, as follows:
To compile and run the application, type the following command:
Spring run hello.groovy
Expand
To pass command-line arguments to the application, use to separate the command from the "spring" command argument, as shown in the following example:-
Spring run hello.groovy-server.port=9000
To set the JVM command line parameters, you can use environment variables, as shown in the following example: JAVA_OPTS:
JAVA_OPTS=-Xmx1024m spring run hello.groovy
Note: when setting up on Windows, be sure to reference the entire directive, for example. Doing so ensures that the value is passed correctly into the process. JAVA_OPTSset "JAVA_OPTS=-Xms256m-Xmx2048m"
Step 3: Web page test visit: http://localhost:8080/
Access to controller succeeded
Test java version: http://localhost:8080/hello
Access to controller succeeded
4. How CLI works: (what did CLI do for us? )
(1) to infer the dependence of "grasp":
The standard Groovy contains a comment that allows you to declare dependencies on third-party libraries. This useful technique allows Groovy to download jar in the same way as maven or Gradle, but without using the build tool @ Grab
Spring Boot extends this technique further and tries to infer which libraries to "crawl" from your code. For example, because the previously shown code uses comments, springboot can grab "tomcat" and "spring MVC". WebApplication@RestController
(2) infer "grab" coordinates:
Springboot extends Groovy's standard support, allowing you to specify no group or version dependencies (for example). Doing so looks at Spring Boot's default dependency metadata to infer the group and version dependencies of the product body. @ Grab@Grab ('freemarker')
(3) automatic master method:
Unlike the equivalent Java application, you don't need to include a method in the script. The main method is created automatically, which automatically creates the following code for us:
Public static void main (String [] args) GroovySpringApplicationsource
(4) Custom dependency management:
By default, CLI uses declared dependency management when resolving dependencies. Additional dependency management, which overrides the default dependency management, can be configured by using comments. The annotation value should specify the coordinates of one or more Maven BOM:
Spring-boot-dependencies@Grab@DependencyManagementBomgroupId:artifactId:version
For example, consider the following statement:
@ DependencyManagementBom ("com.example.custom-bom:1.0.0")
When you specify multiple BOM, they are applied in the order in which you declared them, as shown in the following example:
@ DependencyManagementBom (["com.example.custom-bom:1.0.0", "com.example.another-bom:1.0.0"]) these are all the contents of the article "how to use CLI in Spring Boot". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.