4、SpringMVC自動(dòng)配置
1. Spring MVC auto-configuration
Spring Boot 自動(dòng)配置好了SpringMVC
以下是SpringBoot對(duì)SpringMVC的默認(rèn)配置:==(WebMvcAutoConfiguration)==
-
Inclusion of
ContentNegotiatingViewResolverandBeanNameViewResolverbeans.- 自動(dòng)配置了ViewResolver(視圖解析器:根據(jù)方法的返回值得到視圖對(duì)象(View),視圖對(duì)象決定如何渲染(轉(zhuǎn)發(fā)?重定向?))
- ContentNegotiatingViewResolver:組合所有的視圖解析器的;
- ==如何定制:我們可以自己給容器中添加一個(gè)視圖解析器;自動(dòng)的將其組合進(jìn)來;==
Support for serving static resources, including support for WebJars (see below).靜態(tài)資源文件夾路徑,webjars
Static
index.htmlsupport. 靜態(tài)首頁(yè)訪問-
Custom
Faviconsupport (see below). favicon.ico?
-
自動(dòng)注冊(cè)了 of
Converter,GenericConverter,Formatterbeans.- Converter:轉(zhuǎn)換器; public String hello(User user):類型轉(zhuǎn)換使用Converter
-
Formatter格式化器; 2017.12.17===Date;
@Bean
@ConditionalOnProperty(prefix = "spring.mvc", name = "date-format")//在文件中配置日期格式化的規(guī)則
public Formatter<Date> dateFormatter() {
return new DateFormatter(this.mvcProperties.getDateFormat());//日期格式化組件
}
? ==自己添加的格式化器轉(zhuǎn)換器,我們只需要放在容器中即可==
-
Support for
HttpMessageConverters(see below).HttpMessageConverter:SpringMVC用來轉(zhuǎn)換Http請(qǐng)求和響應(yīng)的;User---Json;
-
HttpMessageConverters是從容器中確定;獲取所有的HttpMessageConverter;==自己給容器中添加HttpMessageConverter,只需要將自己的組件注冊(cè)容器中(@Bean,@Component)==
?
Automatic registration of
MessageCodesResolver(see below).定義錯(cuò)誤代碼生成規(guī)則-
Automatic use of a
ConfigurableWebBindingInitializerbean (see below).==我們可以配置一個(gè)ConfigurableWebBindingInitializer來替換默認(rèn)的;(添加到容器)==
初始化WebDataBinder; 請(qǐng)求數(shù)據(jù)=====JavaBean;
org.springframework.boot.autoconfigure.web:web的所有自動(dòng)場(chǎng)景;
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、擴(kuò)展SpringMVC
<mvc:view-controller path="/hello" view-name="success"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean></bean>
</mvc:interceptor>
</mvc:interceptors>
==編寫一個(gè)配置類(@Configuration),是WebMvcConfigurerAdapter類型;不能標(biāo)注@EnableWebMvc==;
既保留了所有的自動(dòng)配置,也能用我們擴(kuò)展的配置;
//使用WebMvcConfigurerAdapter可以來擴(kuò)展SpringMVC的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
//瀏覽器發(fā)送 /atguigu 請(qǐng)求來到 success
registry.addViewController("/atguigu").setViewName("success");
}
}
原理:
? 1)、WebMvcAutoConfiguration是SpringMVC的自動(dòng)配置類
? 2)、在做其他自動(dòng)配置時(shí)會(huì)導(dǎo)入;@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);
//一個(gè)參考實(shí)現(xiàn);將所有的WebMvcConfigurer相關(guān)配置都來一起調(diào)用;
@Override
// public void addViewControllers(ViewControllerRegistry registry) {
// for (WebMvcConfigurer delegate : this.delegates) {
// delegate.addViewControllers(registry);
// }
}
}
}
? 3)、容器中所有的WebMvcConfigurer都會(huì)一起起作用;
? 4)、我們的配置類也會(huì)被調(diào)用;
? 效果:SpringMVC的自動(dòng)配置和我們的擴(kuò)展配置都會(huì)起作用;
3、全面接管SpringMVC;
SpringBoot對(duì)SpringMVC的自動(dòng)配置不需要了,所有都是我們自己配置;所有的SpringMVC的自動(dòng)配置都失效了
我們需要在配置類中添加@EnableWebMvc即可;
//使用WebMvcConfigurerAdapter可以來擴(kuò)展SpringMVC的功能
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
//瀏覽器發(fā)送 /atguigu 請(qǐng)求來到 success
registry.addViewController("/atguigu").setViewName("success");
}
}
原理:
為什么@EnableWebMvc自動(dòng)配置就失效了;
1)@EnableWebMvc的核心
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
2)、
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
3)、
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
WebMvcConfigurerAdapter.class })
//容器中沒有這個(gè)組件的時(shí)候,這個(gè)自動(dòng)配置類才生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
4)、@EnableWebMvc將WebMvcConfigurationSupport組件導(dǎo)入進(jìn)來;
5)、導(dǎo)入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;
5、如何修改SpringBoot的默認(rèn)配置
模式:
? 1)、SpringBoot在自動(dòng)配置很多組件的時(shí)候,先看容器中有沒有用戶自己配置的(@Bean、@Component)如果有就用用戶配置的,如果沒有,才自動(dòng)配置;如果有些組件可以有多個(gè)(ViewResolver)將用戶配置的和自己默認(rèn)的組合起來;
? 2)、在SpringBoot中會(huì)有非常多的xxxConfigurer幫助我們進(jìn)行擴(kuò)展配置
? 3)、在SpringBoot中會(huì)有很多的xxxCustomizer幫助我們進(jìn)行定制配置