springboot2学习笔记

SpringBoot2- 基础入门

文章目录


请点击这里 获取本文代码

第一章 基础入门

1.1 学习资料

  • 文档地址: https://www.yuque.com/atguigu/springboot

    • 文档不支持旧版本IE、Edge浏览器,请使用chrome或者firefox
  • 视频地址: http://www.gulixueyuan.com/ https://www.bilibili.com/video/BV19K4y1L7MT?p=1

  • 源码地址:https://gitee.com/leifengyang/springboot2

1.2 SpringBoot优点

  • Create stand-alone Spring applications

    • 创建独立Spring应用
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)

    • 内嵌web服务器
  • Provide opinionated ‘starter’ dependencies to simplify your build configuration

    • 自动starter依赖,简化构建配置
  • Automatically configure Spring and 3rd party libraries whenever possible

    • 自动配置Spring以及第三方功能
  • Provide production-ready features such as metrics, health checks, and externalized configuration

    • 提供生产级别的监控、健康检查及外部化配置
  • Absolutely no code generation and no requirement for XML configuration

    • 无代码生成、无需编写XML

SpringBoot是整合Spring技术栈的一站式框架

SpringBoot是简化Spring技术栈的快速开发脚手架

1.3 SpringBoot缺点

  • 人称版本帝,迭代快,需要时刻关注变化
  • 封装太深,内部原理复杂,不容易精通

1.4 maven配置

<mirrors>
      <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror>
  </mirrors>
 
  <profiles>
         <profile>
              <id>jdk-1.8</id>
              <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
              </activation>
              <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
              </properties>
         </profile>
  </profiles>

1.5 简单案例

1.5.1 引入依赖
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
1.5.2 创建主程序
/**
 * 主程序类
 * @SpringBootApplication:这是一个SpringBoot应用
 */
@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}
1.5.3 编写业务
@RestController
public class HelloController {


    @RequestMapping("/hello")
    public String handle01(){
        return "Hello, Spring Boot 2!";
    }


}
1.5.4 测试

直接运行main方法

1.5.5 简化配置

application.properties, 直接在这个文件修改配置就可,下面的就是修改了 服务器的端口号,改成 8888

server.port=8888
1.5.6 简化部署
 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

把项目打成jar包,直接在目标服务器执行即可。

1.6 SpringBoot 的特点

1.6.1 依赖管理
1.6.1.1 父项目做依赖管理
依赖管理    
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
</parent>

他的父项目
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.3.4.RELEASE</version>
  </parent>

几乎声明了所有开发中常用的依赖的版本号,自动版本仲裁机制
1.6.1.2 开发导入starter场景启动器
1、见到很多 spring-boot-starter-* : *就某种场景
2、只要引入starter,这个场景的所有常规需要的依赖我们都自动引入
3、SpringBoot所有支持的场景
https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
4、见到的  *-spring-boot-starter: 第三方为我们提供的简化开发的场景启动器。
5、所有场景启动器最底层的依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
  <version>2.3.4.RELEASE</version>
  <scope>compile</scope>
</dependency>
1.6.1.3 无需关注版本号,自动版本仲裁
1、引入依赖默认都可以不写版本
2、引入非版本仲裁的jar,要写版本号。
1.6.1.4 可以修改默认版本号
1、查看spring-boot-dependencies里面规定当前依赖的版本 用的 key。
2、在当前项目里面重写配置
    <properties>
        <mysql.version>5.1.43</mysql.version>
    </properties>
1.6.2 自动配置
1.6.2.1 自动配好Tomcat
    • 引入Tomcat依赖。
    • 配置Tomcat
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.3.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
1.6.2.2 自动配好SpringMVC
    • 引入SpringMVC全套组件
    • 自动配好SpringMVC常用组件(功能)
1.6.2.3 自动配好Web常见功能,如:字符编码问题
    • SpringBoot帮我们配置好了所有web开发的常见场景
1.6.2.4 默认的包结构
    • 主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来
    • 无需以前的包扫描配置
    • 想要改变扫描路径,@SpringBootApplication(scanBasePackages=“com.nguyenxb”)
      • 或者@ComponentScan 指定扫描路径
@SpringBootApplication
等同于
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.nguyenxb.boot")
1.6.2.5 各种配置拥有默认值
    • 默认配置最终都是映射到某个类上,如:MultipartProperties
    • 配置文件的值最终会绑定每个类上,这个类会在容器中创建对象
1.6.2.6 按需加载所有自动配置项…
    • 非常多的starter
    • 引入了哪些场景这个场景的自动配置才会开启
    • SpringBoot所有的自动配置功能都在 spring-boot-autoconfigure 包里面

第二章 容器功能

2.1 组件添加

2.1.1 @Configuration

基本使用

Full模式与Lite模式

配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断

配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式

###############Configuration使用示例 #################################
/**
 *  1. 配置类里面使用 @Bean 标注在方法上给容器注册组件,默认也是单实例的.
 *  2. 配置类也是本身也是组件
 *  3. proxyBeanMethods : 代理 bean 的方法
 *      Full: @Configuration(proxyBeanMethods = true) , 单例模式
 *          外部无论对配置类中的这个注册方法调用多少次, 获取的都是 之前注册容器中的单实例对象.
 *      Lite : @Configuration(proxyBeanMethods = false) , 多实例模式.
 *          外部无论对配置类中的这个注册方法调用多少次, 获取的都是 新创建的实例对象.
 *
 */
// @Configuration:告诉springboot 这是一个配置类, 即配置文件
@Configuration(proxyBeanMethods = false)
public class MyConfig {
    /*
        外部无论对配置类中的这个注册方法调用多少次, 获取的都是 之前注册容器中的单实例对象.
    *
    * */

    /**
     * @Bean  给容器中添加组件. 以方法名作为组件的id,返回类型是组件的类型,
     * 返回值就是组件在容器中的实例
     */
    @Bean
    public User user01(){
        User zhangsan = new User("zhangsan",20);
//        zhangsan.setPet();
        return zhangsan;
    }
    @Bean("tomcatPet") //  给容器中添加组件. 以tomcatPet作为组件的id
     * 返回值就是组件在容器中的实例
    public Pet tomcat(){
        return new Pet("tom");
    }
}

####################### MainApplication使用 ##############################
/**
 * 主程序类
 * @SpringBootApplication:这是一个SpringBoot应用
 */
//@SpringBootApplication // 这个注解包含了下面三个注解, 用于声明这个是springboot应用
@EnableAutoConfiguration
@SpringBootConfiguration
@ComponentScan("com.nguyenxb.boot") // 这是组件扫描的注解,指定扫描的包为 com.nguyenxb.boot
public class MainApplication {

    public static void main(String[] args) {

        // 1. 返回ioc 容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        // 2. 查看容器里面的组件
        String[] names = run.getBeanDefinitionNames();
        for (String name :
                names) {
            System.out.println(name);
        }

        // 3, 从容器中获取组件
        Pet tom01 = run.getBean("tomcatPet", Pet.class);

        Pet tom02 = run.getBean("tomcatPet", Pet.class);

        System.out.println("Pet 组件 :" +(tom01==tom02)); // Pet 组件 :true

        // 4.如果 设置@Configuration(proxyBeanMethods = true)代理对象调用方法
        // springboot 总是会检查容器中是否存在该组件.保证组件的唯一性
        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);

        User user01 = bean.user01();
        User user = bean.user01();
        System.out.println("user :"+(user==user01));

//        5. 获取容器中的组件
        String[] beansOfType = run.getBeanNamesForType(User.class);
        for (String name :
                beansOfType) {
            System.out.println("user:"+name);
        }
        String[] beansOfType1 = run.getBeanNamesForType(Pet.class);
        for (String name : beansOfType1){
            System.out.println("pet:"+name);
        }

    }
}
2.1.2 @Bean(创建实体对象)、@Component(创建普通类的实例)、@Controller(创建控制器类的实例)、@Service(创建业务类的实例)、@Repository(创建持久层的实例)
2.1.3 @Import
//  @Import({User.class,Pet.class }) : 给容器中自动创建出两个类型的组件,默认的组件名字就是全类名, 即springboot容器中创建了 两个实例 com.nguyenxb.boot.bean.User, com.nguyenxb.boot.bean.Pet.
@Import({User.class, Pet.class})
@Configuration(proxyBeanMethods = false)
public class MyConfig {}

@Import 高级用法: https://www.bilibili.com/video/BV1gW411W7wy?p=8

2.1.4 @Conditional

条件装配:满足Conditional指定的条件,则进行组件注入

// ConditionalOnMissingBean 当容器中没有 某个组件时才执行某些事情

// ConditionalOnSingleCandidate  当容器中的 某个组件只有一个实例时

// ConditionalOnBean  当容器中有某个组件时

// ConditionalOnProperty 当容器中配置文件配置了某个属性时

// ConditionalOnCloudPlatform 
// ConditionalOnJndi 
// Profile 
// ConditionalOnWarDeployment 

// ConditionalOnNotWebApplication 当项目不是web应用时

// ConditionalOnMissingClass  当容器中没有某个类时

// ConditionalOnClass  当容器中有某一个类时

// ConditionalOnResource 当项目的类路径存在某些资源时

// ConditionalOnWebApplication  当项目是web应用时

// ConditionalOnRepositoryType 
// ConditionalOnEnabledResourceChain 

// ConditionalOnJava  当我们项目时某个版本号时

// ConditionalOnExpression 

@ ConditionalOnBean示例

