目录
二十七、请求处理- 【源码分析】 - 怎么改变默认的_method
三十一、请求处理-@MatrixVariable与UrlPathHelper
十六、最佳实践-SpringBoot应用如何编写
1、引入场景依赖
2、查看自动配置了哪些(选做)
自己分析,引入场景对应的自动配置一般都生效了
配置文件中debug=true开启自动配置报告。Negative(不生效)\Positive(生效)
3、是否需要修改
参照文档修改配置项 application.properties
自己分析。xxxxProperties绑定了配置文件的哪些。
自定义加入或者替换组件
@Bean、@Component
十七、最佳实践-Lombok简化开发
1、依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
idea中搜索安装lombok插件
2、测试
===============================简化JavaBean开发===================================
@NoArgsConstructor //无参构造器
//@AllArgsConstructor //全参构造器
@Data
@ToString
@EqualsAndHashCode
public class User {
private String name;
private Integer age;
private Pet pet;
public User(String name,Integer age){
this.name = name;
this.age = age;
}
}
================================简化日志开发===================================
@Slf4j
@RestController
public class HelloController {
@RequestMapping("/hello")
public String handle01(@RequestParam("name") String name){
log.info("请求进来了....");
return "Hello, Spring Boot 2!"+"你好:"+name;
}
}
十八、最佳实践-dev-tools(热加载reload)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
项目或者页面修改以后:Ctrl+F9;
十九、最佳实践-Spring Initailizr
1、选择我们需要的开发场景(项目初始化向导)
2、自动依赖导入
3、自动创建项目结构
4、自动编写好主配置类
二十、配置文件-yaml的用法
1、yaml简介
YAML 是 "YAML Ain't Markup Language"(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。
非常适合用来做以数据为中心的配置文件
2、基本语法
- key: value;kv之间有空格
- 大小写敏感
- 使用缩进表示层级关系
- 缩进不允许使用tab,只允许空格、
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- '#'表示注释
- 字符串无需加引号,如果要加,''与""表示字符串内容 会被 转义/不转义
3、数据类型
字面量:单个的、不可再分的值。date、boolean、string、number、null
k: v
对象:键值对的集合。map、hash、set、object
行内写法: k: {k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3: v3
数组:一组按次序排列的值。array、list、queue
行内写法: k: [v1,v2,v3]
#或者
k:
- v1
- v2
- v3
4、实例
4.1 Person.java文件
@ConfigurationProperties(prefix = "person")
@Component
@ToString
@Data
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private Pet pet;
private String[] interests;
private List<String> animal;
private Map<String, Object> score;
private Set<Double> salarys;
private Map<String, List<Pet>> allPets;
}
4.2 Pet.java文件
@ToString
@Data
public class Pet {
private String name;
private Double weight;
}
4.3 application.yml文件
person:
userName: "zhangsan \n 李四"
# 单引号会将 \n作为字符串输出 双引号会将\n 作为换行输出
# 双引号不会转义,单引号会转义
boss: true
birth: 2020/12/20
age: 18
# interests: [篮球,足球]
interests:
- 篮球
- 足球
animal: [阿猫,阿狗]
# score:
# english: 80
# math: 90
score: {english: 80,math: 90}
salarys:
- 9999.99
- 9998.88
pet:
name: 阿狗
weight: 99.98
allPets:
sick:
- {name: 阿毛,weight: 99.35}
- {name: 码农,weight: 10.24}
- name: 阿虫
weight: 77.77
health:
- {name: 阿毛2,weight: 99.35}
- {name: 阿毛3,weight: 99.35}
4.4 HelloController.java文件
@RestController
public class HelloController {
@Autowired
Person person;
@RequestMapping("/person")
public Person person(){
return person;
}
}
二十一、配置文件-自定义类绑定的配置提示
自定义的类和配置文件绑定一般没有提示
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--打包的时候把它排除掉,跟业务无关-->
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
二十二、web场景-web开发简介
自定义 SpringMVC
二十三、web场景-静态资源规划与定制化
1、静态资源目录
只要静态资源放在类路径下: called /static
(or /public
or /resources
or /META-INF/resources
访问 : 当前项目根路径/ + 静态资源名
原理: 静态映射/**
请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面
改变默认的静态资源路径
spring:
web:
resources:
static-locations: [classpath:/haha/]