SpringBoot_01:快速入门
前言
黑马版本的springboot,地址:
https://www.bilibili.com/video/BV15b4y1a7yG?spm_id_from=333.999.0.0
-
基础篇
-
使用篇
-
原理篇
快速启动
- Spring的作用
用于简化Spring应用的初始搭建和开发过程。 - 新建空工程
- 创建模块
选择SpringInitiaizr
Group,Artifact用于创建maven工程的坐标。
package是默认创建一个包。
自己使用的时候报错,spring initializr error cannot download,查询博客后直到需要将默认的下载地址修改为 https://start.springboot.io/ - 指定Spring工程的用途
以mvc案例为例
注意勾选! - 创建成功
- 编写控制器(restful风格)
//Rest模式
@RestController//相当于@ResponseBody+@Controller
@RequestMapping("/books")//通用访问前缀
public class BookController {
@GetMapping
public String getById(){
System.out.println("spring running");
return "springboot is running";
}
}
- 运行xxxxApplication类
注意这里写有基本配置信息,使用的端口号是8080,服务器是tomcat(版本为9.0.63),工程的contextPaht为""(就是啥也不敲)。。。。。。
打开浏览器,访问http://localhost:8080/books/
那么我们发现和之前相比省略了,配置spring各种配置文件的配置过程。也不用自己配置tomcat服务器
注意基于idea开发springboot程序需要确保联网并且能够加载到程序框架结构
联网痛点解决
- 当使用的不是IDEA
打开spring官网,找到springboot
打开它,和自己创建类似
点击左下角按钮
打开对应的工作空间,把解压缩的文件扔进去
然后在idea导入模块即可 - spring官方网站被墙/访问太慢
不使用默认网址,输入:http://start.aliyun.com.
这样打开会多一些阿里自己做的东西。 - 计算机无法联网
1.创建时选择maven而不是spring initializr
2.cv以下maven依赖坐标
继承
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.创建一个application类(引导类)
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
//SpringApplication.run(Application.class);这样也行
}
}
文件夹痛点
纯手动启动其文件夹比较清爽
而其他方式文件夹比较繁琐
打开setting的file types忽略掉不想看的文件夹
支持*通配符,回车->apply
简单解析
那么它是怎么做到的呢,下面简单解析一下
parent----防止依赖冲突
两个相似的工程。写两个太麻烦,不如写一个pom.xml让其他来引入
再将版本管理和坐标管理分为两个pom
这样避免了调包问题(以前的时候多个三方技术,可能依赖于不同的包,他们之间存在冲突,而springboot经过测试,将融合性最好的统一了下(统一在了parent指定的project-parent:pom.xml中))
以后自己使用springboot时(因为有),引入三方插件的时候不用写版本了
parent仅定义,未使用
starter----简化依赖配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
发现没有写版本号,点击进去,发现,其版本号定义好了
依赖传递
所以在实际开发中不用写版本号了,但是有一些三方技术不写版本号会报错,这是因为spring的parentpom没收录?
引导类----初始化容器,扫描类所在包加载bean
引导类依靠@SpringBootApplication注解,打开看看
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration//配置类注解
@EnableAutoConfiguration
@ComponentScan(/**包扫描注解,啥也不写默认当前配置类所在包及其子包*/
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
点进去@SpringBootConfiguration
将bean类放在配置类的包外
- 总结
问题:
服务器谁启动的呢
内嵌tomcat
spring-boot-starter-web内有一个
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.7.0</version>
<scope>compile</scope>
</dependency>
点进去有一个内嵌的tomcat核心
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.63</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>tomcat-annotations-api</artifactId>
<groupId>org.apache.tomcat</groupId>
</exclusion>
</exclusions>
</dependency>
实际上是将tomcat的执行过程抽取为一个对象,将之交给spring管理
如果不想用tomcat,在工程的pom中
引入另一款服务器,jetty是一款轻量级的服务器,谷歌引擎已经全面切换为jetty
springboot内置了三款服务器