# springbootlearn **Repository Path**: SpringCloud-Learn/springbootlearn ## Basic Information - **Project Name**: springbootlearn - **Description**: springboot编程思想(核心篇) - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-06-15 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # springbootlearn #### 介绍 springboot编程思想(核心篇) #### springboot 特性 1. 创建独立的Spring应用 2. 直接嵌入Tomcat,Jetty,Undertow,不需要部署war包 3. 提供固化的starter依赖,简化构建配置 4. 当条件满足时自动装配Spring或第三方类库 5. 提供运维(Production-Ready)特性,比如指标信息(Metrics),健康检查和外部化配置等 6. 无代码生成,无xml配置 #### 理解独立的Spring应用 springboot 可以支持web应用和非web应用(比如命令行或者图形界面). 非web应用主要用于服务提供,调度任务和消息处理场景. [springboot官网文档](https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/htmlsingle/#boot-features-command-line-runner) ``` java /** *If you need to run some specific code once the SpringApplication has started, you can *implement the ApplicationRunner or CommandLineRunner interfaces. Both interfaces work in *the same way and offer a single run method, which is called just before *SpringApplication.run(…​) completes. * *The CommandLineRunner interfaces provides access to application arguments as a simple *string array, whereas the ApplicationRunner uses the ApplicationArguments interface *discussed earlier. The following example shows a CommandLineRunner with a run method: *If several CommandLineRunner or ApplicationRunner beans are defined that must be called in *a specific order, you can additionally implement the org.springframework.core.Ordered *interface or use the org.springframework.core.annotation.Order annotation. */ import org.springframework.boot.*; import org.springframework.stereotype.*; @Component public class MyBean implements CommandLineRunner { public void run(String... args) { // Do something... } } ``` web应用在spring1.x中只支持servlet容器实现,比如传统的servlet和spring mvc.2.x的版本支持了Reactive Web容器实现(即webflux). ##### 创建Spring Boot应用可执行jar 创建可执行jar(fast jar)的前提是需要添加`spring-boot-maven-plugin`到pom.xml中.通过`https://start.spring.io`构建的spring boot应用默认会加载该插件. 通过执行`java -jar xxx.jar`的方式可以启动fast jar.(生产环境) 通过执行`mvn spring-boot:run`的方式可以在源码上进行启动(开发环境) ``` java -jar 启动一个jar的引导启动类必须配置在MANIFEST.MF资源的Main-Class属性中. 通过解压springboot包可以看到springboot包的配置为: Manifest-Version: 1.0 Implementation-Title: firstlesson Implementation-Version: 0.0.1-SNAPSHOT Start-Class: com.gitee.springboot.firstlesson.FirstlessonApplication Spring-Boot-Classes: BOOT-INF/classes/ Spring-Boot-Lib: BOOT-INF/lib/ Build-Jdk-Spec: 1.8 Spring-Boot-Version: 2.1.5.RELEASE Created-By: Maven Archiver 3.4.0 Main-Class: org.springframework.boot.loader.JarLauncher war包的启动类为 Main-Class:org.springframework.boot.loader.WarLaunchercd ``` ![Launcher依赖关系](imgs/image-20190615113511744.png) SpringBoot 打出来的war包比传统的Servlet项目多了一个WEB-INF/lib-provided,该目录存放的是maven仓库中`orovided`的jar文件. #### 理解固化的Maven依赖 `spring-boot-starter-parent`的父依赖为`spring-boot-dependencies`. ``` xml org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE ``` 与 ``` xml org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE pom import ``` 在使用``的时候要注意: 如果打war包需要引入 ``` xml org.apache.maven.plugins maven-war-plugin 3.1.0 ``` 并且默认情况下 `spring-boot-maven-plugin`是不生效的,需要指定`repackage`和``. ``` xml org.springframework.boot spring-boot-maven-plugin 2.1.5.RELEASE repackage ``` 此处需要理解`maven-war-plugin`在新旧版本的差异以及maven依赖管理的特性. 如果通过``管理springboot依赖默认使用的`maven-war-plugin`版本为2.2,在该版本中的默认打包规则是必须存在Web应用部署描述文件`web.xml`,而在3.1.0的版本中调整了该默认行为. 单独引入`spring-boot-maven-plugin`插件时,需要配置`repackage`的``元素,否则不会添加springboot依赖,进而无法引导当前应用. #### 理解嵌入式Web容器 | Servlet规范 | Tomcat | Jetty | Undertow | | ----------- | ------ | ----- | -------- | | 4.0 | 9.x | 9.x | 2.x | | 3.1 | 8.x | 8.x | 1.x | | 3.0 | 7.x | 7.x | N/A | | 2.5 | 6.x | 6.x | N/A | > servlet4.0规范和 HTTP/2.0协议 Tomcat Jetty Undertow #### 理解自动装配