@ConditionalOnBean(name="tom22") // 当springboot容器中 存在 组件名为 tom22时, springboot才能创建这个MyConfig1 里面的所有组件 , 即容器中有 tom22时, 会创建组件,其id为 tom和user01.
// @ConditionalOnMissingBean (name = "tom22") // 当springboot容器中 不存在 组件名为 tom22时, springboot才能创建这个MyConfig1 里面的所有组件, 即容器中 没有tom22时候,不会创建该类的任何组件
@Configuration
public class MyConfig1 {
    @Bean
    public Pet tom(){
        return new Pet() ;
    }
    @ConditionalOnBean(name="tom11") // 当容器中存在 tom11时, 会执行该方法,并向容器中注入 user01的组件
    @Bean
    public User user01(){
        return new User() ;
    }
}   

2.2、原生配置文件引入

2.2.1 @ImportResource

导入配置文件中的组件

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user02" class="com.nguyenxb.boot.bean.User">
        <property name="name" value="user02"></property>
    </bean>
</beans>

使用方法:在类上方使用注解,并指定文件的路径名称即可导入user02组件到springboot容器中.

 @ImportResource("classpath:beans.xml")
@Configuration(proxyBeanMethods = true)
public class MyConfig {}

2.3 配置绑定

application.properties

mycar.brand=saasd
mycar.price=99999

如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用;

public class getProperties {
     public static void main(String[] args) throws FileNotFoundException, IOException {
         Properties pps = new Properties();
         pps.load(new FileInputStream("application.properties"));
         Enumeration enum1 = pps.propertyNames();//得到配置文件属性的名字
         while(enum1.hasMoreElements()) {
             String strKey = (String) enum1.nextElement();
             String strValue = pps.getProperty(strKey);
             System.out.println(strKey + "=" + strValue);
             //封装到JavaBean。
         }
     }
 }
2.3.1 方式一 : @Component 和@ConfigurationProperties 一起使用
/**
 * 只有在容器中的组件,才会拥有SpringBoot提供的强大功能
 */
@Component
@ConfigurationProperties(prefix = "mycar")// 即读取springboot的核心配置文件中mycar.brand=saasd 的前缀
public class Car {
 	private String brand;
    private Integer price;
	// 已经省略set,get,toString方法
}
2.3.2 方式二 : @EnableConfigurationProperties + @ConfigurationProperties

这个用来解决当方式一的存在的问题, 即当 引入其他包时, 他对于的某个类没有使用 @Component 注解 , 就能使用这个方式.

###############  MyConfig.java (配置类) #########################
@EnableConfigurationProperties(Car.class) // 这个注解必须写在配置注解这里, 开启Car类的属性配置功能
//1、开启Car配置绑定功能
//2、把这个Car这个组件自动注册到容器中
@Configuration(proxyBeanMethods = true)
public class MyConfig {}

################## bean包下 的实体类 #####################
@ConfigurationProperties(prefix = "mycar")
public class Car {
    private String brand;
    private Integer price;
}

第三章 自动配置原理入门

3.1 引导加载自动配置类

@SpringBootApplication // 这个注解包含了下面三个注解, 他是springboot的核心注解

@SpringBootConfiguration 
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
		
		
public @interface SpringBootApplication{}

    
3.1.1 @SpringBootConfiguration

这个注解里面还封装了 @Configuration , 代表当前类是一个配置类

3.1.2 @ComponentScan

指定扫描包,即封装的Spring注解的扫描包;

3.1.3 @EnableAutoConfiguration
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
3.1.3.1 @AutoConfigurationPackage

自动配置包, 指定了默认的包规则

@Import(AutoConfigurationPackages.Registrar.class)  //给容器中导入一个组件
public @interface AutoConfigurationPackage {}

//利用Registrar给容器中导入一系列组件
//将指定的一个包下的所有组件导入进来?即MainApplication 所在包下。
3.1.3.2 @Import(AutoConfigurationImportSelector.class)
public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware,
		ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {

    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
       if (!isEnabled(annotationMetadata)) {
          return NO_IMPORTS;
       }
       AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);// 此方法导入组件
       return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
    }
}    


	1、利用getAutoConfigurationEntry(annotationMetadata);给容器中批量导入一些组件

    2、调用List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类

    3、利用工厂加载 Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader);得到所有的组件

    4、从META-INF/spring.factories位置来加载一个文件。
	默认扫描我们当前系统里面所有META-INF/spring.factories位置的文件
    从 spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories 的文件中 导入 127 个自动配置类, 这个是文件写死的.
    

3.2、按需开启自动配置项

虽然我们127个场景的所有自动配置启动的时候默认全部加载。xxxxAutoConfiguration
按照条件装配规则(@Conditional),最终会按需配置。

3.3、修改默认配置

@Bean
@ConditionalOnBean(MultipartResolver.class)  //容器中有这个类型组件时候 满足条件
@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) //容器中没有这个名字 multipartResolver 的组件
public MultipartResolver multipartResolver(MultipartResolver resolver) {
//给@Bean标注的方法传入了对象参数,这个参数的值就会从容器中找。
//SpringMVC multipartResolver。防止有些用户配置的文件上传解析器的名字不符合规范
// Detect if the user has created a MultipartResolver but named it incorrectly
return resolver;
}
  // 给容器中加入了文件上传解析器;

SpringBoot默认会在底层配好所有的组件。但是如果用户自己配置了以用户的优先

// 比如 用户配置了字符集过滤器, springboot就默认使用, 用户配置的过滤器
@Bean
@ConditionalOnMissingBean
public CharacterEncodingFilter characterEncodingFilter() {}

总结:

  • SpringBoot先加载所有的自动配置类 xxxxxAutoConfiguration

  • 每个自动配置类按照条件进行生效,默认都会绑定配置文件指定的值。xxxxProperties里面拿。xxxProperties和配置文件进行了绑定

  • 生效的配置类就会给容器中装配很多组件

  • 只要容器中有这些组件,相当于这些功能就有了

  • 定制化配置

    • 用户直接自己@Bean替换底层的组件
    • 用户去看这个组件是获取的配置文件什么值就去修改。

xxxxxAutoConfiguration —> 组件 —> xxxxProperties里面拿值 ----> application.properties

3.4 最佳实践

  • 引入场景依赖

    • https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter
  • 查看自动配置了哪些(选做)

    • 自己分析,引入场景对应的自动配置一般都生效了
    • 配置文件中debug=true开启自动配置报告。Negative(不生效)\Positive(生效), 即 在application.properties配置文件中新增一行 debug=true,当应用程序运行时,可以在控制台看到哪些组件生效(Positive matches),哪些组件不生效(Negative matches)
  • 是否需要修改

    • 参照文档修改配置项
      • https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#common-application-properties
      • 自己分析。xxxxProperties绑定了配置文件的哪些。
    • 自定义加入或者替换组件
      • @Bean、@Component… 等spring使用的都能兼容.
    • 自定义器 XXXXXCustomizer

第四章 开发小技巧

4.1 Lombok

简化javaBean的开发

<!-- 1. 在pom文件中加入依赖 
	 2.在idea插件商城中 搜索安装lombok插件-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

用法 :

4.1.1 通过注解生成set,get方法, 构造方法.
package com.nguyenxb.boot.bean;

import lombok.*;

@ToString // 声明 toString 方法
@Data // 声明 set,get方法 , 包含toString方法
@AllArgsConstructor // 声明 全参数的构造器
@NoArgsConstructor // 声明 无参数构造器
@EqualsAndHashCode // 声明 hashcode方法.
public class User {
    private String name;
    private Integer age;
}
4.1.2 快捷打印日志
import com.nguyenxb.boot.bean.Car;
import lombok.extern.slf4j.Slf4j;

@Slf4j // 用于输出日志的注解
@RestController // 控制器对象注解
public class HelloController {

    @RequestMapping("/hello")
    public String handle01(){
        log.info("请求进来了... ");
        return "Hello, Spring Boot 2!";
    }


}

4.2 dev-tools

自动更新工具, 实际就是自动重启 , spring也提供了不重启的热更新工具 jrebel,但是要付费

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

<!--在项目或者页面修改以后:Ctrl+F9;就是idea重新编译项目的快捷键-->

4.3、Spring Initailizr(项目初始化向导)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

SpringBoot2-核心功能

第五章 配置文件

5.1 文件类型

5.1.1 properties

与以前的properties用法相同

5.1.2 yaml
5.1.2.1 简介

YAML 是 “YAML Ain’t Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。

非常适合用来做以数据为中心的配置文件

5.1.2.2 基本语法
  • key: value;kv之间有空格

  • 大小写敏感

  • 使用缩进表示层级关系

  • 缩进不允许使用tab,只允许空格 (idea 可以忽略这个情况)

  • 缩进的空格数不重要,只要相同层级的元素左对齐即可

  • '#'表示注释

  • 字符串无需加引号,如果要加,’'与""表示字符串内容 会被 转义/不转义

5.1.2.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
5.1.2.4 示例
// ==============Person.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<String> animal;
	private Map<String, Object> score;
	private Set<Double> salarys;
	private Map<String, List<Pet>> allPets;
}
// ===============Pet.java====================
@Data
public class Pet {
	private String name;
	private Double weight;
}

application.yaml

# yaml表示以上对象
person:
  userName: zhangsan
#  userName: "zhangsan \n 张三"
#  userName: 'zhangsan \n 张三'
#  单引号会将 \n 作为字符串输出, 双引号会将\n 作为 换行输出
#  即 双引号不会转义, 单引号会转义
  boss: false
  birth: 2020/1/1
  age: 18
  pet:
    # 使用缩进式给 pet 对象赋值
    name: tomcat
    weight: 23.4
    # 行内给 interests 字符串数组赋值
  interests: [篮球,游泳]
  # 使用缩进式给 animal list集合赋值
  animal:
    - jerry
    - mario
  # 使用缩进式给 score map<String,Object>集合赋值
  score:
    english:
      first: 30
      second: 40
      third: 50
    math: [131,140,148]
    chinese: {first: 128,second: 136}
  salarys: [3999,4999.98,5999.99]
  # 使用缩进式给 allPets map<String,List<Pet>>集合赋值
  allPets:
    sick:
      - {name: tom}
      - {name: jerry,weight: 47}
    health: [{name: mario,weight: 47}]
