(二)Spring Boot 官网文档学习之入门


原文:https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#getting-started-introducing-spring-boot


Spring Boot 是什么

Spring Boot 是基于 Spring 的应用程序,对 Spring 和 一些第三方库进行封装,便于程序员快速创建可以单独运行的项目;

官网重点标识:绝对没有代码生成,也不需要XML配置

从这,我们也可以看出 ,Spring Boot 是的一大目标就是解决以前 Spring 的那些繁琐的配置的 ;

就记住是一个更牛,更厉害的框架 ;


系统要求

我使用的 Spring Boot 版本是 Spring Boot 2.1.3.RELEASE

要求:

JDK8 +

Maven 3.3+

否则下面安装将会失败;


Servlet 容器

Spring Boot 内置了 Tomcat ,目前我使用的版本 2.1.3 CURRENT,内置的 Tomcat 版本是 9.0

当然也可以将 Spring Boot 项目打包发布到 Servlet 3.1+ 的服务器上 ;


Maven方式安装Spring Boot

pom 文件 :

<?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>

	< - 继承默认值为Spring Boot  - > 
	<parent> 
		<groupId> org.springframework.boot </groupId> 
		<artifactId> spring-boot-starter-parent </artifactId> 
		<version> 2.1.3.RELEASE < /version> 
	</parent>

	<!-- 添加Web应用程序的典型依赖项 - > 
	<dependencies> 
		<dependency> 
			<groupId> org.springframework.boot </groupId> 
			<artifactId> spring-boot-starter-web </artifactId> 
		</dependency> 
	</依赖>

	< - 打包为可执行jar ,可选插件,只是打包,不影响执行 - > 
	<build> 
		<plugins> 
			<plugin> 
				<groupId> org.springframework.boot </groupId> 
				<artifactId> spring-boot-maven-plugin </artifactId> 
			</plugin > 
		</plugins> 
	</ build>

</project>

这里使用了 parent 标签引入 spring-boot-starter-parent ,是使用 Spring Boot 的一个好方法,但是有时候,可能需要从不同的父级 pom 继承,或者需要使用公司的标准父级pom

或者不想使用 spring-boot-starter-parent 默认配置,它是一个特殊的启动器,,可以使用下面的方式进行特殊的配置 ;

添加如下依赖项,替换掉 parent 标签:

<dependencyManagement>
		<dependencies>
		<dependency>
			<!-- Import dependency management from Spring Boot -->
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.1.3.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

然后你想覆盖掉谁,你就在 <dependencies> 里面写出来,但是要写在 org.springframework.boot 前面,以进行属性的覆盖,比如下面想要使用Spring Data的另外一个版本:

 <dependencyManagement>
        <dependencies>
            <!-- 覆盖Spring Boot提供的Spring Data版本系列 -->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-releasetrain</artifactId>
                <version>Fowler-SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

编写第一个 Spring Boot 项目

pom文件,就使用上面提到的默认的就好了;

编写一个 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!";
	}

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

}

首先是 @RestController 注解,是一个构造型注解,用于告诉阅读代码的人以及 Spring 这是一个处理器;

@RequestMapping("/") 不是什么新鲜注解了,就不用说了;

重点说下 @EnableAutoConfiguration 这是一个新注解,它用于告诉 Spring Boot 进行猜测你想要什么类型的 Spring 配置,猜测的根据是你在 POM文件添加哪些 Jar包配置,比如我们这里仅仅添加了 spring-boot-starter-web

<!-- 添加Web应用程序的典型依赖项 - > 
	<dependencies> 
		<dependency> 
			<groupId> org.springframework.boot </groupId> 
			<artifactId> spring-boot-starter-web </artifactId> 
		</dependency> 
	</依赖>

那么,Spring Boot 将添加 Tomcat SpringMvc 进来,并进行相应的 Spring 配置,方便的很啊,这也是为啥 Spring Boot 不需要我们自己配置 Tomcat 的原因;

最后的 main() 方法,是启动入口,都是一些 Spring Boot 的非必需了解知识了,只要记住,传入两个参数,本class类 and main方法参数

最后启动,在浏览器里面输入 localhost:8080 即可看到 Hello World! ;

原文地址:https://www.cnblogs.com/young-youth/p/11665591.html