SpringBoot學習_SpringMVC自動配置原理
官方文件中對SpringMVC自動配置的說明
翻譯:
以下是SpringBoot對
SpringMVC的預設配置,都在(
WebMvcAutoConfiguration
)這個類中:
Inclusion of ContentNegotiatingViewResolver
and BeanNameViewResolver
beans.
自動配置了ViewResolver
(檢視解析器:根據方法的返回值得到檢視物件(View),檢視物件決定如何渲染(決定是轉發?還是重定向?))
ContentNegotiatingViewResolver
:組合所有的檢視解析器的;
如何自定義檢視解析器:我們可以自己給容器中新增一個檢視解析器;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
:轉換器;型別轉換使用Converter
例子:
頁面提交了一個18,這個18是個string型別的了,我們要把它轉為int型別的,就要用到Converter了
Formatter
Support for HttpMessageConverters (see below).
HttpMessageConverter
:這個類是SpringMVC用來轉換Http請求和響應的;
例子:
比如我們有一個方法返回了User型別物件,現在想以json的方式寫出,這裡就需要用到HttpMessageConverter了,
HttpMessageConverters 是從容器中確定的;獲取所有的HttpMessageConverter; 如果想自定義給容器中新增HttpMessageConverter,只需要將自己的元件註冊容器中就可以了(怎麼註冊:用@Bean,@Component)
Automatic registration of MessageCodesResolver (see below)
:定義錯誤程式碼生成規則
Automatic use of a ConfigurableWebBindingInitializer bean (see below). 我們可以配置一個ConfigurableWebBindingInitializer來替換預設的;(新增到容器) ConfigurableWebBindingInitializer的作用: 初始化WebDataBinder(web資料繫結器); 把請求資料繫結到JavaBean;
如何修改SpringBoot的預設配置
1)、SpringBoot在自動配置很多元件的時候,都是先看容器中有沒有使用者自己配置的(@Bean、@Component)如果有就用使用者配置的,如果沒有,才自動配置;如果有些元件可以有多個(ViewResolver:檢視解析器)會將使用者配置的和自己預設的組合起來;
例子:
2)、在SpringBoot中會有非常多的xxxConfigurert幫助我們進行擴充套件配置
3)、在SpringBoot中會有很多的xxxCustomizer幫助我們進行定製配置
擴充套件SpringMVC
編寫一個配置類(@Configuration),這個類是WebMvcConfigurerAdapter(抽象類,繼承它就可以了)型別的;不能標註@EnableWebMvc; 例子:
//使用WebMvcConfigurerAdapter可以來擴充套件SpringMVC的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@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();
//EnableWebMvcConfiguration 類的父類
//@Autowired自動裝配,這個註解使方法從容器中獲取了所有的WebMvcConfigurer,把所有的addViewControllers都呼叫了一遍,當然也把我們自己擴充套件的addViewControllers也順道呼叫了
@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的自動配置和我們的擴充套件配置都會起作用;
全面接管SpringMVC
如果在配置類中新增@EnableWebMvc註解後,SpringBoot對SpringMVC的自動配置就會失效,所有的都要自己配置了,所有的SpringMVC的自動配置都失效了
為什麼@EnableWebMvc自動配置就失效了?
@EnableWebMvc註解中有匯入DelegatingWebMvcConfiguration的註解
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
DelegatingWebMvcConfiguration這個類可以看到它繼承於WebMvcConfigurationSupport ,
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
SpringMVC的自動配置類WebMvcAutoConfiguration的頭上有個
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
註解,當容器中沒有WebMvcConfigurationSupport這個類時,這個自動配置類才會生效,所以自動配置類會失效
@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 {
4)、@EnableWebMvc將WebMvcConfigurationSupport元件匯入進來; 5)、匯入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;