5.1.2.5 配置提示功能

给yaml文件添加提示功能, 自定义的类和配置文件绑定一般没有提示.

<!--在pom.xml 中导入yaml的提示依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>


<!--添加插件: 将yaml提示功能的依赖 不打包到 spring boot 的 jar项目中,因为
	yaml提示功能的jar包是用于给开发人员提供便捷的,并不会影响项目的运行.如果假如的这个yaml文件的jar 包反而会增加项目的占用内存大小 
-->
<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开发

6.1 SpringMVC自动配置概览

Spring Boot provides auto-configuration for Spring MVC that works well with most applications.(大多场景我们都无需自定义配置)

The auto-configuration adds the following features on top of Spring’s defaults:

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

    • 内容协商视图解析器和BeanName视图解析器
  • Support for serving static resources, including support for WebJars (covered later in this document)).

    • 静态资源(包括webjars)
  • Automatic registration of Converter, GenericConverter, and Formatter beans.

    • 自动注册 Converter,GenericConverter,Formatter
  • Support for HttpMessageConverters (covered later in this document).

    • 支持 HttpMessageConverters (后来我们配合内容协商理解原理)
  • Automatic registration of MessageCodesResolver (covered later in this document).

    • 自动注册 MessageCodesResolver (国际化用)
  • Static index.html support.

    • 静态index.html 页支持
  • Custom Favicon support (covered later in this document).

    • 自定义 Favicon
  • Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).

    • 自动使用 ConfigurableWebBindingInitializer ,(DataBinder负责将请求数据绑定到JavaBean上)

If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.

不用@EnableWebMvc注解。使用 @Configuration + WebMvcConfigurer 自定义规则

If you want to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations and use it to provide custom instances of those components.

声明 WebMvcRegistrations 改变默认底层组件

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.

使用 @EnableWebMvc+@Configuration+DelegatingWebMvcConfiguration 全面接管SpringMVC

6.2 简单功能分析

6.2.1 静态资源访问
6.2.1.1 静态资源目录

​ 默认情况下,Spring Boot 从类路径中的/static (或/public/resources/META-INF/resources)目录或 ServletContext的根目录提供静态内容。

它使用 Spring MVC 中的 ResourceHttpRequestHandler,因此你可以通过添加自己的 WebMvcConfigurer 和重写resourcehandlers方法来修改该行为。

如何访问静态资源? : 当前项目的根路径 + 静态资源名.

访问原理: 静态资源映射的访问路径为 /**

​ 请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面

# springboot的默认静态资源设置为
spring.mvc.static-path-pattern=/resources/**

改变默认的静态资源路径

spring:
# 设置访问静态资源的前缀为 localhost:8080/res/
  mvc:
    static-path-pattern: /res/**

# 设置静态资源的存储路径 为 haha 目录下的文件
  resources:
    static-locations: [classpath:/haha/]
    
#即通过修改这个之后, 他的访问静态资源的地址 为
# localhost:8080/res/haha/文件名.jpg
6.2.1.2 静态资源访问前缀

springboot 默认访问是无前缀的, 即访问静态资源的方式就是 localhost:8080/文件名.jpg

spring:
  mvc:
    static-path-pattern: /resources/**

当前项目 + static-path-pattern + 静态资源名 , 注 : 静态资源名在静态资源文件夹下找,按照路径写上.

6.2.1.3 webjar

自动映射 : /webjars/**

https://www.webjars.org/

<!--导入jquery的依赖-->
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.5.1</version>
</dependency>

访问地址:http://localhost:8080/webjars/jquery/3.5.1/jquery.js 后面地址要按照依赖里面的包路径

6.2.1.4 欢迎页面
  • 静态资源路径下 index.html

    • 可以配置静态资源路径
    • 但是不可以配置静态资源的访问前缀。否则导致 index.html不能被默认访问
spring:
#  mvc:
#    static-path-pattern: /res/**   这个会导致welcome page功能失效

  resources:
    static-locations: [classpath:/haha/]
  • controller能处理/index
6.2.1.5 自定义 Favicon

favicon.ico 放在静态资源目录下即可。就是网站的图标

6.2.1.6 静态资源配置原理
  • SpringBoot启动默认加载 xxxAutoConfiguration 类(自动配置类)
  • SpringMVC功能的自动配置类 WebMvcAutoConfiguration,生效
6.2.2 请求映射
6.2.2.1 rest使用与原理
  • @xxxMapping;

  • Rest风格支持(使用HTTP请求方式动词来表示对资源的操作

    • 以前:**/getUser 获取用户 /deleteUser 删除用户 /editUser 修改用户 /saveUser 保存用户
    • 现在: /user *GET-*获取用户 *DELETE-*删除用户 *PUT-*修改用户 *POST-*保存用户
    • 核心Filter;HiddenHttpMethodFilter
      • 用法: 表单method=post,隐藏域 _method=put
      • SpringBoot中手动开启
    • 扩展:如何把_method 这个名字换成我们自己喜欢的。

    • package com.nguyenxb.boot.config;
      
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.web.filter.HiddenHttpMethodFilter;
      
      @Configuration(proxyBeanMethods = false)
      public class WebConfig {
          @Bean
          public HiddenHttpMethodFilter methodFilter(){
              HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();
              // 设置隐藏的表单访问方式 为 _m 即
              /*
              * <form action="/user" method="get">
                  <input type="hidden" name="_method" value="GET">
                  <input type="hidden" name="_m" value="GET"> 
                  <input type="submit"  value="发起get请求">
      		</form>
              * */
              // 修改的就是  <input type="hidden" name="_m" value="GET"> 
              methodFilter.setMethodParam("_m"); 
      
              return methodFilter;
          }
      }
      

请求页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>aaaaa</title>
</head>
<body>
<h1>index</h1>
<form action="/user" method="get">
    <input type="hidden" name="_method" value="GET">
    <input type="submit"  value="发起get请求">
</form>
<form action="/user" method="post">
    <input type="hidden" name="_method" value="POST">
    <input type="submit"  value="发起POST请求">
</form>
<form action="/user" method="post">
    <input type="hidden" name="_method" value="DELETE">
    <input type="submit"  value="发起delete请求">
</form>
<form action="/user" method="post">
    <input type="hidden" name="_method" value="PUT">
    <input type="submit"  value="发起put请求">
</form>
</body>
</html>

Controller

@GetMapping("/user")    
//@RequestMapping(value = "/user",method = RequestMethod.GET)
    public String getUser(){
        return "GET-张三";
    }
	@PostMapping("/user")  
   // @RequestMapping(value = "/user",method = RequestMethod.POST)
    public String saveUser(){
        return "POST-张三";
    }

	@DeleteMapping("/user")  
    //@RequestMapping(value = "/user",method = RequestMethod.PUT)
    public String putUser(){
        return "PUT-张三";
    }
	@PutMapping("/user")
    //@RequestMapping(value = "/user",method = RequestMethod.DELETE)
    public String deleteUser(){
        return "DELETE-张三";
    }


	@Bean
	@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
	@ConditionalOnProperty(prefix = "spring.mvc.hiddenmethod.filter", name = "enabled", matchIfMissing = false)
	public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
		return new OrderedHiddenHttpMethodFilter();
	}


//自定义filter
    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
        HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();
        methodFilter.setMethodParam("_m");
        return methodFilter;
    }

Rest原理(表单提交要使用REST的时候)

  • 表单提交会带上**_method=PUT**

  • 请求过来被HiddenHttpMethodFilter拦截

    • 请求是否正常,并且是POST
      • 获取到**隐藏域表单_method**的值。
      • 兼容以下请求;PUT.DELETE.PATCH
      • 原生request(post),包装模式requesWrapper重写了getMethod方法,返回的是传入的值。
      • 过滤器链放行的时候用wrapper。以后的方法调用getMethod是调用requesWrapper的。

Rest使用客户端工具,

  • 如PostMan直接发送Put、delete等方式请求,无需Filter。
spring:
  mvc:
    hiddenmethod:
      filter:
        enabled: true   #开启页面表单的Rest功能
6.2.2.2 请求映射原理

