Spring Boot 介绍
官网地址
https://spring.io/projects/spring-boot
Spring Boot 2.3.12.RELEASE API 参考
https://docs.spring.io/spring-boot/docs/2.3.12.RELEASE/api/
Spring Boot 参考文档
https://docs.spring.io/spring-boot/docs/2.3.12.RELEASE/reference/html/
优点
快速创建独立运行 spring 项目和主流框架集成
嵌入式 servlet 容器, 应用无需打成 war 包
starters 自动依赖与版本控制
大量的自动配置, 简化开发, 也可修改默认值
无需配置 xml, 无代码生成, 开箱即用
准生产环境的运行时应用监控
与云计算的天然集成
Spring 主要目标
为所有 Spring 开发提供从根本上更快且可广泛访问的入门体验。
开箱即用,但随着需求开始偏离默认值而迅速摆脱困境。
提供大型项目(例如嵌入式服务器,安全性,度量标准,运行状况检查和外部化配置)通用的一系列非功能性功能。
没有代码生成,也不需要 XML 配置。
环境要求
生成项目
Spring Initializr 在线生成
https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.13&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=demo&name=demo&description=Demo project for Spring Boot&packageName=com.example.demo
https://start.aliyun.com/bootstrap.html/#!type=maven-project&language=java&architecture=none&platformVersion=2.4.1&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=demo&name=demo&description=Demo project for Spring Boot&packageName=com.example.demo
pom 文件分析
方式一:通过指定 parent 父项目进行创建。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 <?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.5.13</version > <relativePath /> </parent > <groupId > com.example</groupId > <artifactId > demo</artifactId > <version > 0.0.1-SNAPSHOT</version > <name > demo</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</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > </plugins > </build > </project >
方式二:通过 dependencyManagement 进行指定。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 <?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 > <groupId > com.example</groupId > <artifactId > demo</artifactId > <version > 0.0.1-SNAPSHOT</version > <name > demo</name > <description > Demo project for Spring Boot</description > <properties > <java.version > 1.8</java.version > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <project.reporting.outputEncoding > UTF-8</project.reporting.outputEncoding > <spring-boot.version > 2.4.1</spring-boot.version > </properties > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency > </dependencies > <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-dependencies</artifactId > <version > ${spring-boot.version}</version > <type > pom</type > <scope > import</scope > </dependency > </dependencies > </dependencyManagement > <build > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-compiler-plugin</artifactId > <version > 3.8.1</version > <configuration > <source > 1.8</source > <target > 1.8</target > <encoding > UTF-8</encoding > </configuration > </plugin > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > <version > 2.4.1</version > <configuration > <mainClass > com.example.demo.DemoApplication</mainClass > </configuration > <executions > <execution > <id > repackage</id > <goals > <goal > repackage</goal > </goals > </execution > </executions > </plugin > </plugins > </build > </project >
启动引导 Spring
ReadingListApplication 在 Spring Boot 应用程序里有两个作用:配置和启动引导。首先,这是主要的 Spring 配置类。虽然Spring Boot的自动配置免除了很多Spring配置,但你还需要进行少量配置来启用自动配置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package readinglist;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class ReadingListApplication { public static void main (String[] args) { SpringApplication.run(ReadingListApplication.class, args); } }
@SpringBootApplication
开启了 Spring 的组件扫描和 Spring Boot 的自动配置功能。实际上,@SpringBootApplication 将三个有用的注解组合在了一起。
Spring的@Configuration
:标明该类使用 Spring 基于 Java 的配置。虽然本书不会写太多配置,但我们会更倾向于使用基于 Java 而不是XML 的配置。
Spring 的@ComponentScan
:启用组件扫描,这样你写的 Web 控制器类和其他组件才能被自动发现并注册为Spring应用程序上下文里的Bean。本章稍后会写一个简单的 Spring MVC 控制器,使用 @Controller 进行注解,这样组件扫描才能找到它。
Spring Boot的@EnableAutoConfiguration
:这个不起眼的小注解也可以称为 @Abracadabra2
,就是这一行配置开启了 Spring Boot 自动配置的魔力,让你不用再写成篇的配置了。
2. 测试 Spring Boot 应用程序
Initializr 还提供了一个测试类的骨架,可以基于它为你的应用程序编写测试。但 ReadingListApplicationTests 不止是个用于测试的占位符,它还是一个例子,告诉你如何为 Spring Boot 应用程序编写测试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package readinglist;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.SpringApplicationConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import readinglist.ReadingListApplication;@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration( classes = ReadingListApplication.class) @WebAppConfiguration public class ReadingListApplicationTests { @Test public void contextLoads () { } }
3. 配置应用程序属性
你完全不用告诉 Spring Boot 为你加载 application.properties
,只要它存在就会被加载,Spring 和应用程序代码都能获取其中的属性。
额外知识
Spring MVC 中将 @PathVariable
的使用
1 2 3 4 5 6 7 @RequestMapping(value="/{reader}", method=RequestMethod.GET) public String readersBooks ( @PathVariable("reader") String reader, Model model) { ... } }
自动配置微调
$ java -jar readinglist-0.0.1-SNAPSHOT.jar --server.port=8000
默认情况下,Spring Boot 会用 Logback(http://logback.qos.ch )来记录日志,并启用INFO
级别输出到控制台。
Spring Boot应用程序有多种设置途径
Spring Boot 能从多种属性源获得属性,包括如下几处。
(1) 命令行参数
(2) java:comp/env 里的JNDI属性
(3) JVM 系统属性
(4) 操作系统环境变量
(5) 随机生成的带 random.* 前缀的属性(在设置其他属性时,可以引用它们,比如${random.long})
(6) 应用程序以外的 application.properties 或者 appliaction.yml 文件
(7) 打包在应用程序内的 application.properties 或者 appliaction.yml 文件
(8) 通过 @PropertySource 标注的属性源
(9) 默认属性
这个列表按照优先级排序,也就是说,任何在高优先级属性源里设置的属性都会覆盖低优先级的相同属性。例如,命令行参数会覆盖其他属性源里的属性。
application.properties 和 application.yml 文件能放在以下四个位置。
(1) 外置,在相对于应用程序运行目录的/config子目录里。
(2) 外置,在应用程序运行的目录里。
(3) 内置,在config 包内。
(4) 内置,在Classpath 根目录。
同样,这个列表按照优先级排序。也就是说,/config 子目录里的application.properties 会覆盖应用程序Classpath里的application.properties 中的相同属性。
此外,如果你在同一优先级位置同时有 application.properties 和 application.yml,那么 application.yml 里的属性会覆盖 application.properties 里的属性。
Spring Boot 会为错误视图提供如下错误属性
timestamp:错误发生的时间。
status:HTTP状态码。
error:错误原因。
exception:异常的类名。
message:异常消息(如果这个错误是由异常引起的)。
errors:BindingResult异常里的各种错误(如果这个错误是由异常引起的)。
trace:异常跟踪信息(如果这个错误是由异常引起的)。
path:错误发生时请求的URL路径。
构建成品后运行项目
一键启用
1 2 3 4 5 6 mvn package java -jar target/springbootTest-0.0.1-SNAPSHOT.jar nohup java -jar 自己的springboot项目.jar >mylog.log 2>&1 &
搭配参数改变端口
1 java -jar demo-0.0.1-SNAPSHOT.jar --server.port=8012
参考
README - 《Spring Boot 中文文档》 - 书栈网 · BookStack
https://www.bookstack.cn/read/springboot/README.md
Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded 的三种解决办法_Hello_World_QWP的博客-CSDN 博客
https://blog.csdn.net/Hello_World_QWP/article/details/83271592