📝

【Spring Boot】素のSpring BootでHello, world!

2024/06/28に公開

なにも追加しない Spring Boot プロジェクトで Hello, world! します。

1. 動作環境

  • macOS 14.5
  • Java 21
  • Gradle 8.8
  • Spring Boot 3.3.1

2. 雛形作成

Spring Initializrでプロジェクトの雛形を作成します。入力値はデフォルトのままです。(以下参照)

項目名 入力値
Project Gradle - Groovy
Language Java
Spring Boot 3.3.1
Project Metadata Group: com.example
Artifact: demo
Name: demo
Description: Demo project for Spring Boot [1]
Package name: com.example.demo
Packaging: Jar
Java: 21
Dependencies なし

3. Hello, world!

src/main/java/com/example/demo 配下に以下のクラスを追加します。

DemoRunner.java
package com.example.demo;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class DemoRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("Hello, world!");
    }

}

4. 実行

./gradlew bootRunを実行するとバナーと起動ログのあとに Hello, world! が表示されます。

$ ./gradlew bootRun

> Task :bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.3.1)

2024-06-28T20:27:03.222+09:00  INFO 88622 --- [demo] [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 21.0.3 with PID 88622 (/Users/iwazou/Desktop/work/demo/build/classes/java/main started by iwazou in /Users/iwazou/Desktop/work/demo)
2024-06-28T20:27:03.224+09:00  INFO 88622 --- [demo] [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2024-06-28T20:27:03.531+09:00  INFO 88622 --- [demo] [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.558 seconds (process running for 0.849)
Hello, world!

BUILD SUCCESSFUL in 1s
4 actionable tasks: 1 executed, 3 up-to-date
$

5. 実行可能 JAR のビルド

./gradlew bootJarを実行すると実行可能 JAR ファイルがビルドされます。

$ ./gradlew bootJar

BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
$ java -jar build/libs/demo-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.3.1)

2024-06-28T20:54:10.077+09:00  INFO 91487 --- [demo] [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT using Java 21.0.3 with PID 91487 (/Users/iwazou/Desktop/work/demo/build/libs/demo-0.0.1-SNAPSHOT.jar started by iwazou in /Users/iwazou/Desktop/work/demo)
2024-06-28T20:54:10.079+09:00  INFO 91487 --- [demo] [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2024-06-28T20:54:10.467+09:00  INFO 91487 --- [demo] [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.655 seconds (process running for 1.048)
Hello, world!
$
脚注
  1. 2024/6/28 時点の Spring Initializr では Gradle - Groovy プロジェクトのDescriptionの入力は反映されない。 ↩︎

Discussion