SpringMVC功能分析都从 org.springframework.web.servlet.DispatcherServlet-》doDispatch()

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    HttpServletRequest processedRequest = request;
    HandlerExecutionChain mappedHandler = null;
    boolean multipartRequestParsed = false;

    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

    try {
      ModelAndView mv = null;
      Exception dispatchException = null;

      try {
        processedRequest = checkMultipart(request);
        multipartRequestParsed = (processedRequest != request);

        // 找到当前请求使用哪个Handler(Controller的方法)处理
        mappedHandler = getHandler(processedRequest);
                
                //HandlerMapping:处理器映射。/xxx->>xxxx

所有的请求映射都在HandlerMapping中。

  • SpringBoot自动配置欢迎页的 WelcomePageHandlerMapping 。访问 /能访问到index.html;

  • SpringBoot自动配置了默认 的 RequestMappingHandlerMapping

  • 请求进来,挨个尝试所有的HandlerMapping看是否有请求信息。

    • 如果有就找到这个请求对应的handler
    • 如果没有就是下一个 HandlerMapping
  • 我们需要一些自定义的映射处理,我们也可以自己给容器中放HandlerMapping。自定义 HandlerMapping

6.3 普通参数和基本注解

6.3.1 注解
6.3.1.1 路径变量 : @PathVariable
  • @PathVariable 无属性值时,获取的时 get路径下的所有参数, 并且参数用Map<String,String>类型接收. 如: @PathVariable Map<String,String> pv
  • @PathVariable 有属性值时,获取的时 get路径下的相对应的参数,如: @PathVariable("id") Integer id, @PathVariable("username") String username
// 发起请求的方式: 
// <a href="/user/car/1/owner/张三">发起请求:/user/car/1/owner/张三</a>

@RestController
public class ParameterTestController {
    @GetMapping("/user/car/{id}/owner/{username}")
    public Map<String,Object> test1(@PathVariable("id") Integer id,
                                     @PathVariable("username") String username,
                                     @PathVariable Map<String,String> pv){
        System.out.println("id : "+id);
        System.out.println("username: "+username);
        for (String key : pv.keySet()) {
            String value = pv.get(key);
            System.out.println("pv == "+key+":::"+value);
        }
        /* 
        *  控制台输出 :
                    id : 1
                    username: 张三
                    pv == id:::1
                    pv == username:::张三
        * */
        
        // 将数据输出到浏览器页面上.
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("username",username);
        map.put("pv",pv);
        return map;
    }
6.3.1.2 获取请求头 : @RequestHeader 用法与pathVariable相同
// <a href="/user1/car/2/owner/李四">1: 发起请求:/user/car1/2/owner/李四</a>
@GetMapping("/user1/car/{id}/owner/{username}")
public Map<String,Object> test2(@PathVariable("id") Integer id,
                                @PathVariable("username") String username,
                                @PathVariable Map<String,String> pv,

                                @RequestHeader("User-Agent") String user_agent,
                                @RequestHeader Map<String,String> reqHeader){

    System.out.println("id : "+id);
    System.out.println("username: "+username);
    for (String key : pv.keySet()) {
        String value = pv.get(key);
        System.out.println("pv == "+key+":::"+value);
    }
    System.out.println("==========================");
    System.out.println("user_agent : "+user_agent);
    for (String key : reqHeader.keySet()) {
        String value = reqHeader.get(key);
        System.out.println("reqHeader == "+key+":::"+value);
    }

    Map<String,Object> map = new HashMap<>();

    map.put("id",id);
    map.put("username",username);
    map.put("pv",pv);

    map.put("user-agent",user_agent);
    map.put("request_header",reqHeader);
    return map;
}
6.3.1.3 获取请求参数 : @RequestParam
  • 当要获取想响应的请求可以使用 带参数的 @RequestParam
  • 当接收的参数是一个数组时,可以使用 List<String> 类型的数据来接收
  • 获取所有的数据,使用 Map<String,String>类型来接收.
// <a href="/user3/car/3/owner/王五?age=20&inters=basketball&inters=game">3:发起请求</a>
@GetMapping("/user3/car/{id}/owner/{username}")
public void test3(@PathVariable("id") Integer id,
                                @PathVariable("username") String username,
                                @PathVariable Map<String,String> pv,

                                @RequestParam("age") Integer age,
                                @RequestParam("inters") List<String> inters,
                                @RequestParam Map<String,String> reqParam){

    System.out.println("id : "+id);
    System.out.println("username: "+username);
    for (String key : pv.keySet()) {
        String value = pv.get(key);
        System.out.println("pv == "+key+":::"+value);
    }
    System.out.println("==========================");
    System.out.println("age : "+age);
    inters.forEach(inter -> System.out.println("inter : "+inter));
    for (String key : reqParam.keySet()) {
        String value = reqParam.get(key);
        System.out.println("reqParam == "+key+":::"+value);
    }
    /*
    *  输出 :
        id : 3
        username: 王五
        pv == id:::3
        pv == username:::王五
        ==========================
        age : 20
        inter : basketball
        inter : game
        reqParam == age:::20
        reqParam == inters:::basketball
    * */
}
6.3.1.4 获取cookie值 : @CookieValue
  • 可以通过 浏览器的 cookie 键 来获取请求 的 cookie值, 或者 Cookie对象.
// a href="/user4/car/4/owner/赵六">发起请求: 4 </a>
@GetMapping("/user4/car/{id}/owner/{username}")
    public void test4(@PathVariable("id") Integer id,
                      @PathVariable("username") String username,
                      @PathVariable Map<String,String> pv,
				// 这里获取的是 浏览器的Cookie 键 , 必须要先查看 浏览器发起的cookie名称是否一致.
                      @CookieValue("Idea-480fa3ba") String cookieStr,
                      @CookieValue("Idea-480fa3ba") Cookie cookie){

        System.out.println("id : "+id);
        System.out.println("username: "+username);
        for (String key : pv.keySet()) {
            String value = pv.get(key);
            System.out.println("pv == "+key+":::"+value);
        }
        System.out.println("==========================");
        System.out.println("cookieStr : "+ cookieStr);
        System.out.println("cookie :"+ cookie.toString());
        /*
        *  输出 :
                   id : 4
                    username: 赵六
                    pv == id:::4
                    pv == username:::赵六
                    ==========================
                    cookieStr : 4d5cbe6a-5240-401f-b963-e5f8a5c271cc
                    cookie :javax.servlet.http.Cookie@5fee8fa1

        * */
    }
6.3.1.5 获取请求体 : @RequestBody , 只有post 才有请求体.
/* <form action="/user5" method="post">
    <input type="text" name="name" value="reqBody">
    <input type="text" name="age" value="30">
    <input type="submit" value="提交">
</form> 
*/
@PostMapping("/user5")
    public void postTest5(@RequestBody String context){
        // 获取请求体的数据
        System.out.println(context); 
        // 输出: name=reqBody&age=30
    }
6.3.1.6 获取request请求域的值: @RequestAttribute
  • 可以通过HttpServletRequest 对象来获取 存入 request作用域中的数据
  • 也可以使用 @RequestAttribute 来获取 request作用域中的数据.
// 浏览器地址栏 输入 :  http://localhost:8080/reqGoto 发起请求.

@Controller
public class RequestController {
    @GetMapping("/reqGoto")
    public String reqGoto(HttpServletRequest request){
        request.setAttribute("msg","成功处理请求");
        request.setAttribute("code",200);
        return "forward:/success";
    }
    
    @GetMapping("/success")
    @ResponseBody
    public Map success(@RequestAttribute("msg") String msg,
                       @RequestAttribute("code") Integer code,
                       HttpServletRequest request){

        String msg1 = (String) request.getAttribute("msg");
        Integer code1 = (Integer) request.getAttribute("code");


        System.out.println("msg : "+msg);
        System.out.println("code : "+code);
        System.out.println("=================");
        System.out.println("msg1 : "+msg1);
        System.out.println("code1 : "+code1);

        Map<String,Object> map = new HashMap<>();
        map.put("msg",msg);
        map.put("code",code);
        map.put("msg1",msg1);
        map.put("code1",code1);

        return map;
    }
}
/* 输出结果 : 
    msg : 成功处理请求
    code : 200
    =================
    msg1 : 成功处理请求
    code1 : 200
*/
6.3.1.7 矩阵变量 : @MatrixVariable
/*1. 矩阵变量语法:
    请求路径: <a href="/cars/sell;low=34;brand=byd,bm,dz" >矩阵变量请求 1</a>
* 2. springboot默认禁用了矩阵变量的功能.
    需要手动开启, 其原理是 使用 UrlPathHelper 进行路径解析
     removeSemicolonContent(移除分号内容)用来控制矩阵变量是否开启的.
  3. 矩阵变量必须有url路径变量才能被解析
* */
@GetMapping("/cars/{path}")
public Map carsSell1(@MatrixVariable("low") Integer low,
                  @MatrixVariable("brand") List<String> brands,
                      @PathVariable("path") String path){
    System.out.println("low : "+low);
    brands.forEach(brand -> System.out.println("brand : "+brand));

    HashMap<String, Object> map = new HashMap<>();
    map.put("low",low);
    map.put("brand",brands);
    map.put("path",path);

    return map;
}

// 当请求路径中存在多个相同的值时,如何获取对于的值? ,可以通过指定pathVar属性来获取相应的值.
//请求方式:  <a href="/boss/1;age=20/2;age=10">矩阵变量 2 </a>
    @GetMapping("/boss/{bossId}/{empId}")
    public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,
                    @MatrixVariable(value = "age",pathVar = "empId") Integer empAge){
        System.out.println("bossAge : "+bossAge);
        System.out.println("empAge :" + empAge);
        Map<String,Object> map = new HashMap<>();

        map.put("bossAge",bossAge);
        map.put("empAge",empAge);
        return map;

    }
如何开启矩阵变量呢 ? 有两种方式 ,

方式一: 实现WebMvcConfigurer 接口, 重写configurePathMatch方法

@Configuration(proxyBeanMethods = false)
public class WebConfigTest1 implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // 获取路径帮助器
        UrlPathHelper pathHelper = new UrlPathHelper();
        // 设置不移除 分号后面的内容, 这样才能让矩阵变量功能生效
        pathHelper.setRemoveSemicolonContent(false);

        configurer.setUrlPathHelper(pathHelper);
    }
}

方式二 : 向springboot 容器中注入 重写了 configurePathMatch 方法的 WebMvcConfigurer 对象

@Configuration(proxyBeanMethods = false)
public class WebConfigTest2 {

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                // 获取路径帮助器
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                // 设置不移除 分号后面的内容, 这样才能让矩阵变量功能生效
                urlPathHelper.setRemoveSemicolonContent(false);
                // 配置路径帮助器
                configurer.setUrlPathHelper(urlPathHelper);
            }
        };
    }
}
6.3.2 POJO封装
6.3.2.1 Servlet API, 即支持的请求类型

WebRequest、ServletRequest、MultipartRequest、 HttpSession、javax.servlet.http.PushBuilder、Principal、InputStream、Reader、HttpMethod、Locale、TimeZone、ZoneId

