Follow the official documentation to learn SpringBoot 1: Preparation

 

Chapter 1

 

【Where to find it】

【Get help】

  • Check out How-to documents  for solutions to some of the most common problems.
  • Spring boot is built on top of many spring projects. There are a lot of technical guide documents on the spring.io site. If you are new to spring, you can try to follow the guided case study ( guides ).
  • Go to stackoverflow.com to ask questions, the official will pay attention to the questions with the [spring-boot] label.
  • Or file a bug at github.com/spring-projects/spring-boot/issues .

【System Requirements】

        spring boot 2.0.0.RELEASE requirements:

 

  • Java 8 and above
  • spring framework 5.0.4.RELEASE and above
  • Explicit support for building with Maven 3.2+ and Gradle 4

        servlet container support (based on servlet version 3.1)

  • Tomcat 8.5
  • Jetty 9.4
  • Undertow 1.4

      In addition, the spring boot project can also be deployed on any servlet 3.0+ compatible container.

 

 

 【Install spring boot】

        You can use spring boot either through Java development tools, or install and use it as a command-line tool. Of course, this requires a development environment based on JDK 1.8 and above.

Enter "java -version" in cmd to view the current system Java version.

       

Note that this and subsequent spring boot article code is based on the Maven demo.

 

        Spring boot's dependent groupId uses org.springframework.boot. The project's POM file will typically inherit from the spring-boot-starter-parent project, and when declaring other dependencies later, multiple dependencies including "Starters" will be used. Spring boot also provides a maven plugin for generating executable jar files .

 

The following is a typical spring boot project pom.xml file:

<?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.example</groupId>
	<artifactId>myproject</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!-- Inherit defaults from Spring Boot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>

	<!-- Add typical dependencies for a web application -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<!-- Package as an executable jar -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

 注意:继承 spring-boot-starter-parent 是一种使用 spring boot 很好的方式, 但是这种方式并不是任何时候都适用的。当你不想使用默认的设置时,可以点击这里查看可调整的解决方式。

 

【Hello World】

开发之前检查环境,在 cmd 中

// 检查 JDK 版本
java -verson
// 检查 Maven 版本
mvn -v

 一切就绪后,准备开始。

 

 

  • 最原始的方式创建 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>myproject</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>

	<!-- Additional lines to be added here... -->

</project>

 

进入该 pom 文件所在位置,shift + 右键,选择“在此处打开命令窗口(win 10 是 PowerShell)”,输入“mvn package”,如果提示 mvn 命令不可用,则需要配置 maven 环境变量。执行成功后会在当前文件夹中生成一个 target 文件。以上步骤为测试 pom 文件的正确性及 maven 环境的检测。

第二步,添加依赖:

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

可以使用“mvn dependency:tree”查看当前项目依赖。

 

 第三步,编写代码:

默认情况下,Maven 会编译 src/main/java 目录下的资源,因此需要创建这个目录结构,然后在 java 目录下创建名为 Example.java 的文件,内容如下:

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

	@RequestMapping("/")
	String home() {
		return "Hello World!";
	}

        // 这是 spring boot 程序的启动方式
	public static void main(String[] args) throws Exception {
		SpringApplication.run(Example.class, args);
	}

}

 在这个 java 文件中有几个稍显不同的注解:

@RestController:这个注解并不是 spring boot 特有的,它属于 spring mvc,它的作用相当于@Controller 和 @ResponseBody,即:将该 bean 纳入 spring 管理,并在请求返回时,将内容序列化为 json。

@EnableAutoConfiguration:使用这个注解,spring boot 会根据添加的依赖去“猜测”如何配置 spring,由于 spring-boot-starter-web 这个依赖已经添加了 Tomcat 和 Spring MVC,auto-comfiguration 会假设你正在开发 web 应用,并自动设置 spring。

 

main 方法也值得注意,可以看到,main 方法将启动程序的任务委托给了 SpringApplication 类的 run 方法。run 方法启动应用,开启 spring 和 Tomcat。通过传递 Example.class 参数告诉 SpringApplication (它是 spring 主要的组件之一)。main 方法中的 args 数组(命令行参数)也被传递。

 

第四步,运行示例。

 在 pom 文件所在目录下,输入命令:

 

$ mvn spring-boot:run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.0.0.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.222 seconds (JVM running for 6.514)
 Open a browser and visit localhost:8080, and the following output will appear:
Hello World!
 Press Ctrl + c and select "Y" to exit the program.  

[Create executable Jar]

An executable Jar (sometimes referred to as a fat Jar ("fat jar")) is an archive file (which can be simply understood as a tarball...) that contains compiled classes and all dependent jars needed to run the code.

 

There are many ways to create, the more common is to use the Maven plugin to generate. This requires adding the following to pom.xml:

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

 Note: The repackage goal has been excluded from the pom.xml file of the parent project of spring-boot-starter-parent. If the parent pom file is not used, you need to declare this configuration yourself. Click to view the details.

After saving, run the following command on the command line:

