英文链接源链接 [20759]
contenteditable="true"

Spring Boot - Bootstrapping 介绍

本章将向您介绍如何在Spring Boot应用程序上执行引导。

春季初始化

One of the ways to Bootstrapping a Spring Boot application is by using 春季初始化. To do this, you will have to visit the 春季初始化 web page www.start.spring.io and choose your Build, Spring Boot Version and platform. Also, you need to provide a Group, Artifact and required dependencies to run the application.

观察以下屏幕快照,该屏幕快照显示了一个示例,其中我们添加了 spring-boot-starter-web 依赖关系以编写REST端点。

春季初始化

提供组,工件,依赖项,构建项目,平台和版本后,单击生成项目按钮。该zip文件将下载并提取文件。

This section explains you the examples by using both Maven and 摇篮.

Maven

下载项目后,解压缩文件。现在,您的 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 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>demo</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.8.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <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>
      </dependency>
   </dependencies>
   
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
   
</project>

摇篮

下载项目后,解压缩文件。现在,您的 build.gradle 文件如下所示-

buildscript {
   ext {
      springBootVersion = '1.5.8.RELEASE'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}
dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}

类路径依赖

Spring Boot提供了许多 Starters 来将jar添加到我们的类路径中。例如,要编写一个Rest Endpoint,我们需要在类路径中添加 spring-boot-starter-web 依赖项。遵守下面显示的代码,以更好地理解-

Maven依赖

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>   

摇篮 dependency

dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
}

主要方法

主要方法应该是编写Spring Boot Application类。此类应使用 @SpringBootApplication 进行注释。这是启动Spring Boot应用程序的入口。您可以使用默认包在 src/java/main 目录下找到主类文件。

在此示例中,主类文件位于 src/java/main 目录中,默认包为 com.tutorialspoint.demo 。观察此处显示的代码以更好地理解-

package com.tutorialspoint.demo;

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

@SpringBootApplication
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

写一个休息端点

要在Spring Boot Application主类文件本身中编写一个简单的Hello World Rest Endpoint,请按照以下步骤操作-

  • 首先,在类顶部添加 @RestController 注释。

  • 现在,编写带有 @RequestMapping 批注的Request URI方法。

  • 然后,Request URI方法应返回 Hello World 字符串。

现在,您的主Spring Boot Application类文件将如下所示,如下所示:

package com.tutorialspoint.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController

public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   @RequestMapping(value = "/")
   public String hello() {
      return "Hello World";
   }
}

创建一个可执行的JAR

Let us create an executable JAR file to run the Spring Boot application by using Maven and 摇篮 commands in the command prompt as shown below −

使用Maven命令mvn clean install,如下所示-

Command MVN Clean Install

执行命令后,您可以在命令提示符下看到 BUILD SUCCESS 消息,如下所示-

BUILD SUCCESS Message

Use the 摇篮 command gradle clean build as shown below −

摇篮 Clean Build

执行命令后,您可以在命令提示符下看到 BUILD SUCCESSFUL 消息,如下所示-

BUILD SUCCESSFUL Message in Command Prompt

使用Java运行Hello World

创建可执行的JAR文件后,可以在以下目录下找到它。

对于Maven,您可以在目标目录下找到JAR文件,如下所示-

Maven JAR File Target Directory

For 摇篮, you can find the JAR file under the build/libs directory as shown below −

JAR File Under Build Libs Directory

Now, run the JAR file by using the command java –jar <JARFILE>. Observe that in the above example, the JAR file is named demo-0.0.1-SNAPSHOT.jar

JAR File Named Demo SNAPSHOT

运行jar文件后,您可以在控制台窗口中看到输出,如下所示-

Output in Console Window

现在,查看控制台,Tomcat在端口8080(http)上启动。现在,转到Web浏览器并点击URL http://localhost:8080/,您将看到如下所示的输出-

Tomcat Started on Port 8080 (http).
点我分享笔记