6.3.2.2 复杂参数

Map、**Model(map、model里面的数据会被放在request的请求域 request.setAttribute)、**Errors/BindingResult、RedirectAttributes( 重定向携带数据)ServletResponse(response)、SessionStatus、UriComponentsBuilder、ServletUriComponentsBuilder

Map<String,Object> map,  Model model, HttpServletRequest request 都是可以给request域中放数据,
然后统一通过 request.getAttribute(); 获取请求域中的数据.

Map、Model类型的参数,会返回 mavContainer.getModel();—> BindingAwareModelMap 是Model 也是Map

mavContainer.getModel(); 获取到值的

6.3.2.3 自定义对象类型

可以自动类型转换与格式化,可以级联封装。

/**
 *     姓名: <input name="userName"/> <br/>
 *     年龄: <input name="age"/> <br/>
 *     生日: <input name="birth"/> <br/>
 *     宠物姓名:<input name="pet.name"/><br/>
 *     宠物年龄:<input name="pet.age"/>
 */
@Data
public class Person {
    
    private String userName;
    private Integer age;
    private Date birth;
    private Pet pet;
    
}

@Data
public class Pet {

    private String name;
    private String age;

}
6.3.2.4 自定义类型参数 封装POJO的处理器

ServletModelAttributeMethodProcessor

会把请求中携带的数据 , 在内存中创建一个临时的对象, 用来存放请求中的所有数据. 即WebDataBinder 对象.

WebDataBinder binder = binderFactory.createBinder(webRequest, attribute, name);

WebDataBinder :web数据绑定器,将请求参数的值绑定到指定的JavaBean里面

WebDataBinder 利用它里面的 Converters 将请求数据转成指定的数据类型。再次封装到JavaBean中

GenericConversionService:在设置每一个值的时候,找它里面的所有converter那个可以将这个数据类型(request带来参数的数据)转换到指定的类型(JavaBean – Integer)

未来我们可以给WebDataBinder里面放自己的Converter;

// 通过自定义该方法 可以 将字符串类型 转换为我们想要的类型 
private static final class StringToNumber<T extends Number> implements Converter<String, T>
    
6.3.2.5 自定义 Converter

/** 需求 : 自定义上传 数据的逻辑, 上传宠物数据的时候, 只需要通过逗号 就能区分数据的类型.
 *     姓名: <input name="userName"/> <br/>
 *     年龄: <input name="age"/> <br/>
 *     生日: <input name="birth"/> <br/>
 *    // 宠物姓名:<input name="pet.name"/><br/>
 *     // 宠物年龄:<input name="pet.age"/>
 		宠物 <input name="pet" value="啊猫,3" /> 即对应的就是 name,age
 */

	//1、WebMvcConfigurer定制化SpringMVC的功能
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                // 不移除;后面的内容。矩阵变量功能就可以生效
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }
			// 添加格式化方法
            @Override
            public void addFormatters(FormatterRegistry registry) {
                registry.addConverter(new Converter<String, Pet>() {
					
                    // 把 String 变成 宠物对象的 转换方法.
                    // source 是请求 提交的值. 
                    @Override
                    public Pet convert(String source) {
                        // name,age  :: 啊猫,3
                        if(!StringUtils.isEmpty(source)){
                            Pet pet = new Pet();
                            String[] split = source.split(",");
                            pet.setName(split[0]);
                            pet.setAge(Integer.parseInt(split[1]));
                            return pet;
                        }
                        return null;
                    }
                });
            }
        };
    }

6.4 响应处理

6.4.1 json响应

json响应使用的是 : jackson.jar+@ResponseBody

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--web场景自动引入了json场景-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-json</artifactId>
    <version>2.3.4.RELEASE</version>
    <scope>compile</scope>
</dependency>

只有引入json依赖和添加@ResponseBody就能给前端自动返回json数据.

其底层的返回值处理器 : RequestResponseBodyMethodProcessor

返回值解析器原理

  1. 返回值处理器判断是否支持这种类型返回值 supportsReturnType
  2. 返回值处理器调用 handleReturnValue 进行处理
  3. RequestResponseBodyMethodProcessor 可以处理返回值标了@ResponseBody 注解的。
    • 利用 MessageConverters 进行处理 将数据写为json
      1. 内容协商(浏览器默认会以请求头的方式告诉服务器他能接受什么样的内容类型)
      2. 服务器最终根据自己自身的能力,决定服务器能生产出什么样内容类型的数据,
      3. SpringMVC会挨个遍历所有容器底层的 HttpMessageConverter ,看谁能处理?
        • 得到MappingJackson2HttpMessageConverter可以将对象写为json
        • 利用MappingJackson2HttpMessageConverter将对象转为json再写出去。

SpringMVC到底支持哪些返回值

ModelAndView
Model
View
ResponseEntity 
ResponseBodyEmitter
StreamingResponseBody
HttpEntity
HttpHeaders
Callable
DeferredResult
ListenableFuture
CompletionStage
WebAsyncTask@ModelAttribute 且为对象类型的
返回值标注了 @ResponseBody注解 就会使用RequestResponseBodyMethodProcessor这个处理器

HTTPMessageConverter原理

HttpMessageConverter: 消息转换器: 即判断是否支持将 此 Class类型的对象,转为MediaType类型的数据。

例子:Person对象转为JSON。或者 JSON转为Person

默认的MessageConverter

其实封装的就是springMVC中的HttpMessageConverter 对象.

6.4.2 内容协商

根据客户端接收能力不同,返回不同媒体类型的数据。

1. 引入xml依赖
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>
2. postman分别测试返回json和xml

只需要改变请求头中Accept字段。Http协议中规定的,告诉服务器本客户端可以接收的数据类型。

3. 开启浏览器参数方式内容协商功能

为了方便内容协商,开启基于请求参数的内容协商功能。

spring:
    contentnegotiation:
      favor-parameter: true  #开启请求参数内容协商模式

发请求: http://localhost:8080/test/person?format=json , 发送json数据

http://localhost:8080/test/person?format=xml , 发送xml类型的数据.

6.4.2.1 内容协商原理
  1. 判断当前响应头中是否已经有确定的媒体类型。MediaType
  2. 获取客户端(PostMan、浏览器)支持接收的内容类型。(获取客户端Accept请求头字段)【application/xml】
    • contentNegotiationManager 内容协商管理器 默认使用基于请求头的策略
    • HeaderContentNegotiationStrategy 确定客户端可以接收的内容类型
  3. 遍历循环所有当前系统的 MessageConverter,看谁支持操作这个对象(Person)
  4. 找到支持操作Person的converter,把converter支持的媒体类型统计出来。
  5. 客户端需要【application/xml】。服务端能力【10种、json、xml】
  6. 进行内容协商的最佳匹配媒体类型
  7. 用 支持 将对象转为 最佳匹配媒体类型 的converter。调用它进行转化 。
6.4.2.2 自定义 MessageConverter

实现多协议数据兼容。json、xml、x-nguyenx

  1. @ResponseBody 响应数据出去 调用RequestResponseBodyMethodProcessor 处理
  2. Processor 处理方法返回值。通过 MessageConverter 处理
  3. 所有 MessageConverter 合起来可以支持各种媒体类型数据的操作(读、写)
  4. 内容协商找到最终的 messageConverter
// 自定义 messageConverter
public class NguyenMessageConverter implements HttpMessageConverter<Person> {
    @Override
    public boolean canRead(Class<?> clazz, MediaType mediaType) {
        return false;
    }

    // 当 传入的数据类型是 person类时, 开放写入权限.
    @Override
    public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        return clazz.isAssignableFrom(Person.class);
    }

    /*
        服务器要统计所有MessageConverter 都能写出哪些内容类型
         服务器调用这个方法后 会知道可以写出 这个类型 application/x-nguyen 的数据.
    *
    * */
    @Override
    public List<MediaType> getSupportedMediaTypes() {
        return MediaType.parseMediaTypes("application/x-nguyen");
    }

    @Override
    public List<MediaType> getSupportedMediaTypes(Class<?> clazz) {
        return null;
    }

    @Override
    public Person read(Class<? extends Person> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        return null;
    }

    @Override
    public void write(Person person, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        // 自定义协议将数据写出.
        String data = person.getId()+";"+person.getName()+";"+person.getAge()+";"+person.getEmail()+";";
        // 将数据写出去
        OutputStream body = outputMessage.getBody();
        body.write(data.getBytes());
    }
}

将自定义的converter添加到容器中, 并自定义内容协商策略

@Configuration(proxyBeanMethods = false)
public class WebConfig {
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            // 自定义内容协商策略
            @Override
            public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
                Map<String, MediaType> mediaTypes = new HashMap<>();
                mediaTypes.put("json",MediaType.APPLICATION_JSON);
                mediaTypes.put("xml",MediaType.APPLICATION_XML);
                mediaTypes.put("x-nguyen",MediaType.parseMediaType("application/x-nguyen"));
                // 指定支持解析 参数对应的媒体类型.
                ParameterContentNegotiationStrategy strategy = new ParameterContentNegotiationStrategy(mediaTypes);
                HeaderContentNegotiationStrategy headerStrategy = new HeaderContentNegotiationStrategy();
                configurer.strategies(Arrays.asList(strategy,headerStrategy));
            }
            // 添加 消息转换器
            @Override
            public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
                converters.add(new NguyenMessageConverter());
            }
        };
    }
}

除了自定义这些内容,也可以参照SpringBoot官方文档web开发内容协商章节直接使用springboot提供的配置方法.

6.5 视图解析与模板引擎

视图解析:SpringBoot默认不支持 JSP,需要引入第三方模板引擎技术实现页面渲染。

6.5.1 视图解析
6.5.1.1 视图解析原理流程

