English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SpringBootプログラムの実行

このセクションでは、簡単なSpring Bootアプリケーションを作成し実行します。

Spring Bootアプリケーションを作成

ステップ1: Spring Initializrを開きます https://start.spring.io/。

ステップ2: Spring Bootバージョンを選択 2.2.2.BUILD-SNAPSHOT。

ステップ3: 提供 グループ名前。私たちはグループ名を提供しました com.w3codebox。

ステップ4: 提供 ビルドアイテム。私たちはビルドアイテムを提供しました spring-boot-application-run。

ステップ5: 追加 Spring Web 依存関係

ステップ6: クリック Generate ボタン。"生成"ボタンをクリックすると、アプリケーションに関連するすべての規約が一つの Jar ファイルから、それをローカルシステムにダウンロードします。

ステップ7: 抽出 jarファイル。

ステップ8: フォルダをコピーし、STSワークスペースに貼り付けます。

ステップ9: インポートこのプロジェクト。

ファイル->インポート->既存のMavenプロジェクト->次へ->ブラウズ->フォルダspringを選択- spring -boot-application-run->フォルダを選択->完了

プロジェクトをインポートするには時間がかかります。プロジェクトを成功してインポートした後、IDEの パッケージエクスプローラー パートでそれを見ることができます。

私たちは自動的に作成された2つのファイルを見て、そのうちの1つは pom.xml 、もう一方は Application.java ファイル。

pom.xml ファイルがすべて含まれています。 依存関係アプリケーション名、Spring Bootバージョン、グループ名、アーティファクト、および他の プラグイン。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.2.BUILD-SNAPSHOT</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.w3codebox</groupId>
	<artifactId>spring-boot-application-run/artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-application-run/name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
	</dependency>
		  <dependency>
 <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</pluginRepository>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>
</project>

main このクラスはmain()メソッドを持つクラスであり、Spring ApplicationContextを起動します。このクラスはアプリケーションを実行するために実行されるクラスです。

SpringBootApplicationRun.java

package com.w3codebox;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootApplicationRun
{
    public static void main(String[] args) 
    {
        SpringApplication.run(SpringBootApplicationRun.class, args);
    }
}

ステップ10: コントローラーを作成します。以下の名前で作成しました。 HelloWorldController のコントローラー。

HelloWorldController.java

package com.w3codebox;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController 
{
    @RequestMapping("/)
    public String hello() 
    {
        return "Hello User";
    }
}

今、Spring Bootアプリケーションに関連するすべての必要なファイルが作成されました。

Spring Bootアプリケーションを実行します

Spring Bootアプリケーションを実行するには、メインアプリケーションファイルを開き、次のように実行します。 Javaアプリケーションのアイデンティティで実行します。

アプリケーションが成功して動作している場合、以下のようにコントロールパネルにメッセージが表示されます。

今、ブラウザを開き、URL http:を呼び出してください。//localhost:8080。これは、コントローラーに戻ったメッセージを表示しています。