1. 程式人生 > 其它 >SpringBoot2核心技術-核心功能 2 Web開發

SpringBoot2核心技術-核心功能 2 Web開發

1、SpringMVC自動配置概覽 2、簡單功能分析 3、請求引數處理 4、資料響應與內容協商 5、檢視解析與模板引擎 6、攔截器 7、檔案上傳 8、異常處理 9、Web原生元件注入(Servlet、Filter、Listener) 10、嵌入式Servlet容器 11、定製化原理

https://www.yuque.com/atguigu/springboot/vgzmgh

二 Web開發

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

2、簡單功能分析

2.1、靜態資源訪問

1、靜態資源目錄

只要靜態資源放在類路徑下: called /static (or /public or /resources or /META-INF/resources
訪問 : 當前專案根路徑/ + 靜態資源名

原理: 靜態對映/**。
請求進來,先去找Controller看能不能處理。不能處理的所有請求又都交給靜態資源處理器。靜態資源也找不到則響應404頁面

改變預設的靜態資源路徑

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

  resources:
    static-locations: [classpath:/haha/]
2、靜態資源訪問字首

預設無字首

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

當前專案 + static-path-pattern + 靜態資源名 = 靜態資原始檔夾下找

3、webjar

自動對映 /webjars/**
https://www.webjars.org/

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.5.1</version>
        </dependency>

問地址:http://localhost:8080/webjars/jquery/3.5.1/jquery.js 後面地址要按照依賴裡面的包路徑

2.2、歡迎頁支援

● 靜態資源路徑下  index.html
  ○ 可以配置靜態資源路徑
  ○ 但是不可以配置靜態資源的訪問字首。否則導致 index.html不能被預設訪問
spring:
#  mvc:
#    static-path-pattern: /res/**   這個會導致welcome page功能失效

  resources:
    static-locations: [classpath:/haha/]

● controller能處理/index

2.3、自定義 Favicon

favicon.ico 放在靜態資源目錄下即可。

spring:
#  mvc:
#    static-path-pattern: /res/**   這個會導致 Favicon 功能失效

2.4、靜態資源配置原理

● SpringBoot啟動預設載入 xxxAutoConfiguration 類(自動配置類)
● SpringMVC功能的自動配置類 WebMvcAutoConfiguration,生效

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {}

● 給容器中配了什麼。

	@Configuration(proxyBeanMethods = false)
	@Import(EnableWebMvcConfiguration.class)
	@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
	@Order(0)
	public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {}

●配置檔案的相關屬性和xxx進行了繫結。

WebMvcProperties==spring.mvc、
ResourceProperties==spring.resources

3、請求引數處理

4、資料響應與內容協商

5、檢視解析與模板引擎

6、攔截器

7、檔案上傳

8、異常處理

9、Web原生元件注入(Servlet、Filter、Listener)

10、嵌入式Servlet容器

11、定製化原理