六.SpringBoot與Web(SpringMVC配置)

前言:
Thymeleaf前面已經介紹了,下面介紹的是關于SpringMVC的內容~ 基本知識其實和我們以前接觸過的SpingMVC差不多
具體的一些詳解可以看官方的文檔:
https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications

1. Spring MVC auto-configuration

我們知道了SpingBoot自動配置好了SpringMVC,我們省了很多配置文件的時間,那么我們就來看看
我們從官方文檔入手,我們可以看看WebMvcAutoConfiguration

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

    • 自動配置了ViewResolver(視圖解析器:根據方法的返回值得到視圖對象(View),視圖對象決定如何渲染
    • ContentNegotiatingViewResolver:組合所有的視圖解析器的;
    • 如何定制:我們可以自己給容器中添加一個視圖解析器;自動的將其組合進來;
  • Support for serving static resources, including support for WebJars (see below).靜態資源文件夾路徑,webjars

  • Static index.html support. 靜態首頁訪問

  • Custom Favicon support (see below). favicon.ico

    ?

  • 自動注冊了 of Converter, GenericConverter, Formatter beans.

    • Converter:轉換器; public String hello(User user):類型轉換使用Converter
    • Formatter 格式化器; 2017.12.17===Date;
        @Bean
        @ConditionalOnProperty(prefix = "spring.mvc", name = "date-format")//在文件中配置日期格式化的規則
        public Formatter<Date> dateFormatter() {
            return new DateFormatter(this.mvcProperties.getDateFormat());//日期格式化組件
        }

? 自己添加的格式化器轉換器,我們只需要放在容器中即可

  • Support for HttpMessageConverters (see below).

    • HttpMessageConverter:SpringMVC用來轉換Http請求和響應的;User---Json(User對象轉換成Json格式);

    • HttpMessageConverters 是從容器中確定;獲取所有的HttpMessageConverter;

      自己給容器中添加HttpMessageConverter,只需要將自己的組件注冊容器中(@Bean,@Component)

      ?

  • Automatic registration of MessageCodesResolver (see below).定義錯誤代碼生成規則

  • Automatic use of a ConfigurableWebBindingInitializer bean (see below).

    我們可以配置一個ConfigurableWebBindingInitializer來替換默認的;(添加到容器)

    初始化WebDataBinder;
    請求數據=====JavaBean;
    

org.springframework.boot.autoconfigure.web:web的所有自動場景;

image.png

If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Configuration class of type WebMvcConfigurerAdapter, but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you can declare a WebMvcRegistrationsAdapter instance providing such components.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

2、擴展SpringMVC

我們以前配置一個映射路徑是這樣的

<mvc:view‐controller path="/hello" view‐name="success"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean></bean>
</mvc:interceptor>
</mvc:interceptors>

現在我們可以使用SpringBoot的方式實現
1.實現WebMvcConfigurer或者繼承WebMvcConfigurerAdapter
2.重寫addViewControllers方法并記得在加上注釋
@Configuration


image.png
image.png
@Configuration
public class MyConfig implements WebMvcConfigurer {
    //設置映射的路徑
    public void addViewControllers(ViewControllerRegistry viewControllerRegistry) {
        //瀏覽器發送 /hello 請求來到 hello
        viewControllerRegistry.addViewController("/hello").setViewName("hello");
    }

}

原理:
1)、WebMvcAutoConfiguration是SpringMVC的自動配置類
2)、在做其他自動配置時會導入;@Import(EnableWebMvcConfiguration.class)


image.png

3)、容器中所有的WebMvcConfigurer都會一起起作用;
4)、我們的配置類也會被調用;
效果:SpringMVC的自動配置和我們的擴展配置都會起作用;

2、全面接管SpringMVC

如果我們想全面接管SpringBoot中的SpringMVC配置,我們可以自己配置;
我們只需要加個@EnableWebMvc注解即可
原理:
1)@EnableWebMvc的核心

@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {

2)我們點進去可以看到這個類

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
  1. 再看回WebMvcAutoConfiguration
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
WebMvcConfigurerAdapter.class })
//容器中沒有這個組件的時候,這個自動配置類才生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

當容器中沒有WebMvcConfigurationSupport這個組件的時候整個SpringMVC的自動配置就會失效
4)@EnableWebMvc將WebMvcConfigurationSupport組件導入進來;
就是原來SpringBoot中的配置會被替換成我們設置的
5)不過我們不要隨便改掉配置,我們只要組擴展就好了。因為本來SpringBoot的自動配置本來給我們提供的就是一些最基本的功能~我們去掉,自己再配不是做無用功嗎
總結:
1.SpringBoot在自動配置很多組件的時候,先看容器中有沒有用戶自己配置的(@Bean、@Component)如果有就用用戶配置的,如果沒有,才自動配置;如果有些組件可以有多個(ViewResolver)將用戶配置的和自己默認的組合起來
2)、在SpringBoot中會有非常多的xxxConfigurer幫助我們進行擴展配置
3)、在SpringBoot中會有很多的xxxCustomizer幫助我們進行定制配置

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。