“Springboot”的版本间的差异
第268行: | 第268行: | ||
若要创建可执行 jar,我们需要将 spring-boot-maven-plugin添加到 我们的pom.xml 。 | 若要创建可执行 jar,我们需要将 spring-boot-maven-plugin添加到 我们的pom.xml 。 | ||
<blockquote>xml | |||
<build> | |||
<plugins> | |||
<plugin> | |||
<groupId>org.springframework.boot</groupId> | |||
<artifactId>spring-boot-maven-plugin</artifactId> | |||
</plugin> | |||
</plugins> | |||
</build> | |||
</blockquote> | |||
===关于starter=== | ===关于starter=== | ||
===关于热部署=== | ===关于热部署=== | ||
===执行器:生产就绪功能=== | ===执行器:生产就绪功能=== |
2021年8月26日 (四) 07:57的版本
Spring Boot 概述
Spring Boot 是什么
Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。Spring Boot采用约定优于配置的方式,大量的减少了配置文件的使用。
一般我们把Spring Boot称为搭建程序的 脚手架 或者说是 便捷搭建基于Spring的工程脚手架 。主要的作用就是帮助开发人员快速构建Spring项目,并且尽可能的减少一切xml配置,做到开箱即用,快速上手,让开发人员关注业务而非配置。Spring Boot简化了基于Spring的应用开发,只需要“run” 就能创建一个独立的、生产级别的Spring应用。Spring Boot为Spring平台及第三方库提供了开箱即用的设置,这样我们就可以简单的开始。多数Spring Boot应用只需要很少的Spring配置。
Spring Boot 的特性
- 创建独立的Spring应用
- 直接嵌入 Tomcat、Jetty 或Undertow(无需部署 WAR 文件)
- 提供固定"启动器"依赖关系,以简化构建配置
- 尽可能自动配置Spring和第三方库
- 提供生产就绪型功能,如指标、运行状况检查和外部配置
- 绝对没有代码生成,也没有 XML 配置要求
为什么要用Spring Boot
Java(Spring)一直被人诟病的一点就是臃肿、麻烦,主要原因是以下两点:
- 复杂的配置
- 混乱的依赖管理
通过上小节Spring Boot的特性,我们可以看到,Spring Boot就是用来解决这些问题的。自动配置,可以节省我们大量的配置问题,Spring Boot为Spring和其他有需要的第三方依赖提供了默认配置,我们甚至可以不做任何配置就可以启动和运行项目。依赖关系,Spring Boot通过starter引入第三方依赖,其中的所有依赖关系都是经过测试和验证的,再加上Maven等依赖管理工具避免了依赖冲突。
Spring Boot 快速启动
启动一个提供Http请求的Restful的服务接口,请求地址为http://localhost:8080/hello 返回 Hello Spring Boot!
环境准备:
- JDK1.8.X
- Maven 3.6.X
- Eclipse 4.x/STS 4.x(Spring Tools Suite)/Idea
- Spring Boot 2.3.X
- Spring 5
创建项目工程
1.创建一个Spring Starter Project
2.填写相关信息
Name: HelloSpringBoot Java Version:8 Group: com.kayak Package: com.kayak.springboot.demo
3.选择版本,配置starter(启动器)
Spring Boot Version: 2.3.X Dependencies: Spring Web
完整的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent><groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath />
</parent>
<groupId>com.example</groupId>
<artifactId>HelloSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloSpringBoot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version></properties>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类
Spring Boot 项目通过main函数即可启动
package com.kayak.springboot.demo;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class HelloSpringBootApplication {
public static void main(String[] args) { SpringApplication.run(HelloSpringBootApplication.class, args); }
}
编写 Controller
package com.kayak.springboot.demo.controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello") public String hello() { return "Hello Spring Boot!"; }
}
使用启动类启动运行
2020-09-16 16:48:10.037 INFO 17152 --- [ main] c.k.s.demo.HelloSpringBootApplication : Starting HelloSpringBootApplication on DESKTOP-R2M87Q2 with PID 17152 (D:\work2\HelloSpringBoot\target\classes started by younge in D:\work2\HelloSpringBoot) 2020-09-16 16:48:10.040 INFO 17152 --- [ main] c.k.s.demo.HelloSpringBootApplication : No active profile set, falling back to default profiles: default 2020-09-16 16:48:12.897 INFO 17152 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-09-16 16:48:12.917 INFO 17152 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-09-16 16:48:12.918 INFO 17152 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37] 2020-09-16 16:48:13.058 INFO 17152 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-09-16 16:48:13.059 INFO 17152 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2907 ms 2020-09-16 16:48:13.413 INFO 17152 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-09-16 16:48:13.763 INFO 17152 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path 2020-09-16 16:48:13.781 INFO 17152 --- [ main] c.k.s.demo.HelloSpringBootApplication : Started HelloSpringBootApplication in 4.381 seconds (JVM running for 5.095) 2020-09-16 16:48:27.398 INFO 17152 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2020-09-16 16:48:27.398 INFO 17152 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2020-09-16 16:48:27.411 INFO 17152 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 13 ms
1. banner:Spring Boot在启动的时候会有一个默认的启动图标,而且是可以自定义的 https://www.jianshu.com/p/bfbcabc4af1d
* 启动类Main方法里面配置: SpringApplication::SetBanner(Banner)、SpringApplication::SetBannerMode(Mode)
* src\resources\banner.txt、banner.png/jpg: #这个是MANIFEST.MF文件中的版本号 ${application.version} #这个是上面的的版本号前面加v后上括号 ${application.formatted-version} #这个是springboot的版本号 ${spring-boot.version} #这个是springboot的版本号 ${spring-boot.formatted-version} #枚举类AnsiColor这个可以替换显示的颜色 ${AnsiColor.***}
* application.properties: spring.main.show-banner=false
* 重写接口Banner实现
2. 默认profile为default
3. 默认容器为Tomcat
修改默认容器
1. 在starter-web的依赖里排除tomcat
2. 添加响应的容器starter依赖
java <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
4. Tomcat默认端口为8080
properties
# 配置端口 server.port=9080
5. SpringMVC的项目路径是“”
properties
# 配置上下文路径 server.servlet.context-path=/yy
使用浏览器访问测试
创建可运行的Jar
我们通过创建一个完全独立的可执行 jar 文件来完成我们的示例,该文件可以在生产环境中运行。可执行 jar(有时称为"Fat jar")是包含编译类以及代码需要运行的所有 jar 依赖项的存档。
Java 不提供加载嵌套 jar 文件的标准方法(jar 文件本身包含在 jar 中)。如果您想要分发自包含的应用程序,这可能有问题。
为了解决这个问题,许多开发人员使用"uber"jar。Uber jar 将所有应用程序依赖项的所有类打包到单个存档中。此方法的问题在于,很难看到应用程序中的库。如果在多个 jar 中使用相同的文件名(但内容不同),也可能存在问题。
Spring Boot 采用不同的方法,让您直接嵌套 jar。
若要创建可执行 jar,我们需要将 spring-boot-maven-plugin添加到 我们的pom.xml 。
xml
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
关于starter
关于热部署
执行器:生产就绪功能