Top / Spring Boot

ひさしぶりにSpringみてみたら、Spring のApplicationを作成するのにものすごく便利なフレームワークができてました。Spring Bootです。

Spring Boot

やってみる

今回は、このチュートリアルから。 Getting Started · Building an Application with Spring Boot

$ curl http://localhost:8080/

ってアクセス出来るWEBサーバを立ち上げてみます。

GitからClone

$ git clone https://github.com/spring-guides/gs-spring-boot.git
Cloning into 'gs-spring-boot'...
remote: Counting objects: 970, done.
remote: Total 970 (delta 0), reused 0 (delta 0), pack-reused 970
Receiving objects: 100% (970/970), 424.60 KiB | 189.00 KiB/s, done.
Resolving deltas: 100% (620/620), done.

ソースの確認

$ cd gs-spring-boot/initial/
$ cat src/main/java/hello/Application.java
package hello;

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }

}
$ cat src/main/java/hello/HelloController.java
package hello;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

}

ビルドと実行

$ mvn package && java -jar target/gs-spring-boot-0.1.0.jar
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-spring-boot 0.1.0
[INFO] ------------------------------------------------------------------------
... 割愛 ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.909 s
[INFO] Finished at: 2017-01-15T18:39:30+09:00
[INFO] Final Memory: 29M/464M
[INFO] ------------------------------------------------------------------------

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.3.RELEASE)

... 割愛 ...

viewControllerHandlerMapping
viewResolver
websocketContainerCustomizer
welcomePageHandlerMapping

アクセスしてみる

$ curl http://localhost:8080/
Greetings from Spring Boot!

WEBサーバ(組み込みのTomcatっぽい)が起動している事が確認できました。

全体の構造の確認

Spring Bootによって起動される部分は Application.java です。

実際にクライアントから呼び出されるロジック部は HelloController?.javaです。このクラスにはURLのマッピング @RequestMapping?("/") などがannotationされたメソッドが定義されていて、クライアントから

http://localhost:8080/

とURLのマッピングに従って呼び出すことができます。

関連リンク

コンテンツ一覧


FrontPage
現在のアクセス:2741


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2017-01-17 (火) 11:26:22 (2646d)