Spring Boot 概念、创建和运行 · Spring Boot 的优点 · 启动第一个 Spring Boot · Spring Boot 的注意事项

news2024/12/23 19:15:28

  • 一、什么是 Spring Boot
  • 二、Spring Boot 优点
  • 三、Spring Boot 项目创建
  • 四、项目目录介绍和运行
    • 4.1 运行项目
    • 4.2 输出 Hello World
  • 五、注意事项
    • 5.1 包路径错误
    • 5.2 小结:约定大于配置
  • 六、总结


一、什么是 Spring Boot

Spring 的诞生是为了简化 Java 程序的开发的,而 Spring Boot 的诞生是为了简化 Spring 程序开发的。

Spring Boot 翻译过来就是 Spring 脚手架。
盖房子就是用脚手架,砌筑砖墙,浇筑混凝土,墙面抹灰,装饰和粉刷。简单来说,就是使用脚手架能更加快速的盖房子。
而 Spring Boot 就是 Spring 框架的脚手架,它就是为了快速开发 Spring 框架而诞生的。


二、Spring Boot 优点

  • 快速集成框架,Spring Boot 提供了添加依赖的功能,更加方便集成各种框架。
  • 内置运行容器,不需要配置 Tomcat 等 Web 容器,直接运行和部署项目。
  • 快速部署项目,不需要外部容器即可启动并且运行项目。
  • 抛弃了 XML 文件配置,使用注解和配置的方式进行开发。
  • 支持更多监控指标,能更好的了解项目的运行情况。

三、Spring Boot 项目创建

使用 IDEA 创建新项目

选择 Spring Initializr

选择配置

添加是需要的第三方框架

在这里插入图片描述

最后选择项目名称和保存路径

注意事项

第一次打开 Spring Boot 项目需要加载很久,因为当前 Spring Boot 框架并没有在自己的本地仓库。

为了快速的加载 Spring Boot 项目,建议配置自己的 Maven 为国内源:

setting.xml 文件的配置信息如下:

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
  <mirrors>
    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
  </mirrors>

  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>

配置好之后,选择右边的 Maven,再次载入。一般就不会报错了。


四、项目目录介绍和运行

新建的 Spring Boot 项目目录如下:

主要有 java 蓝色的源文件夹目录;resources 资源文件目录;test 测试文件目录。


4.1 运行项目

找到启动类,点击启动类的 main 方法就可以运行 Spring Boot 项目了。

启动成功,如图所示:


4.2 输出 Hello World

我们学习 JavaEE 就是用来实现 Web 项目或接口的,而之前 Spring 其实是一个普通 Java 项目,没办法直接和浏览器进行交互,所以接下来我们要用 Spring Boot 来实现和浏览器以及用户的交互。

在创建项目包路径下创建 UserController 文件,需要两个注解,自定义的方法上一定要加上 @RequestMapping 注解,实现代码如下:

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/sayhi")
    public String sayHi() {
        return "hi, spring boot.";
    }
}

启动项目,访问 http://localhost:8080/user/sayhi 最终效果如下:


五、注意事项

5.1 包路径错误

我们尝试将 controller 移动到其它包下

运行我们的项目,发现同一个网址无法访问了

这说明 Spring Boot 项目没有将对象注入到容器中。

只有将要注入的类和启动类放在同级目录下,启动项目才能访问到该资源。


5.2 小结:约定大于配置

以上情况,分应出 Spring Boot 的设计思想:约定大于配置

约定了需要注入容器的资源,都需要放在启动类的同级目录下。

对比 Spring 的项目,我们也会发现这一特点。Spring 中也是要配置 Bean 的扫描路径的(Spring Boot 不需要)


六、总结

Spring Boot 是为了快速开发 Spring 而诞生的,Spring Boot 具备:

1. 快速集成框架,Spring Boot 提供了启动添加依赖的功能。

在 pom.xml 文件中,右键找到 Generate 或者快捷键 Alt+Insert,可以快速选择并引入依赖。

2. 内置运行容器,无需配置 Tomcat 等 Web 容器,直接运行和部署项目。

根据上面的 hello world 程序,我们发现并没有配置 Tomcat 服务器就可以直接运行,很方便。

3. 快速部署项目,无需外部容器就可以启动并且运行项目。

通过 maven 的打包,可以放在容易目录下,通过 cmd 命令就可以运行项目(window 环境下)。

4. 完全抛弃繁琐的 XML 文件,使用注解和配置的方式进行开发。

