SpringBoot-自动配置SpringMVC的原理解析(八)

参考SpringBoot的官方文档:https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications对我们自动化配置SpringMVC做出了详细的描述.

1. Spring MVC auto-configuration

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. 静态首页访问

Support for HttpMessageConverters (see below).

HttpMessageConverter:SpringMVC用来转换Http请求和响应的;User---Json;

HttpMessageConverter:是从容器中确定;获取所有的HttpMessageConverter;

自己给容器中添加HttpMessageConverter,只需要将自己的组件注册容器中(@Bean,@Component)
Automatic registration of MessageCodesResolver (see below).

定义错误代码生成规则
Automatic use of a ConfigurableWebBindingInitializer bean (see below).

我们可以配置一个ConfigurableWebBindingInitializer来替换默认的;(添加到容器)

org.springframework.boot.autoconfigure.web

web的所有自动场景

这是SpringBoot给我们的官网提供的自动配置.

2、扩展SpringMVC

我们既想使用SpringBoot带给我们的自动配置SpringMVC,又想在他的功能上进行扩展,该怎么做呢?

使用WebMvcConfigurationSupport可以来扩展SpringMVC的功能

编写一个配置类(@Configuration),是WebMvcConfigurationSupport

类型;不能标注@EnableWebMvc,既保留了所有的自动配置,也能用我们扩展的配置.

@Configuration
public class MvcCongif extends WebMvcConfigurationSupport {

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

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }
}

WebMvcConfigurationAdapter 在spring boot 2.0被废弃了,使用 WebMvcConfigurationSupport 而静态文件不显示CSS样式的,这是因为替换之后之前的静态资源文件 会被拦截,导致无法可用。

解决方法::重写 addResourceHandlers()方法,加入静态文件路径即可
代码如下:

 @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }

但是呢:如果继承WebMvcConfigurationSupport 这个类就会导WebMvcAutoConfiguration失效具体可以看这个类上面的条件,会全盘接管web,如果不想全盘接管,可以实现WebMvcConfigurer这个接口,这样原本的默认配置也有,你加的扩展配置也成立.

@Configuration
public class MvcCongif implements WebMvcConfigurer {

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

spring 5.0后要使用Java8,而在Java8中接口是可以有default方法的,所以我们只需要在自定义配置类中直接实现 WebMvcConfigurer这个方法就好了.

3、全面接管SpringMVC

配置类中添加@EnableWebMvc注解SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了.

不推荐使用这种方法!

原理:

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 })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
        ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
//容器中没有这个组件的时候,这个自动配置类才生效

4.@EnableWebMvc将WebMvcConfigurationSupport组件导入进来;导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能.

4.如何修改SpringBoot的默认配置

模式:

1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来.

2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置.

3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置.

猜你喜欢

转载自www.cnblogs.com/xiaoqiqistudy/p/11349208.html