mvn package

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] .... ..
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject ---
[INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.0.RELEASE:repackage (default) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

 完成后,查看 target 目录,会看到 有名为 myproject-0.0.1-SNAPSHOT.jar 和 myproject-0.0.1-SNAPSHOT.jar.original 文件。后缀为 original 的文件是 maven 打包的原文件(在 spring boot 重新打包之前)。

此时,以下方式也可以启动项目:

java -jar target/myproject-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.0.0.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.536 seconds (JVM running for 2.864)

 

 OK,准备工作完成,热身结束了。

 

下一篇,使用 spring boot !

 

 

 

 

 

 

 

【去哪儿找】

【获得帮助】

【系统要求】

        spring boot 2.0.0.RELEASE 要求:

 

  • Java 8 及以上
  • spring framework 5.0.4.RELEASE 及以上
  • 明确支持使用 Maven 3.2+ 及 Gradle 4构建

        servlet 容器支持(基于 servlet 3.1版本)

  • Tomcat 8.5
  • Jetty 9.4
  • Undertow 1.4

      除此以外,也可以将 spring boot 项目部署在任何 兼容 servlet 3.0+ 的容器上。

 

 

 【安装 spring boot】

        既可以通过 Java 开发工具来使用 spring boot,也可以将其作为一种命令行工具安装和使用,当然,这都需要基于 JDK 1.8 及以上的开发环境。

在 cmd 输入 "java -version" 查看当前系统 Java 版本。

       

请注意,本文及之后的 spring boot 文章代码均基于 Maven 演示。

 

        spring boot 的依赖 groupId 均使用 org.springframework.boot。项目的 POM 文件会典型的继承于 spring-boot-starter-parent 项目,并且在后面声明其他依赖时,会使用到多个包含”Starters“的依赖。spring boot 也提供了一个用于生成可执行 jar 文件的 maven 插件

 

以下是一个典型的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>myproject</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!-- Inherit defaults from Spring Boot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>

	<!-- Add typical dependencies for a web application -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<!-- Package as an executable jar -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

 注意:继承 spring-boot-starter-parent 是一种使用 spring boot 很好的方式, 但是这种方式并不是任何时候都适用的。当你不想使用默认的设置时,可以点击这里查看可调整的解决方式。

 

【Hello World】

开发之前检查环境,在 cmd 中

// 检查 JDK 版本
java -verson
// 检查 Maven 版本
mvn -v

 一切就绪后,准备开始。

 

 

  • 最原始的方式创建 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>myproject</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>

	<!-- Additional lines to be added here... -->

</project>

 

进入该 pom 文件所在位置,shift + 右键,选择“在此处打开命令窗口(win 10 是 PowerShell)”,输入“mvn package”,如果提示 mvn 命令不可用,则需要配置 maven 环境变量。执行成功后会在当前文件夹中生成一个 target 文件。以上步骤为测试 pom 文件的正确性及 maven 环境的检测。

第二步,添加依赖:

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

可以使用“mvn dependency:tree”查看当前项目依赖。

 

 第三步,编写代码:

默认情况下,Maven 会编译 src/main/java 目录下的资源,因此需要创建这个目录结构,然后在 java 目录下创建名为 Example.java 的文件,内容如下:

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

	@RequestMapping("/")
	String home() {
		return "Hello World!";
	}

        // 这是 spring boot 程序的启动方式
	public static void main(String[] args) throws Exception {
		SpringApplication.run(Example.class, args);
	}

}

 在这个 java 文件中有几个稍显不同的注解:

@RestController:这个注解并不是 spring boot 特有的,它属于 spring mvc,它的作用相当于@Controller 和 @ResponseBody,即:将该 bean 纳入 spring 管理,并在请求返回时,将内容序列化为 json。

@EnableAutoConfiguration:使用这个注解,spring boot 会根据添加的依赖去“猜测”如何配置 spring,由于 spring-boot-starter-web 这个依赖已经添加了 Tomcat 和 Spring MVC,auto-comfiguration 会假设你正在开发 web 应用,并自动设置 spring。

 

main 方法也值得注意,可以看到,main 方法将启动程序的任务委托给了 SpringApplication 类的 run 方法。run 方法启动应用,开启 spring 和 Tomcat。通过传递 Example.class 参数告诉 SpringApplication (它是 spring 主要的组件之一)。main 方法中的 args 数组(命令行参数)也被传递。

 

第四步,运行示例。

 在 pom 文件所在目录下,输入命令:

 

$ mvn spring-boot:run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.0.0.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.222 seconds (JVM running for 6.514)
 Open a browser and visit localhost:8080, and the following output will appear:
Hello World!
 Press Ctrl + c and select "Y" to exit the program.  

[Create executable Jar]

An executable Jar (sometimes referred to as a fat Jar ("fat jar")) is an archive file (which can be simply understood as a tarball...) that contains compiled classes and all dependent jars needed to run the code.

 

There are many ways to create, the more common is to use the Maven plugin to generate. This requires adding the following to pom.xml:

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

 注意:spring-boot-starter-parent 父项目的 pom.xml 文件中已经排除了 repackage goal,如果没有使用 父 pom 文件,需要自行声明这个配置,点击查看详情。

保存后,在命令行运行如下命令:

mvn package

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] .... ..
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject ---
[INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.0.RELEASE:repackage (default) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

 完成后,查看 target 目录,会看到 有名为 myproject-0.0.1-SNAPSHOT.jar 和 myproject-0.0.1-SNAPSHOT.jar.original 文件。后缀为 original 的文件是 maven 打包的原文件(在 spring boot 重新打包之前)。

此时,以下方式也可以启动项目:

java -jar target/myproject-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.0.0.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.536 seconds (JVM running for 2.864)

 

 OK,准备工作完成,热身结束了。

 

下一篇,使用 spring boot !

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326027361&siteId=291194637