我们回看 Spring Boot 创建出来的目录结构,并没有 XML 配置文件。

5. 支持更多的监控指标,更好的了解项目的运行情况等特点。

ps. 补充:

Spring Boot 可以使用 IDEA 或者网页创建,它的设计思想是约定大于配置,类上标注 @SpringBootApplication 就可以启动 Spring Boot 项目了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/87250.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

GAMES101-现代计算机图形学入门-闫令琪 课程笔记 - 汇总(上)

一些前言与感慨&#xff1a; 学了再多的AI&#xff0c;终究还是没有办法拒绝计算机图形学的魅力。当初就不该一招不慎&#xff0c;踏入AI的坑。 可惜当年在学校里学计算机图形学的时候&#xff0c;还没有闫令琪这么好的课程&#xff0c;当时学得一知半解&#xff0c;云里雾里…

极市直播回放第106期丨阿里达摩院:兼顾速度与精度的高效目标检测框架DAMO-YOLO

阿里巴巴达摩院智能计算实验室团队设计并开源了一款兼顾速度与精度的目标检测框架DAMO-YOLO&#xff0c;其性能超越了目前的一众YOLO系列方法&#xff0c;在实现精度SOTA的同时&#xff0c;保持了很高的推理速度。DAMO-YOLO是在YOLO框架基础上引入了MAE-NAS、efficient-RepGFPN…

cas:1628790-40-8|脂溶性Cyanine7-COOH|CY7-Carboxylic Acid

cas:1628790-40-8|脂溶性Cyanine7-COOH|CY7-Carboxylic Acid 名称&#xff1a;脂溶性Cyanine7-COOH|CY7-Carboxylic Acid cas:1628790-40-8 英文同义词: Cy7;Colpro;Prothil;R-13615;Cy7-COOH;CY7ACID;Cy7NHS;AY-62022;Cy7,>97%;Sulfo-Cyanine7 中文名称:磺基-CY7羧酸 …

【小甲鱼C语言】课后笔记第一章第四节——数据类型

目录 1、数据类型 2、short 和 long 3. sizeof 运算符 4. signed 和 unsigned 5、课后习题&#xff08;编程题&#xff09; 1、数据类型 在 C 语言里&#xff0c;所谓的数据类型就是坑的大小。我们说变量就是在内存里边挖一个坑&#xff0c;然后给这个坑命名。那么数据类型…

U盘格式化后能恢复数据吗?U盘删除的数据还能恢复吗

U盘格式化后能恢复数据吗&#xff1f;通常情况下&#xff0c;我们U盘里的数据丢失后&#xff0c;它们并没有立即消失&#xff0c;它们只是被系统做了一个标记&#xff0c;将数据存储的位置标记成可写入的状态&#xff0c;只有当新数据写入的时候&#xff0c;这个存储位置才会被…

USB转UART的桥接控制器 国产DPU02能不能软硬件兼容替换CP2102?

DPU02是一个高度集成的USB转UART的桥接控制器&#xff0c;可将RS-232设计更新为USB设计&#xff0c;并简化PCB组件空间。 该DPU02包括了一个USB 2.0全速功能控制器、USB收发器、振荡器、EEPROM和带有完整调制解调控制信号的异步串行数据总线&#xff08;UART&#xff09;控制器…

自动驾驶车辆安全保证机制-Mobileye的RSS模型

自动驾驶汽车(AV)将如何与人类司机安全地共享道路? 成功实现自动驾驶未来的最大威胁之一是对自动驾驶汽车安全驾驶的含义缺乏共识。只有当行业、政府和公众有一个共同的方式来理解和评估自动驾驶汽车(AV)的驾驶技能和安全,他们才会被信任,可以安全地与人类驾驶的车辆一起…

Go1.19.3 数组与切片原理简析

数组 Go语言数组&#xff0c;声明有如下几种方式&#xff1a; var arr1 [10]intarr1[0] 10000var arr2 [10]int{0:0,2:2}var arr3 [...]int{1,2,3}其中arr1只是进行声明&#xff0c;数组在声明时&#xff0c;内存空间已经被开辟过&#xff0c;所以可以赋值。arr2是声明的同…

javafx 编写管理页面 增删改查

注册界面&#xff1a;用户通过输入页面信息&#xff0c;点击注册&#xff0c;将数据存入数据库中。 <Tab text"用户注册"> <content> <AnchorPane minHeight"0.0" minWidth"0.0" prefHeight"761.0" prefWidth"819…

Vue的四个常用选项

