您的位置:首页 > 编程语言 > Java开发

SpringBoot学习_SpringMVC自动配置原理

2018-11-03 20:48 906 查看

官方文档中对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
格式化器;
例子:
把2017.12.17格式化为Date;
自己添加的格式化器转换器,我们只需要放在容器中即可

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最基本的功能;

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: