原文: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! ;

(二)Spring Boot 官网文档学习之入门的更多相关文章

  1. (五)Spring Boot官网文档学习

    文章目录 SpringApplication SpringApplication 事件 `ApplicationContext ` 类型 访问传递给 `SpringApplication` 的参数 A ...

  2. (四)Spring Boot官网文档学习

    文章目录 关于默认包的问题 加载启动类 配置 Bean管理和依赖注入 @SpringBootApplication Developer Tools 关于 Developer Tools 的一些细节 原 ...

  3. (三)Spring Boot 官网文档学习之默认配置

    文章目录 继承 `spring-boot-starter-parent` 覆盖默认配置 启动器 原文地址:https://docs.spring.io/spring-boot/docs/2.1.3.R ...

  4. Spring Security 官网文档学习

    文章目录 通过`maven`向普通的`WEB`项目中引入`spring security` 配置 `spring security` `configure(HttpSecurity)` 方法 自定义U ...

  5. Spring3.0官网文档学习笔记(二)

    1.3 使用场景 典型的成熟的spring web应用 spring使用第三方框架作为中间层 远程使用场景 EJB包装 1.3.1 依赖管理.命名规则(包)     spring-*.jar *号代表 ...

  6. Spring3.0官网文档学习笔记(八)--3.4.3~3.4.6

    3.4.3 使用depends-on     使用depends-on能够强制使一个或多个beans先初始化,之后再对这个bean进行初始化. 多个bean之间用","." ...

  7. Spring3.0官网文档学习笔记(一)

    Part 1 Spring框架概述 Spring是模块化的,在应用中仅仅须要引入你所须要用到的模块的jar包,其余的jar包不用引入. spring框架支持声明式的事务管理,通过RMI或web ser ...

  8. Spring3.0官网文档学习笔记(七)--3.4.2

    3.4.2 依赖与配置的细节     3.4.2.1  Straight values (primitives, Strings, and so on)     JavaBeans PropertyE ...

  9. Spring3.0官网文档学习笔记(四)--3.1~3.2.3

    3.1 Spring IoC容器与Beans简单介绍     BeanFactory接口提供对随意对象的配置:     ApplicationContext是BeanFactory的子接口.整合了Sp ...

随机推荐

  1. Linux压缩和解压类指令

    一.gzip / gunzip  指令 gzip 用于压缩文件,gunzip 用于解压文件. 基本语法gizp  文件 (功能描述:只能将文件压缩为*.gz文件)gunzip  文件.gz (功能描述 ...

  2. UML图中时序图的基本用法

    快速阅读 序列图主要用来更直观的表现各个对象交互的时间顺序,将体现的重点放在 以时间为参照,各个对象发送.接收消息,处理消息,返回消息的 时间流程顺序,也称为时序图. 里面用到的基本元素如下: 角色- ...

  3. GRU和LSTM比较

    比较: https://www.jianshu.com/p/3774d46b665e https://blog.csdn.net/sinat_33741547/article/details/8282 ...

  4. CMU Database Systems - Indexes

    这章主要描述索引,即通过什么样的数据结构可以更加快速的查询到数据 介绍Hash Tables,B+tree,SkipList 以及索引的并行访问 Hash Tables hash tables可以实现 ...

  5. ISO/IEC 9899:2011 条款6.2——概念

    6.2 概念 6.2.1 标识符的作用域 6.2.2 标识符的连接 6.2.3 标识符的名字空间 6.2.4 对象的存储持久性 6.2.5 类型 6.2.6 类型的表示 6.2.7 兼容类型与组合类型 ...

  6. ISO/IEC 9899:2011 条款6.7.6——声明符

    6.7.6 声明符 语法 1.declarator: pointeropt    direct-declarator direct-declarator: identifier (    declar ...

  7. 我的求职之路:9个offer,12家公司,35场面试,最终谷歌【转载】

    作者:Luc(写于2012年) 一.简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也作为之前三个半月的求职的回顾. 首先说说我拿到的offer情况: 微软,3面->终面,搞定 百 ...

  8. Windows下MariaDB数据库安装图文教程

    MariaDB是基于MySQL的开源数据库,兼容MySQL,现有的MySQL数据库可以迁移到MariaDB中使用   说明: MariaDB是基于MySQL的开源数据库,兼容MySQL,现有的MySQ ...

  9. jQuery根据style筛选元素

    <div style="display:block;"> <input/> </div> <div style="display ...

  10. 使用postman调用webservice接口

    通过wsdl查看接口地址: 填入xml报文: 配置相应的headers: