Spring Boot CLIのセットアップとHelloWorldの例

私の以前の投稿「Spring Bootの紹介」と「Spring Bootのコンポーネントと内部」では、Spring Bootの基本的な内容と4つの主要なSpring Bootコンポーネントの使用について話しました。今回はSpring Bootコンポーネントの一つであるCLIについて詳しく説明します。

Spring Boot CLIとは何ですか?

Spring Boot CLI(Command Line Interface)は、コマンドプロンプトからSpring Bootアプリケーションを実行およびテストするためのSpring Bootソフトウェアです。CLIを使用してSpring Bootアプリケーションを実行すると、内部的にはSpring Boot StarterおよびSpring Boot AutoConfigurateコンポーネントを使用してすべての依存関係を解決し、アプリケーションを実行します。内部的には、GroovyとGrape(JAR依存性マネージャー)が含まれており、Spring Bootのデフォルトを追加し、すべての依存関係を自動的に解決します。Windows環境でのCLIのインストール、CLIのセットアップ、およびCLIのコマンドについて説明します。ほかの環境でもほぼ同様です。

Spring Boot CLIのインストール

Windows InstallerまたはZipファイルのいずれかを使用して、Spring Boot CLIソフトウェアをインストールすることができます。どちらの方法も簡単にインストールでき、同じSpring Boot CLIソフトウェアが提供されます。私たちはZipファイルを使用する簡単な方法を使用します。Spring Bootの最新バージョンである1.2.3.RELEASEを使用します。Spring Boot CLIソフトウェアは、https://start.spring.io/(Spring Initializr WebInterfaceです。このコンポーネントについては、後のポストで詳しく説明します)からダウンロードできます。Windows環境でSpring Boot CLIソフトウェアをインストールしてセットアップするために、次の手順に従ってください。

  • Download Spring Boot CLI zip file using Spring Initilizr
    Click on “Download Spring CLI Zip” button as shown below: Once we download Spring Boot CLI Zip file into our local FileSystem, it looks like this. – Extract spring-boot-cli-1.2.3.RELEASE.zip file into our local FileSystem.
    – Set Spring Boot CLI Environment Variables in Windows System as shown below.
set PATH=D:\spring-boot-cli-1.2.3.RELEASE\bin;%PATH%
  • Execute the below command to verify our installation process.
    We can use “spring –version” to know the Spring Boot CLI Version as shown below.
spring --version

以下のようにして、Spring Boot CLIのバージョンを知るために「spring –help」を使用することができます。

spring --help

今、私たちのSpring Boot CLIのインストールプロセスは成功裏に完了しました。Spring Bootの「HelloWorld」の例について話し合う前に、まずはコマンドプロンプトからGroovyスクリプトを実行する方法を見ていきます。

Spring Bootの「spring」コマンド

Spring Boot CLIソフトウェアは、コマンドプロンプトからSpring Boot Groovyスクリプトを実行するための「spring」というコマンドを提供します。先ほど見たように、Spring Bootの「spring –help」というコマンドには、このコマンドをさまざまな目的で使用するための多くのオプションがあります。ここで使用する重要なオプションは、「run」オプションです。「spring」コマンドの構文は次のとおりです-

 spring run <SpringBoot-Groovy-Scriptname>

こちらはSpring BootアプリケーションのGroovyスクリプトのファイル名です。このコマンドを使用して、Spring BootのHelloWorldの例を実行します。Spring Boot CLIを使用して、簡単なHelloWorld Spring Bootの例と一緒に作業する時間です。

Spring BootのHelloWorldの例を示す。

今からSpring Boot MVC RestControllerの例を開発します。これはSpring Boot Frameworkの力を示すためにPivotalチームがTwitterで最初に公開した例です。Spring Boot HelloWorldの例を開発するには、以下の手順に従ってください。

  • Create a “HelloWorld” Folder in our Local FileSystem to place our groovy scripts.
  • Develop a Groovy script file using the following content
@RestController
class HelloWorld {
  @RequestMapping("/")
  String hello() {
    "Hello JournalDev World."
  }
}

このファイルの名前をHelloWorld.groovyとしてください。ここでは、”.groovy”の拡張子が必要です。コードの説明。

  • Defined a REST Controller using Spring 4 MVC @RestController annotation.
  • Defined a Mapping URL “/” using Spring MVC @RequestMapping annotation.
  • Defined a method to return a String to a Client or Web Browser.

コードの観察 HelloWorld.groovy を観察すると、以下の重要なポイントが見つかります。

  • 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

では、これらすべてのものを私たちのSpring Boot HelloWorldアプリケーションに提供するのは誰ですか?まずアプリケーションを実行して結果を確認し、それからこの質問に答えましょう。- 今、Spring Boot HelloWorldの例のフォルダーはこのようになります。

今、私たちのSpring Boot HelloWorldの例は、Spring MVC RestControllerを使用して準備ができています。Spring Boot Frameworkのパワーを知るために、この例を実行してテストする時間です。

Spring BootのHelloWorldの例を実行する。

弊社のSpring Boot HelloWorld Exampleアプリケーションをテストするために、以下の手順に従ってください。

  • Open command prompt at “HelloWorld” Folder in our Local FileSystem.
  • Execute the following command
 spring run HelloWorld.groovy
  • Observe the output at “spring run” command console.
    If we observe here, when we execute “spring run HelloWorld.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. NOTE:- If we observe the above screen shot, I have highlighted “SpringApplication” class file. Here o.s.boot.SpringApplication means org.springframework.boot.SpringApplication class. What is this SpringApplication? What is the use of SpringApplication? Please refer my upcoming posts to answer these questions.- Open browser and access the following link.
    Access this URL: https://localhost:8080/ Now we are able to access our first Spring Boot MVC RESTful WebService.

もし私たちがこのSpring Bootアプリケーションを観察するなら、私たちの頭にはこの疑問が浮かぶかもしれません:誰が私たちのSpring Boot HelloWorldアプリケーションにこれらのすべてのものを提供するのでしょうか?

  • 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

この質問の答えは、Spring BootのコアコンポーネントであるGroovyコンパイラ(groovyc)とGroovy Grape(GroovyのJAR依存関係マネージャ)の責任を持っています。Spring Bootコンポーネントは、必要なインポートの追加、必要な構成の提供、jar依存関係の解決、main()メソッドの追加などのデフォルトを提供するためにGroovyコンパイラとGroovy Grapeを使用しています。Spring Boot開発者として、これらのことを心配する必要はありません。Spring Bootフレームワークがこれらのすべてを世話をしてくれます。それがSpring Bootフレームワークの美しさです。これにより、Spring Bootフレームワークは多くの雛形コードやSpringの設定を避け、開発時間を短縮し、生産性を向上させることができます。ここでは、Spring Bootの注釈やAPI、Spring Bootアプリケーションのmain()メソッドの使用については詳しく説明しませんでした。これらの質問には、Spring BootのIDEを使用して今後の投稿で回答します。これでSpring Boot CLIについての説明は終わりです。ご質問があれば、コメントでお知らせください。

コメントを残す 0

Your email address will not be published. Required fields are marked *