SpringBoot -01 介绍

相关文档 http://felord.cn/_doc/_springboot/2.1.5.RELEASE/_book/

1、SpringBoot介绍

SpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品:

2、SpringBoot好处

在学习SSM过程中,为了完成框架对接,要进行大量的配置,导入很多的包,如果包之间版本出现不兼容,包之间的依赖管理很麻烦,配置过程占用了过度的时间。

SpringBoot的设计就是为了解决这个问题的。

SpringBoot是搭建程序的脚手架,能够帮助我们导入很多的相关包。要使用相关组件只需要导入对应的starter脚手架即可。很多以前在xml配置内容通过写代码的方式实现。

3、SpringBoot特点

  • 为所有 Spring 的开发者提供一个非常快速的、广泛接受的入门体验
  • 开箱即用(启动器starter-其实就是SpringBoot提供的一个jar包),但通过自己设置参数(.properties),即可快速摆脱这种方式。
  • 提供了一些大型项目中常见的非功能性特性,如内嵌服务器、安全、指标,健康检测、外部化配置等
  • 绝对没有代码生成,也无需 XML 配置。

4、SpringBoot搭建Web项目体验

1)在磁盘上创建SpingBoot目录

2)通过Idea打开该目录

3)创建Maben类型的模块

4)pom.xml中配置JDK版本

1
2
3
4
<!--设置JDK版本-->
<properties>
    <java.version>1.8</java.version>
</properties>

5)pom.xml中添加父工程坐标

1
2
3
4
5
6
7
8
<!--
添加SpringBoot的基础工程为父工程,这个项目会把包之间的依赖关系处理好,不需要我们处理了
-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.5.RELEASE</version>
</parent>

SpringBoot提供了一个名为spring-boot-starter-parent的工程,内部包含了常用的依赖包(并非全部),我们的Maven项目要以这个项目为父工程,这样我们就不用操心依赖的版本问题。

6)pom.xml中引入Web启动器包

1
2
3
4
5
6
7
<dependencies>
    <!--SpringMVC Web的启动器,会自动引入很多的Maven包-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

这个依赖添加后,Maven加载结束后,会发现项目中引入了很多的包。

7)创建启动类

1
2
3
4
5
6
7
8
9
10
11
12
package rui;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/*启动类*/
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

springBoot的程序启动需要通过该启动类,虽然是Web项目,无需通过Tomcat运行,其内部内置了Tomcat的运行环境,直接运行main函数即可。

8)编写Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package rui.web;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping(value = "hello")
public class HelloController {
 
    @RequestMapping(value = "index")
    public String hello()
    {
        return "hello, spring boot!";
    }
}

9)添加yaml配置文件,是一直新的配置文件格式,替代了以前的application.properties,当然application.properties也是可以继续使用的。application.properties的优先级高。

10)运行程序并测试

 

 

 

 

5、SpringBoot热部署

1)pom.xml加入依赖包

1
2
3
4
5
6
<!--热启动的包-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

2)pom.xml增加build配置

1
2
3
4
5
6
7
8
9
10
11
12
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>
</build>

 

 3)application.yaml文件配置

1
2
3
4
5
#热部署
spring:
  devtools:
    restart:
      enabled: true

4)Idea配置

 

 

 

 

 

 修改文件保存后,控制台会自动输入如下的内容,类似自动启动了一下。

 



欢迎阅读,有错误请留言指正。
posted @   草莓爸  阅读(58)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示