美文网首页
SpringBoot

SpringBoot

作者: 欧子有话说_ | 来源:发表于2022-08-11 10:19 被阅读0次

1、SpringBoot简介

Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

SpringBoot官网: https://spring.io/projects/spring-boot/

Spring Boot 可以轻松创建可以“直接运行”的独立的、生产级的基于 Spring 的应用程序。

Spring Boot 基于Spring开发,Spring Boot 本身并不提供Spring 框架的核心特性以及扩展功能,只是用于快速.敏捷的开发新一代基于Spring 框架的应用程序。 Spring Boot以约定大于配置的核心思想,默认帮我们进行了很多设置,多数Spring Boot应用只需要很少的Spring配置,几乎可以零配置开箱即用。

image.png

2、优势

  • 创建独立的 Spring 应用程序

  • 直接嵌入 Tomcat、Jetty 或 Undertow(无需部署 WAR 文件)

  • 提供自以为是的“入门”依赖项以简化您的构建配置

  • 尽可能自动配置 Spring 和 3rd 方库

  • 提供生产就绪功能,例如指标、健康检查和外部化配置

  • 完全无需代码生成,无需 XML 配置

3、快速入门

1、新建工程

image.png

2、勾选SpringWeb

image.png

经过以上步骤后就创建了如下结构的模块,它会帮我们自动生成一个 Application 类

image.png
  1. 在创建好的工程中不需要创建配置类

  2. 创建好的项目会自动生成其他的一些文件,而这些文件目前对我们来说没有任何作用,所以可以将这些文件删除。

可以删除的目录和文件如下: .mvn .gitignore HELP.md mvnw mvnw.cmd

3、创建Controller

<pre class="public-DraftStyleDefault-pre" data-offset-key="2b4cv-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="fmb5u" data-offset-key="2b4cv-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

package com.example.xmp.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

@RequestMapping("/")
public String index() {
    return "Greetings from Spring Boot!";
}

}

</pre>

</pre>

4、启动服务器

运行 SpringBoot 工程不需要使用本地的 Tomcat 和 插件,只运行项目包下的 Application类,我们就可以在控制台看出如下信息:

image.png

使用 Apifox或者Postman工具来测试我们的程序

image.png

通过上面的入门案例我们可以看到使用 SpringBoot 进行开发,使整个开发变得很简单。

pom.xml 配置文件中的内容详解

<pre class="public-DraftStyleDefault-pre" data-offset-key="811h7-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="fmb5u" data-offset-key="811h7-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>xmp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xmp</name>
<description>xmp</description>

<properties>
    <!--JDK 的版本-->
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>

<dependencies>
    <!--该依赖就是我们在创建 SpringBoot 工程勾选的那个 Spring Web 产生的-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <!--这个插件是在打包时需要的-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

</pre>

</pre>

我们代码之所以能简化,就是因为指定的父工程和 Spring Web 依赖实现的。

4、Spring和SpringBoot对比

|

类/配置文件

|

Spring

|

SpringBoot

|
|

pom.xml

|

手工制作

|

勾选添加

|
|

Web3.0配置类

|

手工制作

|

|
|

Spring/SpringMVC配置类

|

手工制作

|

|
|

控制器Controller

|

手工制作

|

手工制作

|

  • 坐标 Spring 程序中的坐标需要自己编写,而且坐标非常多 SpringBoot 程序中的坐标是我们在创建工程时进行勾选自动生成的

  • web3.0配置类 Spring 程序需要自己编写这个配置类。这个配置类大家之前编写过,肯定感觉很复杂 SpringBoot 程序不需要我们自己书写

  • 配置类 Spring/SpringMVC 程序的配置类需要自己书写。而 SpringBoot 程序则不需要书写。

5、在官网构建工程

官网地址如下:

<pre class="public-DraftStyleDefault-pre" data-offset-key="42m8p-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="fmb5u" data-offset-key="42m8p-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; word-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

https://spring.io/projects/spring-boot

</pre>

</pre>

进入到 SpringBoot 官网后拖到最下方就可以看到如下内容

image.png

然后点击 Spring Initializr 超链接就会跳转到如下页面

image.png

然后操作与在Idea中类似,创建完成后会生成一个文件夹压缩包,打开即可,与Idea创建的一模一样。

6、问题

  • Idea创建SpringBoot一直转圈圈

  • 打不开Spring官网

可通过阿里云镜像创建: https://start.aliyun.com/

image.png

相关文章

网友评论

      本文标题:SpringBoot

      本文链接:https://www.haomeiwen.com/subject/fqbzwrtx.html