- java 11
- maven 3.8.4
- B站视频网站:
- https://www.bilibili.com/video/BV19K4y1L7MT?p=1
- 笔记网站:
- https://www.yuque.com/atguigu/springboot
- Spring官网:
- https://spring.io/
- Spring解决方案:
- https://spring.io/projects/spring-framework
- SpringBoot reference documentation
- https://docs.spring.io/spring-boot/docs/current/reference/html/
- Using SpringBoot
- https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using
- Developer Tools
- https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.devtools
Maven中的properties,build,repository中都是什么意思?
Maven是不是得从头到尾学习一下?
问题2: IoC和AOP至今也没有理解到本质,也没有办法讲清楚
知识点1:文件路径和意义bean:对象(组件),在Spring中,每一个对象Class,都可以被称为一个组件
config:配置文件
controller:业务控制,接口一般都放在这里
service:业务逻辑
Dao:数据访问
注解组件- @Conponent 普通组件
- @Controller 控制器
- @Service 业务逻辑组件
- @Repository 数据库组件
SpringBootApplication注解 该注解是SpringBoot启动的注解,其内部相当于实现了如下注解
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScanComponent注解
一般放在一个Class上,将Class注入到容器中
Configuration注解// todo 感觉这块没有理解清楚
@Configuration注解通常用来自动做配置的类
-
轻量配置,全量配置 @Configuration(proxyBeanMethods = true)
- proxyBeanMethods = true
- 全量配置Full模式,每次调用容器中的组件,都是同一个
- proxyBeanMethods = false
- 轻量配置Lite模式,每次调用都会重新生成一个组件
如下代码中,每次调用cat()时,都会重新实例化一个全新的组件。
- proxyBeanMethods = true
@Import({AtImport.class})
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(Car.class)
public class MyConfig1 {
@Bean
public User user01() {
return new User("ZhangSan", 18);
}
@Bean("tom") // 可以自定义组件的名称
public Pet cat() {
return new Pet("tomcat");
}
// 条件组件,当组件中有tom的情况下,才会引入下边的组件
@ConditionalOnBean(name = "tom")
public Pet cat2() {
return new Pet("tomcat2");
}
}
知识点3:ComponentScan注解
@ComponentScan(“com.abc.def.xxx”)注解可以修改包扫描时应该扫描的位置
知识点4:Import注解 @Import组件,可以往容器中导入组件,一般需要放在一个组件上
-
默认组件的名字就是全类名
如下所示,在加载MyConfig1时,会加载组件AtImport,
@Import({AtImport.class})
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(Car.class)
public class MyConfig1 {
@Bean
public User user01() {
return new User("ZhangSan", 18);
}
@Bean("tom") // 可以自定义组件的名称
public Pet cat() {
return new Pet("tomcat");
}
// 条件组件,当组件中有tom的情况下,才会引入下边的组件
@ConditionalOnBean(name = "tom")
public Pet cat2() {
return new Pet("tomcat2");
}
}
@Data
public class AtImport {
private String name;
private int age;
}
知识点5:Conditional注解
@Conditional注解是满足了特定的条件,才会给容器中添加组件
- ConditionalOnBean(name = “name”) 当容器中的组件包含"name"时会引入
- ConditionalOnMissingBean 缺少某一个组件后,则会向容器中增加组件
- ConditionalOnResource
- …
@ImportResource注解会将原始的spring中写的.xml配置的组件,加入到容器中
知识点7:ConfigurationProperties注解 在某一个组件通过@Component或@Bean等注入到容器后,可以用ConfigurationProperties注解,将配置文件中的值注入到组件中。要求名字必须要一致。
通常用来实现数据库相关的组件。
@ConfigurationProperties(prefix = "mycar")
@Component
@Data
public class Car {
private String brand;
private Integer price;
}
要求配置文件中的值为:
mycar.brand=BYD mycar.price=1000
至此,容器中的Car组件即为BYD和1000。
EnableConfigurationProperties注解 一般用在@Configration注解同级,表示开启属性配置,并将组件自动配置并注入到容器中。
config文件下:
@Configuration @EnableConfigurationProperties(Car.class)
配置文件:
mycar.brand=BYD mycar.price=1000
对象bean文件:
@Data
@ConfigurationProperties(prefix = "mycar")
public class Car {
private String brand;
private Integer price;
}
容器启动之后,就会有car出现在容器中。
AutoWired注解 去重启中取值,容器中没有就是null
SpringBootConfiguration注解 @Configuration代表当前是一个配置类
ImportResource注解 有一些旧的Spring工程,仍然在使用旧的.xml配置,因此需要将其所在的位置,加载到容器中。
@Configuration
@ImportResource("classpath:others/beans.xml")
public class MyConfig1 {
}
SpringBoot自动配置原理入门
// todo 这个部分理解的不行
SpringBootApplication注解由三个注解合成
-
SpringBootConfiguration
-
ComponentScan
-
EnableAutoConfiguration 最重要的一个注解,由如下两个注解合成
- AutoConfigurationPackage
- Import
==============================================
视频18集及以后
==============================================
DevTools-
SpringBoot做热部署需要的依赖
org.springframework.boot spring-boot-devtoolstrue
引入该依赖,完成代码修改之后,点击build,就会自动完成部署,算是无缝衔接。
这部分算是重新编译,如果想要用真正的热部署,需要用付费版的JRebel
yaml自动装配yaml中经常会用来配置组件,代码中不进行任何硬编码,这样修改起来会更方便。
yaml配置的格式需要学习一下,例子如下
Java类:
@Data
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private Pet pet;
private String[] interests;
private List animal;
private Map score;
private Set salarys;
private Map> allPets;
}
Yaml写法:
# yaml表示以上对象
person:
userName: zhangsan
boss: false
birth: 2019/12/12 20:12:33
age: 18
pet:
name: ergou
weight: 23.4
interests: [篮球,游泳]
animal:
- jerry
- mario
score:
english:
first: 30
second: 40
third: 50
math: [131,140,148]
chinese: {first: 128,second: 136}
salarys: [3999,4999.98,5999.99]
allPets:
sick:
- {name: tom}
- {name: jerry,weight: 47}
health: [{name: mario,weight: 47}]
配置处理器
参考文档传送门
SpringBoot在配置yaml文件时,有时候需要配置传统的xml或者properties,此时需要依赖
org.springframework.boot spring-boot-configuration-processortrue
配置文件中写入
author.name=zhangsan author.age=20
在类中写注解@PropertySource
@Component
@PropertySource(value = {"classpath:static/config/authorSetting.properties"},
ignoreResourceNotFound = false, encoding = "UTF-8", name = "authorSetting.properties")
public class AuthorTest {
@Value("${author.name}")
private String name;
@Value("${author.age}")
private int age;
}
SpringBoot Web开发
对应21视频–无意义文档
静态资源 静态资源目录 静态资源放置在如下路径下可以自动读取,如果存储的为文件,直接在接口中访问资源名即可直接访问。
- resources/public
- resources/static
- resources/resources
- resources/META-INF/resources
有些时候想要让静态资源访问时增加前缀,可以在yaml中配置
spring:
mvc:
static-path-pattern: /res/**
原始访问:
http://localhost:8888/bug-public.png
修改后访问:
http://localhost:8888/res/bug-public.png静态资源路径配置
默认情况会有四个文件夹下可以配置,但如果想指定特定文件夹为资源文件,可以在yaml中配置
spring:
web:
resources:
static-locations: classpath:/others
spring:
web:
resources:
static-locations: [classpath:/others, classpath:/others2]
现在及为在resources中的others的文件夹内为资源文件,原始默认的资源文件全部失效
静态资源配置欢迎页-
静态资源路径下 index.html
-
- 可以配置静态资源路径
- 但是不可以配置静态资源的访问前缀。否则导致 index.html不能被默认访问
-
每次访问项目根路径的时候,就会自动跳转到欢迎页上
-
注意不能配置资源路径
spring:
# mvc:
# static-path-pattern: /res/** 这个会导致welcome page功能失效
resources:
static-locations: [classpath:/haha/]
配置好一个index.html,直接访问接口,就会进入欢迎页
访问接口
http://localhost:8888/
index.html
Title
spring欢迎页
webjar自动映射
引入三方库webjar,可以将引入的三方库依赖加载到容器中
org.webjars jquery3.5.1
自动映射 /webjars/**
访问地址后面地址要按照依赖里面的包路径
http://localhost:8080/webjars/jquery/3.5.1/jquery.js静态资源配置原理
= =|||
实话实说,这块没有咋看懂,大概看了一圈
请求参数处理31 视频
@MatrixVariable 矩阵变量 注解矩阵变量通常用来传递多个参数,用来做多个选择
路径变量通常是使用@RequestParam注解进行获取,例如
/car/{path}/abc?xxx=xxx&aaa=aaa
MatrixVariable 可以在路径中传递一个矩阵
/car/path;low=34;brand=byd,audi,yd
这个注解通常来解决Cookie禁用的情况。
通常情况,矩阵变量是默认关闭的,如果想要开启,必须注入一个组件,如
//开启矩阵变量
@Configuration(proxyBeanMethods = false)
public class WebConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
// 不移除后边的内容,矩阵变量可以生效
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
接收矩阵变量的接口
@RestController
public class ParameterTestController {
//1、语法: 请求路径:/cars/sell;low=34;brand=byd,audi,yd
//2、SpringBoot默认是禁用了矩阵变量的功能
// 手动开启:原理。对于路径的处理。UrlPathHelper进行解析。
// removeSemicolonContent(移除分号内容)支持矩阵变量的
//3、矩阵变量必须有url路径变量才能被解析
@GetMapping("/cars/{path}")
public Map carsSell(@MatrixVariable("low") Integer low,
@MatrixVariable("brand") List brand,
@PathVariable("path") String path){
Map map = new HashMap<>();
map.put("low",low);
map.put("brand",brand);
map.put("path",path);
return map;
}
}
矩阵变量的前端
http://localhost:8888/cars/sell;low=34;brand=byd,audi,yd
请求结果可以解析矩阵变量,如
{"path":"sell","low":34,"brand":["byd","audi","yd"]}