1、目标方法处理的过程中,所有数据都会被放在 ModelAndViewContainer 里面。包括数据和视图地址

2、方法的参数是一个自定义类型对象(从请求参数中确定的),把他重新放在 ModelAndViewContainer

3、任何目标方法执行完成以后都会返回 ModelAndView(数据和视图地址)。

**4、**processDispatchResult 处理派发结果(页面改如何响应)

  • 1、render(mv, request, response); 进行页面渲染逻辑

    • 1、根据方法的String返回值得到 View 对象【定义了页面的渲染逻辑】
      • 1、所有的视图解析器尝试是否能根据当前返回值得到View对象
      • 2、得到了 redirect:/main.html --> Thymeleaf new RedirectView()
      • 3、ContentNegotiationViewResolver 里面包含了下面所有的视图解析器,内部还是利用下面所有视图解析器得到视图对象。
      • 4、view.render(mv.getModelInternal(), request, response); 视图对象调用自定义的render进行页面渲染工作
        • RedirectView 如何渲染【重定向到一个页面】
        • 1、获取目标url地址
        • **2、**response.sendRedirect(encodedURL);
6.5.1.2 视图解析
    • 返回值以 forward: 开始: new InternalResourceView(forwardUrl); --> 转发****request.getRequestDispatcher(path).forward(request, response);
    • 返回值以 redirect: 开始: new RedirectView() --> render就是重定向
    • 返回值是普通字符串: new ThymeleafView()—> 自定义视图解析器+自定义视图;
6.5.2 模板引擎-Thymeleaf
6.5.2.1 thymeleaf简介

Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text.

现代化、服务端Java模板引擎

6.5.2.2 基本语法
1. 表达式
表达式名字语法用途
变量取值${…}获取请求域、session域、对象等值
选择变量*{…}获取上下文对象值
消息#{…}获取国际化等值
链接@{…}生成链接
片段表达式~{…}jsp:include 作用,引入公共页面片段
2. 字面量

文本值: ‘one text’ , ‘Another one!’ **,…**数字: 0 , 34 , 3.0 , 12.3 **,…**布尔值: true , false

空值: null

变量: one,two,… 变量不能有空格

3. 文本操作

字符串拼接: +

变量替换: |The name is ${name}|

4. 数学运算

运算符: + , - , * , / , %

5. 布尔运算

运算符: and , or

一元运算: ! , not

6. 比较运算

比较: > , < , >= , <= ( gt , lt , ge , le **)**等式: == , != ( eq , ne )

7. 条件运算

If-then: (if) ? (then)

If-then-else: (if) ? (then) : (else)

Default: (value) ?: (defaultvalue)

8. 特殊操作

无操作: _

9. 设置属性值-th:attr

设置单个值

<form action="subscribe.html" th:attr="action=@{/subscribe}">
  <fieldset>
    <input type="text" name="email" />
    <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
  </fieldset>
</form>

设置多个值

<img src="../../images/gtvglogo.png"  th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />

以上两个的代替写法 th:xxxx

<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>
<form action="subscribe.html" th:action="@{/subscribe}">

所有h5兼容的标签写法

https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes

10. 迭代
<tr th:each="prod : ${prods}">
        <td th:text="${prod.name}">Onions</td>
        <td th:text="${prod.price}">2.41</td>
        <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
  <td th:text="${prod.name}">Onions</td>
  <td th:text="${prod.price}">2.41</td>
  <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
11. 条件运算
<a href="comments.html"
th:href="@{/product/comments(prodId=${prod.id})}"
th:if="${not #lists.isEmpty(prod.comments)}">view</a>
<div th:switch="${user.role}">
  <p th:case="'admin'">User is an administrator</p>
  <p th:case="#{roles.manager}">User is a manager</p>
  <p th:case="*">User is some other thing</p>
</div>
6.5.2.4 thymeleaf使用

1.引入Starter

// 声明使用 <html xmlns:th="http://www.thymeleaf.org">

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2.springboot自动配置了thymeleaf,直接使用即可

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class })
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class })
public class ThymeleafAutoConfiguration { }

其配置规则:

  • 所有thymeleaf的配置值都在 ThymeleafProperties
  • 配置好了 SpringTemplateEngine
  • 配好了 ThymeleafViewResolver
  • 我们只需要直接开发页面
  public static final String DEFAULT_PREFIX = "classpath:/templates/";

  public static final String DEFAULT_SUFFIX = ".html";  //xxx.html

6.6 拦截器

6.6.1 HandlerInterceptor 接口

通过实现接口实现拦截功能. 具体内容查看springmvc知识点

/**
 * 登录检查
 * 1、配置好拦截器要拦截哪些请求
 * 2、把这些配置放在容器中
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 目标方法执行之前
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String requestURI = request.getRequestURI();
        log.info("preHandle拦截的请求路径是{}",requestURI);

        //登录检查逻辑
        HttpSession session = request.getSession();

        Object loginUser = session.getAttribute("loginUser");

        if(loginUser != null){
            //放行
            return true;
        }

        //拦截住。未登录。跳转到登录页
        request.setAttribute("msg","请先登录");
//        re.sendRedirect("/");
        request.getRequestDispatcher("/").forward(request,response);
        return false;
    }

    /**
     * 目标方法执行完成以后
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("postHandle执行{}",modelAndView);
    }

    /**
     * 页面渲染以后
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("afterCompletion执行异常{}",ex);
    }

6.6.2 配置拦截器
/**
 * 1、编写一个拦截器实现HandlerInterceptor接口
 * 2、拦截器注册到容器中(实现WebMvcConfigurer的addInterceptors)
 * 3、指定拦截规则【如果是拦截所有,静态资源也会被拦截】
 */
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")  //所有请求都被拦截包括静态资源
                .excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**"); //放行的登录页面和静态资源的请求
    }
}
6.6.3 拦截器原理

1、根据当前请求,找到**HandlerExecutionChain【**可以处理请求的handler以及handler的所有 拦截器】

2、先来顺序执行 所有拦截器的 preHandle方法

  • 1、如果当前拦截器prehandler返回为true。则顺序执行下一个拦截器的preHandle
  • 2、如果当前拦截器返回为false。直接倒序执行所有已经执行了的拦截器的 afterCompletion;

3、如果任何一个拦截器返回false。直接跳出不执行目标方法

4、所有拦截器都返回True。执行目标方法

5、倒序执行所有拦截器的postHandle方法。

6、前面的步骤有任何异常都会直接倒序触发 afterCompletion

7、页面成功渲染完成以后,也会倒序触发 afterCompletion

6.7 文件上传

6.7.1 页面表单
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form th:action="/upload">
        用户名: <input type="text" name="username"><br/>
        邮箱:<input type="text" name="email"><br/>
        头像: <input name="headerImg" type="file"><br/>
        多图片:<input name="photos" type="file" multiple><br/>
        <input type="submit" value="提交">
    </form>
