SpringBoot简介
概述
SpringBoot是Spring生态的一个模块,现在有2个版本,但是SpringBoot2版本的更新幅度非常大,而且小版本更新也非常快!
在spring官网下划有2段话,就能看出Spring Boot的地位
With Spring Boot in your app, just a few lines of code is all you need to start building services like a boss.
--在应用程序中使用Spring Boot,仅需要几行代码就可以构建服务
Originally [Netflix's Java] libraries and frameworks were built in-house. I'm very proud to say, as of early 2019, we've moved our platform almost entirely over to Spring Boot.”
作用
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
--能快速创建出生产级别的Spring应用
特性
- Create stand-alone Spring applications
创建独立的Spring应用程序
- Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)
- Provide opinionated 'starter' dependencies to simplify your build configuration
自动starter依赖,简化构建配置
- Automatically configure Spring and 3rd party libraries whenever possible
自动配置Spring以及第三方功能
- Provide production-ready features such as metrics, health checks, and externalized configuration
提供生产级别的监控、健康检查及外部化配置
- Absolutely no code generation and no requirement for XML configuration
无代码生成、无需编写XML
参考地址
SpringBoot官网地址:https://spring.io/projects/spring-boot
SpringBoot版本说明:https://github.com/spring-projects/spring-boot/wiki#release-notes
SpringBoot中文参考:https://www.springcloud.cc/spring-boot.html#boot-documentation-about
SpringBoot结构
SpringBoot入门
SpringBoot入门可以参考地址:
https://docs.spring.io/spring-boot/docs/2.5.2/reference/html/getting-started.html#getting-started
系统要求
工具 | 说明 |
---|---|
JDK | java8,兼容Java15 |
Maven | 3.5+ |
或者Gradle | 6(6.8或更高版本) |
Spring Boot | 2.5.2 |
创建SpringBoot项目有3种方式
- 创建Maven工程,然后手动添加依赖更改目录
- spring官网提供的Spring Initailizr,创建步骤:https://spring.io/quickstart,创建地址:https://start.spring.io/
- 使用开发工具IDEA结合项目初始化向导(Spring Initailizr)
一、使用普通Maven工程方式
创建Maven工程
引入依赖
<!--继承springboot的⽗项⽬-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
</parent>
<dependencies>
<!--引⼊springboot的web⽀持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
二、使用spring官网提供的Spring Initailizr
项目下载完之后,先进行解压,然后在IDE中打开即可!
三、使用开发工具IDEA结合项目初始化向导(Spring Initailizr)
生成的项目目录与第二种方式相同;这两种方式好处就是自动生成了配置文件、启动类,无需手动添加SpringBoot相关的依赖仅此而已!
创建主程序
package com.ty;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
编写业务
package com.ty.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello SpringBoot!";
}
}
运行程序
执行主程序,浏览器发送:http://localhost:8080/hello
配置文件
web程序中有各种配置比如端口、项目路径等,SpringBoot有默认值,但是也可以创建配置文件进行更改
配置的参考地址:https://docs.spring.io/spring-boot/docs/2.5.2/reference/html/application-properties.html#application-properties
创建application.properties文件
server.port=9999
server.servlet.context-path=/springboot
此时浏览器发送的地址为:http://localhost:9999/springboot/hello
简化部署
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后就可以把项目打成jar包,之后直接在目标的服务器执行即可!java -jar 项目.jar
入门程序的说明
看一下继承springboot的⽗项⽬pom依赖
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
点进去可见还有一个父项目的依赖
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.2</version>
接着点进去就可以看见里面有各种依赖,在里面搜索spring-boot-starter-web,可以看见
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.2</version>
</dependency>
ps:看见各种的spring-boot-starter-*,这是SpringBoot内置的启动器,表示此场景下所需的依赖都进行导入
starter参考地址:
https://docs.spring.io/spring-boot/docs/2.5.2/reference/html/using.html#using.build-systems.starters
看一下引入spring-boot-starter-web之后的jar包继承图
总结一下SpringBoot的自动配置特点
- 内嵌tomcat服务器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.5.2</version>
</dependency>
- SpringBoot强大的地方就在于它的场景starter,引入对应的场景starter就具备了其对应的功能以及默认配置;
比如上面我们引入了web的starter就具有了SpringMVC的功能,也就是SpringMVC的常用组件默认也都能做
- 默认的包结构:主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来,依靠的就是@SpringBootApplication注解
另外都说SpringBoot约定优于配置,默认的包结构是其中一点;默认的starter配置是一点;另外在上面用第二种和第三种方式创建SpringBoot项目时,会发现多了两个目录,这两个目录可不是白给了,是有用的,先留个玄机!
主启动类说明:
/**
* 全局⼊⼝类 有且只能有⼀个
*
* @SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan
* @SpringBootConfiguration:标识这是⼀个springboot的配置类
* @EnableAutoConfiguration:⾃动与项⽬中集成的第三⽅技术进⾏集成
* @ComponentScan:扫描⼊⼝类所在⼦包以及⼦包后代包中注解
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
/**
*第一个参数为要加载的主要来源,第二个参数为应用程序参数(通常从 Java 主方法传递)
*/
SpringApplication.run(MainApplication.class, args);
}
}