第十章:SpringBoot
第1节:概述
1.1 微服务 SpringCloud
要搭建一个微服务,运维和部署都变得非常复杂,spring提供了一套解决方案:
springBoot: 快速构建单个服务;
springcloud: 是一系列有序框架的集合,其主要的设施有,服务发现与注册,配置中心,消息总线,负载均衡,断路器,数据监控等,通过Spring Boot 的方式,可以实现一键启动,和部署。
Spring cloud data flow: 为基于微服务的分布式流处理和批处理数据通道提供了一系列模型和最佳实践.
1.2 简介
官方网站:
https: / / spring. io/ projects/ spring- boot
Spring - Boot 是由Pivotal 团队提供的全新框架,其设计目的是用来简化新Spring 应用的初始搭建以及开发过程。个人理解来说Spring - Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,Spring - Boot 整合了其他相关联框架。
总结: springboot 全新框架,底层是spring springmvc, 并且提供了很多关联框架。 简化spring项目的开发。
1.3 优势
1 、快速构建项目。
2 、对主流开发框架的无配置集成。 内部集成了优秀的框架
3 、项目可独立运行,无须外部依赖Servlet 容器。内置了 tomcat jetty
4 、提供运行时的应用监控。
5 、极大的提高了开发、部署效率。
6 、与云计算的天然集成。
云原生~
1.4 核心功能
1 、独立运行Spring 项目
Spring Boot 可以以jar包形式独立运行,运行一个Spring Boot 项目只需要通过java - jar xx. jar来运行。
2 、内嵌servlet容器
Spring Boot 可以选择内嵌Tomcat 、jetty或者Undertow , 这样我们无须以war包形式部署项目。
3 、提供starter简化Maven 配置[ 重点]
spring提供了一系列的start pom来简化Maven 的依赖加载,例如,当你使用了spring- boot- starter- web,会自动加入如图5 - 1 所示的依赖包。
4 、自动装配Spring 【重点】
Spring Boot 会根据在类路径中的jar包,类、为jar包里面的类自动配置Bean ,这样会极大地减少我们要使用的配置。当然,Spring Boot 只考虑大多数的开发场景,并不是所有的场景,若在实际开发中我们需要配置Bean ,而Spring Boot 灭有提供支持,则可以自定义自动配置。
5 、准生产的应用监控
Spring Boot 提供基于http ssh telnet对运行时的项目进行监控。
6 、无代码生产和xml配置
Spring Boot 不是借助与代码生成来实现的,而是通过条件注解来实现的,这是Spring4 . x提供的新特性。
第2节:入门程序
2.1 创建工程导入依赖
< parent>
< groupId> org.springframework.boot groupId>
< artifactId> spring-boot-starter-parent artifactId>
< version> 2.4.3 version>
parent>
< dependencies>
< dependency>
< groupId> org.springframework.boot groupId>
< artifactId> spring-boot-starter-web artifactId>
dependency>
dependencies>
Spring Boot 将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter 相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器 。
2.2 编写主程序
2.2.1 项目结构
2.2.2 主程序代码
@SpringBootApplication
public class Example {
public static void main ( String [ ] args) {
SpringApplication . run ( Example . class , args) ;
}
}
@SpringBootApplication : 注解说明这个类是SpringBoot 的主配置类,SpringBoot 就应该运行这个类的main方法来启动SpringBoot 应用;并将主配置类(@SpringBootApplication 标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring 容器;
2.3 编写Controller代码
@Controller
public class TestController {
@RequestMapping ( "/hello" )
@ResponseBody
public String helloWorld ( ) {
return "Hello world!" ;
}
}
2.4 运行主程序进行测试
第3节:简化部署
3.1 添加maven插件
< build>
< plugins>
< plugin>
< groupId> org.springframework.boot groupId>
< artifactId> spring-boot-maven-plugin artifactId>
plugin>
plugins>
build>
3.2 执行项目打包
执行打包命令后,会在target目录下生成项目对应的jar包。
3.3 执行命令运行程序
第4节:快速创建项目
4.1 创建Spring Boot项目
4.2 项目结构解析
1 、java文件夹目录结构中自动创建好指定包和Spring Boot 启动主程序SpringbootApplication . class ;
2 、resources文件夹中目录结构
static :保存所有的静态资源; js css images;
templates:保存所有的模板页面,(Spring Boot 默认jar包使用嵌入式的Tomcat ,默认不支持JSP 页面),可以使用模板引擎(freemarker、thymeleaf);
application. properties:Spring Boot 应用的配置文件;可以修改一些默认设置;
该配置文件能够被自动读取。
第5节:配置文件
5.1 配置文件的作用及规范
Spring Boot 使用一个全局的配置文件,配置文件名是固定的;默认使用以下两种格式:
•application. properties
•application. yml
配置文件的作用:修改SpringBoot 自动配置的默认值;Spring Boot 启动时会根据配置文件自动注册相关的应用组件;
5.2 yaml配置文件
YAML :以数据为中心,比json、xml等更适合做配置文件;
5.2.1 基本语法
– 使用缩进表示层级关系
– 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 – 大小写敏感
- 键值对中间必须要有空格k:(空格)v
5.2.2 值的写法
YAML 支持的三种数据结构:
1. 字面量:普通的值(数字,字符串,布尔)
server :
port : 8081
注意:字符串默认不用加上单引号或者双引号;
双引号:特殊符号表示转义符本身;
name : "zhangsan \n lisi":输出;zhangsan 换行 lisi
单引号;特殊字符就表示字符本身;
name : ‘zhangsan \n lisi’:输出;zhangsan \n lisi
2. 对象、Map(属性和值)(键值对)
person :
name : zhangsan
age : 12
另一种行内写法:
person : { name : zhangsan, age : 12 }
3. 数组(List,Set)
hobbies :
- singing
- dancing
- running
用- (空格)值表示数组中的一个元素
另一种行内写法:
hobbies : [ singing, dancing, running]
yaml参考文档
5.2.3 配置文件值的注入
1. 构建bean对象
@Component
@ConfigurationProperties ( prefix = "person" )
public class Person {
private String pname;
private int age;
private boolean success;
private Date birth;
private Car car;
private Map < String , Object > maps;
private List < Object > lists;
}
总结: @ConfigurationProperties ( prefix = "person" ) + @Component 结合使用
或是
@ConfigurationProperties ( prefix = "person" ) + @EnableConfigurationProperties ( Person . class ) 注解共同使用
注:@EnableConfigurationProperties ( Person . class ) 加在启动类或测试类上
@ConfigurationProperties ( prefix = "person" ) 加在要获取数据的类上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
2. 构建配置文件
person :
pname : zhangsan
age : 12
birth : 2020/12/12
success : true
car :
cname : 奔驰
cprice : 200.0
lists : [ 唱歌, 跳舞]
maps : { key1 : v1, key2 : v2}
3. 执行单元测试
@SpringBootTest
class SpringbootApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads ( ) {
System . out. println ( person) ;
}
}
4. 引入配置文件处理器插件
< dependency>
< groupId> org.springframework.boot groupId>
< artifactId> spring-boot-configuration-processor artifactId>
< optional> true optional>
dependency>
总结:
(1)解决实体当中报错问题
(2)yml配置文件当中, 可以根据前缀提示~~
5.3 properties配置文件
5.3.1 properties语法
以KEY=VALue键值对的方式设置值
1. 字面量:普通的值(数字,字符串,布尔)
name=张三
2. 对象、Map(属性和值)(键值对)
person.name=张三
person.age=12
maps.key1=value1
maps.key2=value2
3. 数组(List,Set)
hobbies=singing,dancing,running
5.3.2 配置文件值的注入
1. 构建bean对象
@Component
@ConfigurationProperties ( prefix = "person" )
public class Person {
private String pname;
private int age;
private boolean success;
private Date birth;
private Car car;
private Map < String , Object > maps;
private List < Object > lists;
}
2. 构建配置文件
person.pname=张三
person.age=12
person.birth=2019/12/12
person.success=true
person.car.cname=宝马
person.car.cprice=100.0
person.lists=唱歌,跳舞,跑步
person.maps.k1=value1
person.maps.k2=value2
3. 执行单元测试
@SpringBootTest
class SpringbootApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads ( ) {
System . out. println ( person) ;
}
}
5.4 说明
5.4.1 注解区别
@ConfigurationProperties 注解和@Value 注解的区别
1 、@ConfigurationProperties 注解用于根据配置文件批量注入值,springboot默认配置文件application. yml/ application. properties;
2 、@Value 注解用于根据配置文件单个注入值;
区别 @ConfigurationProperties @Value SpEL 不支持 支持:@Value(“${jdbc.url}”) 复杂类型封装 支持 不支持
案例:
@Component
public class Person {
@Value ( "${person.pname}" )
private String pname;
@Value ( "#{12*2}" )
private int age;
private boolean success;
private Date birth;
private Car car;
private Map < String , Object > map;
private List < Object > hobbies;
}
5.4.2 配置文件中的占位符
1、springboot全局配置文件中可以使用随机数
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
2、springboot全局配置文件中可以使用占位符
通过${属性名}获取已经在配置文件中加载过的属性值;
通过${属性名:默认值}来指定找不到属性时的默认值;
案例:
person.pname=张三
person.age=${random.int}
person.success=true
person.birth=2012/12/12
person.car.cname=${car.cname:奔驰}
person.car.cprice=200.0
person.map.key1=value1
person.map.key2=value2
person.hobbies=唱歌,跳舞,跑步
5.4.3 多环境支持
Profile是Spring对不同环境提供不同配置功能的支持,可以通过激活、指定参数等方式快速切换环境;
1、多文件多环境形式:
格式:application- { profile} .properties/yml
例如:可以在项目中创建如下主配置文件:application- dev.properties、application- test.properties、application- prod.properties、application.properties, 默认使用application.properties, 可以通过配置spring.profiles.active=profile指定使用某个环境的配置文件。
2、yaml支持单文件多环境形式:
spring :
profiles :
active : test
---
spring :
profiles : dev
server :
port : 8081
spring :
config :
activate :
on-profile : dev
server :
port : 8081
---
spring :
profiles : test
server :
port : 8082
通过--- 来区分不同的profile环境。
3、激活方式:
在配置文件中指定 spring.profiles.active=dev
命令行 java - jar springboot- 0.0.1- SNAPSHOT.jar - - spring.profiles.active=dev
jvm参数 - Dspring.profiles.active=dev
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
5.4.4 配置文件加载位置
springboot默认会从以下位置加载配置文件:application. properties/ application. yml
项目所在磁盘路径file: . /config/
项目所在磁盘路径file: . /
项目类路径classpath: / config/
项目类路径classpath: /
优先级由高到底,高优先级的配置会覆盖低优先级的配置;SpringBoot 会从这四个位置全部加载主配置文件,互补配置;
如果要加载其他位置的配置文件,可以通过-- spring. config. location(只能加到命令行)来指定要加载的配置文件的位置。
第6节:Spring Boot的web开发
6.1 静态资源的处理
springboot启动时会根据配置文件自动配置相应场景的组件xxxAutoConfiguration,web项目启动时会初始化WebMvcAutoConfiguration 组件处理请求相关的操作,其中有默认处理静态资源的方法:
@Override
protected void addResourceHandlers ( ResourceHandlerRegistry registry) {
super . addResourceHandlers ( registry) ;
if ( ! this . resourceProperties. isAddMappings ( ) ) {
logger. debug ( "Default resource handling disabled" ) ;
return ;
}
ServletContext servletContext = getServletContext ( ) ;
addResourceHandler ( registry, "/webjars/**" , "classpath:/META-INF/resources/webjars/" ) ;
addResourceHandler ( registry, this . mvcProperties. getStaticPathPattern ( ) , ( registration) -> {
registration. addResourceLocations ( this . resourceProperties. getStaticLocations ( ) ) ;
if ( servletContext != null ) {
registration. addResourceLocations ( new ServletContextResource ( servletContext, SERVLET_LOCATION ) ) ;
}
} ) ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
从该方法中可以发现:
1 、默认情况下:
(1 )匹配/ webjars