- 一、SpringBoot2 基础入门
- 1.1. 什么是 SpringBoot
- 1.2. SpringBoot 优点
- 1.3. SpringBoot 缺点
- 1.4. SpringBoot(2.5.5) 入门
- 1.4.1. 系统要求
- 1.4.2. 配置 Maven
- 1.4.3. 创建 Hello World
- 1.4.3.1. 创建 Maven 工程并且引入依赖
- 1.4.3.2. 创建主程序
- 1.4.3.3. 创建业务类
- 1.4.3.4. 测试
- 1.4.3.5. 简化配置
- 1.4.3.6. 简化部署
- 1.5. SpringBoot 特点
- 1.5.1. 依赖管理
- 1.5.1.1. 父项目做依赖管理
- 1.5.1.2. 场景启动器
- 1.5.1.3. 版本仲裁
- 1.5.2. 自动配置
- 1.5.2.1. 引入 spring-boot-starter-web
- 1.5.2.2. 默认的包结构
- 1.5.2.3. 默认配置都拥有默认值
- 1.5.2.4. 按需加载自动配置项
1.2. SpringBoot 优点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
- 创建独立的应用
-
Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
- 内嵌 WEB 服务器
-
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
- 人称版本帝,迭代快
- 封装太深,入门容易,精通难
- Java 8 以上 并且兼容 Java 17
- Spring framework 5.3.10
- Maven 3.5 +
修改 Maven 的 settings.xml :
1.4.3. 创建 Hello Worldnexus-aliyun central Nexus aliyun. http://maven.aliyun.com/nexus/content/groups/public jdk-1.8 true 1.8 1.8 1.8 1.8
需求:浏览器发送 /Hello,响应 Hello Spring Boot 2
1.4.3.1. 创建 Maven 工程并且引入依赖pom.xml
1.4.3.2. 创建主程序4.0.0 cn.com.springboot.demo boot-01-helloworld 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.5.5 org.springframework.boot spring-boot-starter-web
dir:cn.com.springboot.demo.MainApp.java
package cn.com.springboot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
1.4.3.3. 创建业务类
dir:cn.com.springboot.demo.controller.HelloController.java
package cn.com.springboot.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String handler() {
return "Hello Spring Boot";
}
}
1.4.3.4. 测试
直接运行主程序。
. ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ ( ( )___ | '_ | '_| | '_ / _` | \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |___, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.5) 2021-09-29 00:18:47.607 INFO 8248 --- [ main] cn.com.springboot.demo.MainApp : Starting MainApp using Java 1.8.0_121 on DESKTOP-LT8H9RE with PID 8248 (F:IdeaProjectsboot-01-helloworldtargetclasses started by wwwjy in F:IdeaProjectsmy-app) 2021-09-29 00:18:47.617 INFO 8248 --- [ main] cn.com.springboot.demo.MainApp : No active profile set, falling back to default profiles: default 2021-09-29 00:18:48.255 INFO 8248 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2021-09-29 00:18:48.265 INFO 8248 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021-09-29 00:18:48.265 INFO 8248 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.53] 2021-09-29 00:18:48.328 INFO 8248 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021-09-29 00:18:48.328 INFO 8248 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 676 ms 2021-09-29 00:18:48.578 INFO 8248 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2021-09-29 00:18:48.586 INFO 8248 --- [ main] cn.com.springboot.demo.MainApp : Started MainApp in 1.252 seconds (JVM running for 1.529)1.4.3.5. 简化配置
dir:resources/application.properties
server.port=80811.4.3.6. 简化部署
生成可执行 jar ,引入以下的依赖:
...... org.springframework.boot spring-boot-maven-plugin 2.5.5
执行以下的命令:
mvn -clean mvn -package
进入 jar 所在路径,执行以下的命令:
F:IdeaProjectsboot-01-helloworldtarget>java -jar boot-01-helloworld-1.0-SNAPSHOT.jar . ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ ( ( )___ | '_ | '_| | '_ / _` | \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |___, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.5) 2021-09-29 00:25:34.990 INFO 2120 --- [ main] cn.com.springboot.demo.MainApp : Starting MainApp v1.0-SNAPSHOT using Java 1.8.0_121 on DESKTOP-LT8H9RE with PID 2120 (F:IdeaProjectsboot -01-helloworldtargetboot-01-helloworld-1.0-SNAPSHOT.jar started by wwwjy in F:IdeaProjectsboot-01-helloworldtarget) 2021-09-29 00:25:34.992 INFO 2120 --- [ main] cn.com.springboot.demo.MainApp : No active profile set, falling back to default profiles: default 2021-09-29 00:25:35.823 INFO 2120 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http) 2021-09-29 00:25:35.833 INFO 2120 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021-09-29 00:25:35.833 INFO 2120 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.53] 2021-09-29 00:25:35.881 INFO 2120 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021-09-29 00:25:35.881 INFO 2120 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 848 ms 2021-09-29 00:25:36.156 INFO 2120 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path '' 2021-09-29 00:25:36.164 INFO 2120 --- [ main] cn.com.springboot.demo.MainApp : Started MainApp in 1.504 seconds (JVM running for 1.831)1.5. SpringBoot 特点 1.5.1. 依赖管理 1.5.1.1. 父项目做依赖管理
所有的项目(这里指:用 Maven 构建的项目)的 pom.xml 文件都会引入以下的内容:
...... org.springframework.boot spring-boot-starter-parent 2.5.5
这将导致下边的所有依赖都不需要指定版本号。如引入 web 开发的依赖:
...... org.springframework.boot spring-boot-starter-web
进入 spring-boot-starter-parent 中(Ctrl + 左键),发现它还引用了父项目:
...... org.springframework.boot spring-boot-dependencies 2.5.5
进入 spring-boot-dependencies 中(Ctrl + 左键),它引入了所有的常规开发所需要的依赖。
1.5.1.2. 场景启动器- spring-boot-starter-*:是官方定义的 spring-boot 启动器,只要引入对应的 starter ,那么该启动器所对应的所有依赖都会被自动引入
- 自动引入原理:Maven 的依赖传递
- *-spring-boot-starter:是第三方提供的启动器
- 所有启动器最底层都依赖,如下
...1.5.1.3. 版本仲裁... org.springframework.boot spring-boot-starter 2.5.5 compile
因为在 spring-boot-dependencies 中配置了当前 spring-boot 版本(2.5.5)所支持依赖版本。
如:
...5.16.3 2.7.7 1.9.91 2.17.0 1.9.7 3.19.0 4.0.6 4.0.3 ...3.2.0 1.10.22 2.9.2 4.11.3 1.5.1 1.15 2.8.0 3.12.0 1.6 2.9.0 ...8.0.26 ...
所以引入的依赖都不需要写版本号,这就是 spring-boot 的自动版本仲裁机制。
如果不想使用 spring-boot 自动仲裁的版本
在当前项目(开发项目中)的 pom.xml 中,指定自己的依赖即可,如:
...... 6.0.6
一定要和 spring-boot-dependencies 中标签名相同
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cMFJSSTK-1632929238416)(C:UserswwwjyAppDataRoamingTyporatypora-user-imagesimage-20210929230430351.png)]
1.5.2. 自动配置 1.5.2.1. 引入 spring-boot-starter-web自动配置好了以下的内容:
- Tomcat
- SpringMVC
...1.5.2.2. 默认的包结构org.springframework.boot spring-boot-starter-tomcat 2.5.5 compile org.springframework spring-web 5.3.10 compile ... org.springframework spring-webmvc 5.3.10 compile
主程序所在的包及其子包都会被扫描到。
示例:在主程序所在包外新建 controller
主程序
package cn.com.springboot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
WorldController
package cn.com.springboot;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WorldController {
@RequestMapping("/world")
public String handler() {
return "Hello World.";
}
}
运行结果
1.5.2.3. 默认配置都拥有默认值- 所有的配置最终都映射到一个具体的 java 类上
- 配置文件的值都会绑定到对应的类上,这个类会在容器中创建对象
- 引入哪个启动器,才会启用这个启动器的自动配置
- spring-boot 的所有自动配置都绑在 spring-boot-autoconfigure 包内