文章目录前言一、四大选项简介二、filters&#xff08;过滤器&#xff09;三、computed&#xff08;计算属性&#xff09;四、methods&#xff08;方法&#xff09;五、watch&#xff08;观察&#xff09;总结:前言 本文讲解了vue.js中的四个常用选项&#xff0c;4个参数选项&…

数据结构——归并排序

坚持看完&#xff0c;结尾有思维导图总结 这里写目录标题归并排序的思路归并算法的图解具体程序对性质的分析归并排序的非递归版本总结归并排序的思路 首先第一个问题是&#xff0c;什么是归并排序&#xff1f; 官方的说法: 归并排序&#xff08;MERGE-SORT&#xff09;是建立…

pikachu靶场-7 不安全的文件下载和上传

不安全的文件下载和上传 不安全的文件下载 文件下载&#xff08;unsafedownload&#xff09;漏洞概述 很多网站都会提供文件下载功能&#xff0c;即用户可以通过点击下载链接&#xff0c;下载到链接所对应的文件。 但是&#xff0c;如果文件下载功能设计不当&#xff0c;则…

基于51单片机的数字频率计设计

仿真原理图&#xff1a; 程序运行图&#xff1a; 部分程序&#xff1a; #define LED_GLOBAL 1 #include "led.h" /******************************************************************************************* *函数名称&#xff1a;delay_us(uint us) *函数…

15.JavaScript 02

文章目录一、DOM简单学习&#xff1a;为了满足案例要求1、DOM知识点简单学习2、事件简单学习3、案例1&#xff1a;电灯开关二、BOM1、概念2、组成3、Window&#xff1a;窗口对象1. Window窗口对象知识点2. 案例2&#xff1a;轮播图4、Location&#xff1a;地址栏对象1. Locatio…

手写Spring5(资源加载Spring.xml解析和注册Bean对象)

文章目录目标设计思路项目结构一、实现1、资源加载接口定义和实现获取ClassPath下的文件信息获取指定文件路径的方式读取文件信息获取HTTP的方式读取云服务的文件2、包装资源加载器定义和实现-策略模式的体现包装资源加载器实现3、Bean定义读取接口4、Bean定义抽象类实现5、解析…

[激光原理与应用-53]:《激光焊接质量实时监测系统研究》-4-激光焊接系统软件设计

目录 前言&#xff1a; 4.1 操作系统和开发平台 4.1.1 Windows2000 操作系统概述 4.1.2 虚拟仪器开发平台软件 LabWindows/CVI 4.2 总体软件设计 4.2.1 数据采集程序 4.2.2 软件实现的功能 4.2.2.1 主机软件的数据采集 4.2.2.2 主机软件的数据分析&#xff08;核心&am…

暗棕红色粉末ICG-COOH, ICG Carboxlaic acid,181934-09-8,ICG和PEG链接可在体内长循环

英文名&#xff1a;ICG-COOH ICG Carboxlaic acid CAS No:181934-09-8 外观&#xff1a;暗棕红色粉末 溶解度&#xff1a;在水或甲醇中溶解 纯度&#xff1a;90% 结构式&#xff1a; 西安凯新近红外荧光染料Near IRDyes激发和发射波长和颜色图 ICG NHS ester的NHS可以和蛋白…

八、闭包高级、对象、构造函数、实例化

闭包高级、对象、构造函数、实例化 闭包高级 函数被定义时生成[[scope]]->生成scope chain -> scope chain中存着上级的环境。 函数被执行的前一刻&#xff08;预编译过程&#xff09;&#xff0c;生成自己的AO&#xff0c;排到scope chain的最顶端。 函数执行完毕的…

基于天鹰算法优化的lssvm回归预测-附代码

基于天鹰算法优化的lssvm回归预测 - 附代码 文章目录基于天鹰算法优化的lssvm回归预测 - 附代码1.数据集2.lssvm模型3.基于天鹰算法优化的LSSVM4.测试结果5.Matlab代码摘要&#xff1a;为了提高最小二乘支持向量机&#xff08;lssvm&#xff09;的回归预测准确率&#xff0c;对…

DFA的最小化

一、实验目的 1&#xff0e;熟练掌握DFA与NFA的定义与有关概念。 2&#xff0e;理解并掌握确定的有穷自动机的最小化等算法。 二、实验要求 输入&#xff1a;DFA 输出&#xff1a;最小化的DFA 三、实验过程 1&#xff0e;化简DFA关键在于把它的状态集分成一些两两互不相交…