1、spring-boot-starter-parent管理着Springboot项目的所有依赖,
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
它自身还有一个叫做spring-boot-dependencies的父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.5</version>
</parent>
父项目中又管理着很多版本号。
2、spring项目创建的时候会自动依赖spring-boot-starter-web,该模块自动引入了Tomcat和springMCV
3、主程序所在的包默认都会被扫描
但是我们也可以另外可以至指定扫描包
一、在SpringbootApplication上加scanBasePackages
@SpringBootApplication(scanBasePackages = "com.example")
//==============================================
二、或者注释掉@SpringbootApplication,然后换成下面三个:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.example")
4、可以再application.properties这个文件下开启debug模式,然后他就会自动打印引入的具体场景了,哪些生效Positive,哪些未生效有会打印Negative。
debug=true
5、application.yml要配置什么东西都可以再这里进行查找Common Application Properties
6、通过@Bean和@Component来自定义组件
7、yml配置文件和java bean相互绑定
1)首先要在Bean类中注解@Component和@ConfigurationProperties(prefix="myuser"),这是绑定的关键,而且bean中必须要有set和get方法
@ConfigurationProperties(prefix="myuser")
@Component
public class User {
private String myUserName;
private int myAge;
private String address;
private ArrayList<String> friends;
private HashMap<String, String> myMap;
//此处省略set和get方法
}
2)yml的开头要和bean中指定的prefix指定的一致,这里使用的是myuser
myuser:
my-age: 32
my-user-name: "唐僧"
address: "大宋"
friends: ["孙悟空", "猪八戒", "沙僧"]
my-map:
name: "牛魔王"
age: 33
3)为了在编辑yml文件的时候有提示,我们还要加入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
但是这个包不应该在打包的时候参与打包,所以通过excludes排除掉
8、为静态资源加前缀
spring:
mvc:
static-path-pattern: /myresource/**
访问方式必须加上myresource:http://localhost:8080/myresource/byz.jpg
9、如果不想用默认的静态资源位置(如static目录下),也可以改变
spring:
mvc:
static-path-pattern: /myresource/**
web:
resources:
static-locations: [classpath:/myresources/]
目录如下:
10、请求参数
1)路径变量@PathVariable
服务端请丢地址直接中括号括起来就可以了
@GetMapping("params/{id}")
public String getByPathVariable(@PathVariable String id){
return "路径变量:"+id;
}
请求的时候直接在链接后面相应位置写上参数:http://localhost/params/99
2)参数请求@RequestParam
可以一次性用map接收完,也可以一个一个的接
@GetMapping("/req")
public String getByRequestParam(/*@RequestParam("id1") String id, @RequestParam("name") String name, */@RequestParam Map<String, String> p){
return /*"路径变量:"+id+", 名字:"+name*/p.toString();
}
请求方式:http://localhost/req?id1=99&&name=张三
3)POST参数@RequestBodys
@PostMapping("/getPost")
public String getPost(@RequestBody String p){
return "Post参数:"+p;
}
11、如果自定义了WebMvcConfigurer了,并且加了@EnableWebMvc,则Springboot所有自动配置都将会失效,需要手动自己写
12、自动配置原理套路分析:
引入starter——>xxxAutoConfiguration——>导入xxx组件——>绑定xxxProperties——>绑定配置文件
所以需要修改相关配置要倒过来,先修改配置文件,如果不行就只能一步步的回溯上去,自定义相关Properties或者是组件,从而达到自定义的效果。