</body>
</html>
6.7.2 文件上传到服务器
    /**
     * MultipartFile 自动封装上传过来的文件
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("email") String email,
                         @RequestParam("username") String username,
                         @RequestPart("headerImg") MultipartFile headerImg,
                         @RequestPart("photos") MultipartFile[] photos) throws IOException {
		// 输出日志信息
        log.info("上传的信息:email={},username={},headerImg={},photos={}",
                email,username,headerImg.getSize(),photos.length);
		// 当上传的文件不为空时,将文件保存到服务器
        if(!headerImg.isEmpty()){
            //保存到文件服务器,OSS服务器
            String originalFilename = headerImg.getOriginalFilename();
            headerImg.transferTo(new File("H:\\cache\\"+originalFilename));
        }
        if(photos.length > 0){
            for (MultipartFile photo : photos) {
                if(!photo.isEmpty()){
                    String originalFilename = photo.getOriginalFilename();
                    photo.transferTo(new File("H:\\cache\\"+originalFilename));
                }
            }
        }
        return "main";
    }
6.7.3 相关配置
# 设置上传单个文件的最大为10MB,所有文件的最大 100MB
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB
6.7.4 文件上传原理

**文件上传自动配置类-MultipartAutoConfiguration-**MultipartProperties

  • 自动配置好了 StandardServletMultipartResolver 【文件上传解析器】

  • 原理步骤

    • 1、请求进来使用文件上传解析器判断(isMultipart)并封装(resolveMultipart,返回MultipartHttpServletRequest)文件上传请求
    • 2、参数解析器来解析请求中的文件内容封装成MultipartFile
    • **3、将request中文件信息封装为一个Map;**MultiValueMap<String, MultipartFile>

FileCopyUtils。实现文件流的拷贝

6.8 异常处理

6.8.1 错误处理默认规则
  • 默认情况下,Spring Boot提供/error处理所有错误的映射
  • 对于机器客户端,它将生成JSON响应,其中包含错误,HTTP状态和异常消息的详细信息。对于浏览器客户端,响应一个“ whitelabel”错误视图,以HTML格式呈现相同的数据
src/
 +- main/
     +- java/
     |   + <source code>
     +- resources/
         +- public/
             +- error/
             |   +- 404.html
             +- <other public assets>
  • 要完全替换默认行为,可以实现 ErrorController并注册该类型的Bean定义,或添加ErrorAttributes类型的组件以使用现有机制但替换其内容。
  • error/下的4xx,5xx页面会被自动解析;
6.8.2 定制错误处理逻辑
  • 自定义错误页

    • error/404.html error/5xx.html;有精确的错误状态码页面就匹配精确,没有就找 4xx.html;如果都没有就触发白页
  • @ControllerAdvice+@ExceptionHandler处理全局异常;底层是 ExceptionHandlerExceptionResolver 支持的

  • @ResponseStatus+自定义异常 ;底层是 ResponseStatusExceptionResolver ,把responsestatus注解的信息底层调用 response.sendError(statusCode, resolvedReason);tomcat发送的/error

  • Spring底层的异常,如 参数类型转换异常;DefaultHandlerExceptionResolver 处理框架底层的异常。

    • response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage());
6.8.3 异常处理自动配置原理
  • ErrorMvcAutoConfiguration 自动配置异常处理规则

    • 容器中的组件:类型:DefaultErrorAttributes -> id:errorAttributes
      • public class DefaultErrorAttributes implements ErrorAttributes, HandlerExceptionResolver
      • DefaultErrorAttributes:定义错误页面中可以包含的数据。
    • **容器中的组件:类型:**BasicErrorController --> id:basicErrorController(json+白页 适配响应)
      • 处理默认 /error 路径的请求;页面响应 new ModelAndView(“error”, model);
      • 容器中有组件 View->id是error;(响应默认错误页)
      • 容器中放组件 BeanNameViewResolver(视图解析器);按照返回的视图名作为组件的id去容器中找View对象。
    • **容器中的组件:**类型:**DefaultErrorViewResolver -> id:**conventionErrorViewResolver
      • 如果发生错误,会以HTTP的状态码 作为视图页地址(viewName),找到真正的页面
      • error/404、5xx.html

如果想要返回页面;就会找error视图【StaticView】。(默认是一个白页)

6.8.4 异常处理步骤流程

1、执行目标方法,目标方法运行期间有任何异常都会被catch、而且标志当前请求结束;并且用 dispatchException

2、进入视图解析流程(页面渲染?)

processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);

3、mv = processHandlerException;处理handler发生的异常,处理完成返回ModelAndView;

  • 1、遍历所有的 handlerExceptionResolvers,看谁能处理当前异常**[HandlerExceptionResolver处理器异常解析器]**

  • 2、系统默认的 异常解析器;

  • 1、DefaultErrorAttributes先来处理异常。把异常信息保存到rrequest域,并且返回null;

  • 2、默认没有任何人能处理异常,所以异常会被抛出

      • 1、如果没有任何人能处理最终底层就会发送 /error 请求。会被底层的BasicErrorController处理
      • 2、解析错误视图;遍历所有的 ErrorViewResolver 看谁能解析。
      • 3、默认的 DefaultErrorViewResolver ,作用是把响应状态码作为错误页的地址,error/500.html
      • 4、模板引擎最终响应这个页面 error/500.html
// 处理 整个web controller的异常.
@ControllerAdvice
public class GlobalExceptionHandler {
    // 处理异常,空指针异常,类未找到异常.
    @ExceptionHandler({NullPointerException.class,ClassCastException.class})
    public String myException(){
        return "err";// 返回的是视图地址,也可以使用ModelAndView
    }

}

6.9 Web原生组件注入(Servlet、Filter、Listener)

6.9.1 使用Servlet API

@ServletComponentScan(basePackages = “com.atguigu.admin”) :指定原生Servlet组件的位置, 这个注解写在springboot主方法那边.

@WebServlet(urlPatterns = “/my”):效果:直接响应,没有经过Spring的拦截器?

@WebServlet(urlPatterns = "/my")
public class servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}

@WebFilter(urlPatterns={"/css/*","/images/*"})

@WebListener

扩展:DispatchServlet 如何注册进来

  • 容器中自动配置了 DispatcherServlet 属性绑定到 WebMvcProperties;对应的配置文件配置项是 spring.mvc。

  • 通过 ServletRegistrationBean 把 DispatcherServlet 配置进来。

  • 默认映射的是 / 路径。

6.9.2 使用RegistrationBean

ServletRegistrationBean, FilterRegistrationBean, and ServletListenerRegistrationBean

@Configuration
public class MyRegistConfig {

    @Bean
    public ServletRegistrationBean myServlet(){
        MyServlet myServlet = new MyServlet();

        return new ServletRegistrationBean(myServlet,"/my","/my02");
    }


    @Bean
    public FilterRegistrationBean myFilter(){

        MyFilter myFilter = new MyFilter();
//        return new FilterRegistrationBean(myFilter,myServlet());
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*"));
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean myListener(){
        MySwervletContextListener mySwervletContextListener = new MySwervletContextListener();
        return new ServletListenerRegistrationBean(mySwervletContextListener);
    }
}

6.10 嵌入式Servlet容器

6.10.1 切换嵌入式Servlet容器
  • 默认支持的webServer

    • Tomcat, Jetty, or Undertow
    • ServletWebServerApplicationContext 容器启动寻找ServletWebServerFactory 并引导创建服务器
  • 切换服务器

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
6.10.2 定制Servlet容器
  • 实现 WebServerFactoryCustomizer

  • 把配置文件的值和ServletWebServerFactory 进行绑定

  • 修改配置文件 server.xxx

  • 直接自定义 ConfigurableServletWebServerFactory

xxxxxCustomizer:定制化器,可以改变xxxx的默认规则

import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(9000);
    }

}

6.11 定制化原理

6.11.1 定制化的常见方式
  • 修改配置文件;

  • xxxxxCustomizer;

  • 编写自定义的配置类 xxxConfiguration;+ @Bean替换、增加容器中默认组件;视图解析器

  • Web应用 编写一个配置类实现 WebMvcConfigurer 即可定制化web功能;+ @Bean给容器中再扩展一些组件

@Configuration
public class AdminWebConfig implements WebMvcConfigurer
  • @EnableWebMvc + WebMvcConfigurer —— @Bean 可以全面接管SpringMVC,所有规则全部自己重新配置; 实现定制和扩展功能

    • 原理
    • 1、WebMvcAutoConfiguration 默认的SpringMVC的自动配置功能类。静态资源、欢迎页…
    • 2、一旦使用 @EnableWebMvc 、。会 @Import(DelegatingWebMvcConfiguration.class)
    • 3、DelegatingWebMvcConfiguration 的 作用,只保证SpringMVC最基本的使用
      • 把所有系统中的 WebMvcConfigurer 拿过来。所有功能的定制都是这些 WebMvcConfigurer 合起来一起生效
      • 自动配置了一些非常底层的组件。RequestMappingHandlerMapping、这些组件依赖的组件都是从容器中获取
      • public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport
    • 4、WebMvcAutoConfiguration 里面的配置要能生效 必须 @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
    • 5、@EnableWebMvc 导致了 WebMvcAutoConfiguration 没有生效。
  • … …

6.11.2 原理分析套路

场景starter - xxxxAutoConfiguration - 导入xxx组件 - 绑定xxxProperties – 绑定配置文件项

第七章 数据访问

7.1 SQL

7.1.1 数据源的自动配置-HikariDataSource
7.1.1.1 导入JDBC场景
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
7.1.1.2 导入驱动

数据库要和驱动的版本对应.

默认版本:<mysql.version>8.0.22</mysql.version>


想要修改版本
1、直接依赖引入具体版本(maven的就近依赖原则)
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.49</version>
</dependency>

2、重新声明版本(maven的属性的就近优先原则)
<properties>
    <java.version>1.8</java.version>
    <mysql.version>5.1.49</mysql.version>
</properties>
7.1.1.3 分析自动配置

自动配置的类

  • DataSourceAutoConfiguration : 数据源的自动配置

    • 修改数据源相关的配置:spring.datasource
    • 数据库连接池的配置,是自己容器中没有DataSource才自动配置的
    • 底层配置好的连接池是:HikariDataSource
// 导入默认的数据源
@Configuration(proxyBeanMethods = false)
@Conditional(PooledDataSourceCondition.class)
@ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
@Import({ DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class,
         DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.OracleUcp.class,
         DataSourceConfiguration.Generic.class, DataSourceJmxConfiguration.class })
protected static class PooledDataSourceConfiguration
  • DataSourceTransactionManagerAutoConfiguration: 事务管理器的自动配置

  • JdbcTemplateAutoConfiguration: JdbcTemplate的自动配置,可以来对数据库进行crud

    • 可以修改这个配置项@ConfigurationProperties(prefix = “spring.jdbc”) 来修改JdbcTemplate
    • @Bean@Primary JdbcTemplate;容器中有这个组件
  • JndiDataSourceAutoConfiguration: jndi的自动配置

  • XADataSourceAutoConfiguration: 分布式事务相关的

7.1.1.4 修改配置项
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db_account
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
7.1.1.5 测试
@Slf4j
@SpringBootTest
class Boot05WebAdminApplicationTests {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Test
    void contextLoads() {

//        jdbcTemplate.queryForObject("select * from account_tbl")
//        jdbcTemplate.queryForList("select * from account_tbl",)
        Long aLong = jdbcTemplate.queryForObject("select count(*) from account_tbl", Long.class);
        log.info("记录总数:{}",aLong);
    }

}

7.1.2 使用Druid数据源

7.1.2.1 druid官方github地址

https://github.com/alibaba/druid

整合第三方技术的两种方式

自定义方式
7.1.2.1 创建数据源
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.17</version>
        </dependency>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
    destroy-method="close">
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxActive" value="20" />
    <property name="initialSize" value="1" />
    <property name="maxWait" value="60000" />
    <property name="minIdle" value="1" />
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <property name="minEvictableIdleTimeMillis" value="300000" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <property name="poolPreparedStatements" value="true" />
    <property name="maxOpenPreparedStatements" value="20" />
7.1.2.2 StatViewServlet

StatViewServlet的用途包括:

  • 提供监控信息展示的html页面

  • 提供监控信息的JSON API

  <servlet>
    <servlet-name>DruidStatView</servlet-name>
    <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DruidStatView</servlet-name>
    <url-pattern>/druid/*</url-pattern>
  </servlet-mapping>
7.1.2.3 StatFilter

用于统计监控信息;如SQL监控、URI监控

需要给数据源中配置如下属性;可以允许多个filter,多个用,分割;如:

<property name="filters" value="stat,slf4j" />

系统中所有filter:

别名Filter类名
defaultcom.alibaba.druid.filter.stat.StatFilter
statcom.alibaba.druid.filter.stat.StatFilter
mergeStatcom.alibaba.druid.filter.stat.MergeStatFilter
encodingcom.alibaba.druid.filter.encoding.EncodingConvertFilter
log4jcom.alibaba.druid.filter.logging.Log4jFilter
log4j2com.alibaba.druid.filter.logging.Log4j2Filter
slf4jcom.alibaba.druid.filter.logging.Slf4jLogFilter
commonloggingcom.alibaba.druid.filter.logging.CommonsLogFilter

慢SQL记录配置

<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
    <property name="slowSqlMillis" value="10000" />
    <property name="logSlowSql" value="true" />
</bean>

使用 slowSqlMillis 定义慢SQL的时长
使用官方starter方式
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.17</version>
</dependency>
7.1.2.5 分析自动配置
  • 扩展配置项 spring.datasource.druid

  • DruidSpringAopConfiguration.class, 监控SpringBean的;配置项:spring.datasource.druid.aop-patterns

  • DruidStatViewServletConfiguration.class, 监控页的配置:spring.datasource.druid.stat-view-servlet;默认开启

  • DruidWebStatFilterConfiguration.class, web监控配置;spring.datasource.druid.web-stat-filter;默认开启

  • DruidFilterConfiguration.class}) 所有Druid自己filter的配置

7.1.2.5 配置示例
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/jdbc
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

    druid:
      aop-patterns: com.atguigu.admin.*  #监控SpringBean
      filters: stat,wall     # 底层开启功能,stat(sql监控),wall(防火墙)

      stat-view-servlet:   # 配置监控页功能
        enabled: true
        login-username: admin
        login-password: admin
        resetEnable: false

      web-stat-filter:  # 监控web
        enabled: true
        urlPattern: /*
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'


      filter:
        stat:    # 对上面filters里面的stat的详细配置
          slow-sql-millis: 1000
          logSlowSql: true
          enabled: true
        wall:
          enabled: true
          config:
            drop-table-allow: false

SpringBoot配置示例

https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

配置项列表https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8

整合MyBatis操作

https://github.com/mybatis

SpringBoot官方的Starter:spring-boot-starter-*

第三方的: *-spring-boot-starter

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>
1. 配置模式
  • 全局配置文件

  • SqlSessionFactory: 自动配置好了

  • SqlSession:自动配置了 SqlSessionTemplate 组合了SqlSession

  • @Import(AutoConfiguredMapperScannerRegistrar.class);

  • Mapper: 只要我们写的操作MyBatis的接口标准了 @Mapper 就会被自动扫描进来

@EnableConfigurationProperties(MybatisProperties.class)MyBatis配置项绑定类。
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
public class MybatisAutoConfiguration{}

@ConfigurationProperties(prefix = "mybatis")
public class MybatisProperties

可以修改配置文件中 mybatis 的所有配置;

# 配置mybatis规则
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml  #全局配置文件位置
  mapper-locations: classpath:mybatis/mapper/*.xml  #sql映射文件位置
  
Mapper接口--->绑定Xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.admin.mapper.AccountMapper">
<!--    public Account getAcct(Long id); -->
    <select id="getAcct" resultType="com.atguigu.admin.bean.Account">
        select * from  account_tbl where  id=#{id}
    </select>
</mapper>

配置 private Configuration configuration; mybatis.configuration下面的所有,就是相当于改mybatis全局配置文件中的值

# 配置mybatis规则
mybatis:
#  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true
    
 可以不写全局;配置文件,所有全局配置文件的配置都放在configuration配置项中即可
  • 导入mybatis官方starter

  • 编写mapper接口。标准@Mapper注解

  • 编写sql映射文件并绑定mapper接口

  • 在application.yaml中指定Mapper配置文件的位置,以及指定全局配置文件的信息 (建议;配置在mybatis.configuration

2. 注解模式
@Mapper
public interface CityMapper {

    @Select("select * from city where id=#{id}")
    public City getById(Long id);

    public void insert(City city);

}
整合MyBatis-Plus

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

mybatis plus 官网

建议安装 MybatisX 插件

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>

自动配置

●MybatisPlusAutoConfiguration 配置类,MybatisPlusProperties 配置项绑定。mybatis-plus:xxx 就是对mybatis-plus的定制

SqlSessionFactory 自动配置好。底层是容器中默认的数据源

mapperLocations 自动配置好的。有默认值。classpath*:/mapper/**/*.xml;任意包的类路径下的所有mapper文件夹下任意路径下的所有xml都是sql映射文件。 建议以后sql映射文件,放在 mapper下

