2021年1月14日 星期四

[SpringBoot 1.5] SpringMVC自動配置

 

以下是SpringBoot文件的網址

https://docs.spring.io/spring-boot/docs/1.5.12.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications


1. auto-configuration

Spring Boot provides auto-configuration for Spring MVC that works well with most applications.

Spring Boot自動配置好了SpringMVC

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

他的自動配置包含了以下Spring的初始特色

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

ContentNegotiatinViewResolver:用來組合所有視圖解析器的

他自動配置了ViewResolver(視圖解析器:根據方法來取得試圖對象(View), 由視圖對象決定可轉載或著重定向)

制定方式: 

在main application下面定義回傳ViewResolver的方法

回傳一個私有的繼承ViewResolver的類別

@Bean
public ViewResolver myViewResolver(){
return new MyViewResolver();
}

private class MyViewResolver implements ViewResolver {
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
return null;
}
}

我們如何知道這個會work呢?

主要是使用debug模式觀察DispatcherServlet中的doDispatch方法(用control+n找)

將中斷點下在這裡

當我們啟動之後, 訪問一個頁面, 我們就會看到

在this中他有multipartResolver文件上傳的, localeResolver國際化功能的Resolver

而我們要看的是viewResolvers中有我們的MyViewResolver的確進來了

也就是只要通過添加組件(Bean)就能夠使用





  • Support for serving static resources, including support for WebJars (see below).靜態資源文件夾路徑 webjars

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

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

  • Automatic registration of ConverterGenericConverterFormatter beans.

    • 自動註冊ConverterGenericConverterFormatter
    • Converter:轉換器;public String hello(User user);類別轉換使用
    • Formatter:格式化器;2017-12-17===Date;
  • 自己添加的格式化器转换器,我们只需要放在容器中即可;

  • Support for HttpMessageConverters (see below).

    • HttpMessageConverters:SpringMVC用來轉換Http請求和響應的;User---json;

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

      ​ 自己给容器中添加HttpMessageConverter,只需要將自己的组件注册到容器中(@Bean,@Component)

  • Automatic registration of MessageCodesResolver (see below).

    • 定義錯誤代碼產生規則
  • Automatic use of a ConfigurableWebBindingInitializer bean (see below).

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

---------------------------

2. 擴展SpringMVC

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 RequestMappingHandlerMappingRequestMappingHandlerAdapter 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.

-------------------------

這段的意思就是~如果有些Spring MVC的功能不在初始設定中

但是你想要使用的話, 你可以新增一個類別符合他的設定, 他就會掃得到裡面的設定

那要注意的是, 如果加上@EnableWebMvc的話

對於SpringMVC的配置, 你就要自己全部重配了

連初始會去掃靜態頁面的功能, 也會需要自己手動建立導頁

------------------------

比方說以下我們想使用Spring轉頁功能, 將hello頁面轉到success頁面

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

以上在SpringMVC中的設置

我們在SpringBoot中可以用以下方式完成

先建立config.MymvcConfig類別, 去繼承WebMvcConfigurerAdapter類別

其中的addViewController方法就能完成我們要做的事(可用control+o查看所有方法)

 

//使用WebMvcConfigurerAdapter 可以來擴展SpringMVC的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
//control+O 可以找到父類所有可用方法

@Override
public void addViewControllers(ViewControllerRegistry registry) {
//super.addViewControllers(registry);
//瀏覽器發送/atguigu請求來到success
registry.addViewController("/atguigu").setViewName("success");
}
}

-----------------------------------

原理:

​ 1)、WebMvcAutoConfiguration是SpringMVC的自動配置類

​ 2)、在做其他配置時會導入:@Import(EnableWebMvcConfiguration.class)

	@Configuration
	public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
		private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

	// 從容器中獲取所有WebMvcConfigurer
	@Autowired(required = false)
	public void setConfigurers(List<WebMvcConfigurer> configurers) {
		if (!CollectionUtils.isEmpty(configurers)) {
			this.configurers.addWebMvcConfigurers(configurers);
			// 一個參考實現:將所有的WebMvcConfigurer相關配置都來一起調用;
              //  @Override
              //  public void addViewControllers(ViewControllerRegistry registry) {
              //      for (WebMvcConfigurer delegate : this.delegates) {
              //          delegate.addViewControllers(registry);
              //      }
              /  }
		}
	}

​ 3)、容器中所有的WebMvcConfigurer都會一起起作用;

​ 4)、我們的配置類也會被調用;

​ 效果:SpringMVC的自動配置和我們的擴展配置都會起作用。


參考 https://github.com/sheep-cloud/Spring-Boot

沒有留言:

張貼留言

[leetcode] [KMP] KMP

ABCDABD... ABCDABF... 簡單的說, 傳統解兩字串匹配部分 可能會來個雙迴圈, 哀個比對, 當不匹配的時候, 會將下方列再後移1位 然後不匹配再後移 然而 如果像上放已經有4個屬於匹配的字串, 她就應該直接往後移四位來匹配, 而不是只移動1位 隱藏的思維是, 當...