springboot-mvc+扩展SpringMVC+@EnableWebMvc+修改自动化配置

1. Spring MVC auto-configuration

https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration

27.1.1 Spring MVC auto-configuration

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.
Support for serving static resources, including support for WebJars (see below). 静态资源文件夹 WebJars
Automatic registration of Converter, GenericConverter, Formatter beans.
Support for HttpMessageConverters (see below).   
Automatic registration of MessageCodesResolver (see below).
Static index.html support.  静态首页访问
Custom Favicon support (see below).
Automatic use of a ConfigurableWebBindingInitializer bean (see below).
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.

解析:

 

Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans

  • 自动配置了视图解析器,根据方法的返回值得到视图对,视图对象决定如何渲染
  • ContentNegotiatingViewResolver:组合所有的视图解析器的;
  • 如何定制:我们可以自己给容器中添加一个视图解析器,自动的将其组合进来
加载所有视图,并获取最优的那个.
@Bean
@ConditionalOnBean(ViewResolver.class)
@ConditionalOnMissingBean(name = "viewResolver", value = ContentNegotiatingViewResolver.class)
public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
   ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
   resolver.setContentNegotiationManager(
         beanFactory.getBean(ContentNegotiationManager.class));
   // ContentNegotiatingViewResolver uses all the other view resolvers to locate
   // a view so it should have a high precedence
   resolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
   return resolver;
}
初始化的时候加载了所有视图解析器
@Override
protected void initServletContext(ServletContext servletContext) {
   Collection matchingBeans =
         BeanFactoryUtils.beansOfTypeIncludingAncestors(obtainApplicationContext(), ViewResolver.class).values();
   if (this.viewResolvers == null) {
      this.viewResolvers = new ArrayList<>(matchingBeans.size());
      for (ViewResolver viewResolver : matchingBeans) {
         if (this != viewResolver) {
            this.viewResolvers.add(viewResolver);
         }
      }
   }

  定制一个视图解析器

 

@SpringBootApplication
public class WebApplication {

   public static void main(String[] args) {
      SpringApplication.run(WebApplication.class, args);
   }




   //定制一个视图解析器
   @Bean
   public ViewResolver myViewReolver(){
      return new MyViewResolver();
   }

   public static class MyViewResolver implements ViewResolver{

      @Override
      public View resolveViewName(String viewName, Locale locale) throws Exception {
         return null;
      }
   }
}

验证

springboot-mvc+扩展SpringMVC+@EnableWebMvc+修改自动化配置_第1张图片

 

Automatic registration of Converter, GenericConverter, Formatter beans

转换器、通用转换器、格式化bean的自动注册。

Converter:将浏览器数据封装到controller对应的方法参数中,比如:public void (User user),这里使用了反射。

Formatter 格式化器;比如日期格式化

 

Support for HttpMessageConverters (see below).   可以自己定义。

HttpMessageConverters :SpringMVC用来转换Http请求和响应的,比如响应时候将User转换为Json;

他从容器中加载来,是从容器中确定。所以可以自己定义。

public class HttpMessageConverters implements Iterable> {

  ........
构造器添加对象
   public HttpMessageConverters(HttpMessageConverter... additionalConverters) {
      this(Arrays.asList(additionalConverters));
   }

 

Automatic use of a ConfigurableWebBindingInitializer bean (see below).

自动配置web绑定的初始化

 

2扩展SpringMVC(WebMvcConfigurerAdapter)

01实现视图解析器和拦截器的功能



    
        
        
    


 

做法

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//@EnableWebMvc   不要接管SpringMVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {



    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //浏览器发送 /success2 请求来到 success
        registry.addViewController("/success2").setViewName("success");
    }

}

3@EnableWebMvc

让所有的SpringMVC的自动配置都失效了,使用用户自己的配置

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能,
 @EnableWebMvc 去掉mvc的自动配置
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {



    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //浏览器发送 /success2 请求来到 success
        registry.addViewController("/success2").setViewName("success");
    }

}

 

 

 

4 修改自动化配置

  1. 先看容器中有没有用户自己配置的(@Bean、@Component)如
    1.     有:就用用户配置的    
    2.     没有: 自动配置;
    3.     组件有多个(如ViewResolver):将用户配置的和容器默认的组合起来;
  2. 在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置
  3. 在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置

 

 

 

 

 

 

 

 

你可能感兴趣的:(SpringBoot)