容器中也自动配置好了 SqlSessionTemplate

●@Mapper 标注的接口也会被自动扫描;建议直接 @MapperScan(“com.atguigu.admin.mapper”) 批量扫描就行

优点:

● 只需要我们的Mapper继承 BaseMapper 就可以拥有crud能力

3、CRUD功能

    @GetMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable("id") Long id,
                             @RequestParam(value = "pn",defaultValue = "1")Integer pn,
                             RedirectAttributes ra){

        userService.removeById(id);

        ra.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }


    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value="pn",defaultValue = "1") Integer pn,Model model){
        //表格内容的遍历
//        response.sendError
//     List<User> users = Arrays.asList(new User("zhangsan", "123456"),
//                new User("lisi", "123444"),
//                new User("haha", "aaaaa"),
//                new User("hehe ", "aaddd"));
//        model.addAttribute("users",users);
//
//        if(users.size()>3){
//            throw new UserTooManyException();
//        }
        //从数据库中查出user表中的用户进行展示

        //构造分页参数
        Page<User> page = new Page<>(pn, 2);
        //调用page进行分页
        Page<User> userPage = userService.page(page, null);


//        userPage.getRecords()
//        userPage.getCurrent()
//        userPage.getPages()


        model.addAttribute("users",userPage);

        return "table/dynamic_table";
    }
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {


}

public interface UserService extends IService<User> {

}

2、NoSQL

Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings)散列(hashes)列表(lists)集合(sets)有序集合(sorted sets) 与范围查询, bitmapshyperloglogs地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication)LUA脚本(Lua scripting)LRU驱动事件(LRU eviction)事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。

  1. Redis自动配置
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

perLocations 自动配置好的。有默认值。classpath*:/mapper/**/*.xml;任意包的类路径下的所有mapper文件夹下任意路径下的所有xml都是sql映射文件。 建议以后sql映射文件,放在 mapper下

容器中也自动配置好了 SqlSessionTemplate

●@Mapper 标注的接口也会被自动扫描;建议直接 @MapperScan(“com.atguigu.admin.mapper”) 批量扫描就行

优点:

● 只需要我们的Mapper继承 BaseMapper 就可以拥有crud能力

3、CRUD功能

    @GetMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable("id") Long id,
                             @RequestParam(value = "pn",defaultValue = "1")Integer pn,
                             RedirectAttributes ra){

        userService.removeById(id);

        ra.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }


    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value="pn",defaultValue = "1") Integer pn,Model model){
        //表格内容的遍历
//        response.sendError
//     List<User> users = Arrays.asList(new User("zhangsan", "123456"),
//                new User("lisi", "123444"),
//                new User("haha", "aaaaa"),
//                new User("hehe ", "aaddd"));
//        model.addAttribute("users",users);
//
//        if(users.size()>3){
//            throw new UserTooManyException();
//        }
        //从数据库中查出user表中的用户进行展示

        //构造分页参数
        Page<User> page = new Page<>(pn, 2);
        //调用page进行分页
        Page<User> userPage = userService.page(page, null);


//        userPage.getRecords()
//        userPage.getCurrent()
//        userPage.getPages()


        model.addAttribute("users",userPage);

        return "table/dynamic_table";
    }
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {


}

public interface UserService extends IService<User> {

}

2、NoSQL

Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings)散列(hashes)列表(lists)集合(sets)有序集合(sorted sets) 与范围查询, bitmapshyperloglogs地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication)LUA脚本(Lua scripting)LRU驱动事件(LRU eviction)事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。

  1. Redis自动配置
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以和你分享一些关于Spring Boot学习笔记。 1. Spring Boot是什么? Spring Boot是一个基于Spring框架的快速开发框架,它能够帮助开发者快速搭建Spring项目,简化了Spring应用开发的繁琐过程,提高了开发效率。 2. Spring Boot的优点有哪些? Spring Boot的优点有很多,其中包括: - 简化了Spring应用的开发,提高了开发效率; - 集成了很多常用的第三方库,减少了依赖管理的工作; - 自动化配置,减少了配置文件的编写工作; - 内嵌了Tomcat等Web容器,使得应用的部署更加便捷; - 提供了Actuator等模块,使得应用的监控和管理更加便捷。 3. Spring Boot的核心注解有哪些? Spring Boot的核心注解包括: - @SpringBootApplication:标注在启动类上,代表这是一个Spring Boot应用; - @Controller:标注在控制器类上,处理HTTP请求; - @Service:标注在服务类上,用于处理业务逻辑; - @Repository:标注在数据访问类上,用于数据库访问; - @Configuration:标注在配置类上,用于配置Spring应用上下文。 4. Spring Boot的配置文件有哪些? Spring Boot的配置文件包括: - application.properties:基于Key-Value的属性文件; - application.yml:基于YAML语法的配置文件。 5. 如何使用Spring Boot集成数据库? 使用Spring Boot集成数据库需要完成以下几个步骤: - 在pom.xml中添加相关数据库依赖; - 配置数据源和JPA/Hibernate等相关配置; - 编写实体类和DAO层代码。 以上就是一些关于Spring Boot学习笔记,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值