关于java:Spring-Boot快速入门之三自启Bootstrapping

5次阅读

共计 4262 个字符,预计需要花费 11 分钟才能阅读完成。

【注】本文译自:https://www.tutorialspoint.com/spring_boot/spring_boot_bootstrapping.htm

本文解说 Spring Boot 利用如何执行自启(bootstrapping)。

Spring 初始化器(Intializer)

自启 Spring Boot 利用的一个形式是应用 Spring 初始化器(Intializer)。应用 Spring 初始化器,要拜访网页 www.start.spring.io 抉择所要用到的 Spring Boot 版本和平台。还要提供运行利用的组(Group)、工件(Artifact)和所需的依赖等信息。

上面的截图展现了增加 spring-boot-starter-web 依赖,编写 REST 端点的界面:

输出组、工件、依赖、构件我的项目、平台和版本之后,单击 Generate Project 按钮,相应的 zip 文件就会被下载下来并解压到本地目录。

上面解说如何应用 Maven 和 Gradle。

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>

Gradle

下载我的项目、解压文件后,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 会加到你的类门路中。例如:写一个 Rest 端点,你要在类门路中加上 spring-boot-starter-web 依赖。代码如下:

Maven 依赖

<dependencies>

   <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-web</artifactId>

   </dependency>

</dependencies>  

Gradle 依赖

dependencies {

  compile(‘org.springframework.boot:spring-boot-starter-web’)

}

主办法

在 Spring Boot 利用类中该当退出主办法。这个类要加上 @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;

@SpringBootApplicationpublic class DemoApplication {

    public static void main(String[] args) {

       SpringApplication.run(DemoApplication.class, args);

    }

}

写一个 Rest 端点(Endpoint)

要在 Spring Boot 利用的主类本身的文件中写一个简略的 Hello World Rest 端点,要做上面几步:

  • 首先,在类下面加上 @RestController 注解。
  • 当初,通过 @RequestMapping 注解表明申请 URI 办法。
  • 接下来,申请 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

让咱们在命令窗口用 Maven 和 Gradle 命令创立一个可执行的 JAR 文件来运行 Spring Boot 利用,如下所示:

应用 Maven 命令 mvn clean install,如下所示:

执行命令后,你会看到如下所示的 BUILD SUCCESS 的音讯:

应用 Gradle 命令 gradle clean build,如下所示:

执行命令后,你会看到 BUILD SUCCESSFUL 的音讯,如下所示:

以 Java 形式运行 Hello World

你能够在上面的目录下找到所创立的 JAR 文件。

对于 Maven,你能够在 target 目录下找到 JAR 文件,如下所示:

对于 Gradle,你能够在 build/libs 目录下找到 JAR 文件,如下所示:

上面,输出 java –jar <JARFILE> 命令运行 JAR 文件。对于下面的示例,JAR 文件名是 demo-0.0.1-SNAPSHOT.jar

运行 jar 文件,你会在控制台窗口看到相应的输入,如下所示:

当初,看一下控制台,Tomcat 启动了,端口是 8080 (http)。当初关上 web 浏览器敲入 URL http://localhost:8080/ 会看到上面的输入:

正文完
 0