5.4 mvc配置原理
-
在进行测试前,我们还需要知道一个东西,就是SpringBoot对我们的SpringMVC还做了哪些配置,包括如何扩展,如何定制。只有把这些都搞清楚了,我们在之后使用才会更加得心应手。途径一:源码分析,途径二:官方文档
-
官方地址:https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/reference/html/boot-features-developing-web-applications.html
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.
//支持静态资源文件夹的路径,以及webjars
Support for serving static resources, including support for WebJars (covered later in this document)).
//自动注册了Converter:【转换器,这就是我们网页提交数据到后台自动封装成为对象的东西,比如把18字符串自动转换为int类型】
//Formatter:【格式化器,比如页面给我们了一个2022-7-31,它会给我们自动格式化为Date对象】
Automatic registration of Converter, GenericConverter, and Formatter beans.
//HttpMessageConverters:SpringMVC用来转换ttp请求和响应的的,比如我们要把一个User对象转换为JSON字符串,可以去看官网文档解释:
Support for HttpMessageConverters (covered later in this document).
//定义错误代码生成规则的
Automatic registration of MessageCodesResolver (covered later in this document).
//首页定制
Static index.html support.
//图标定制
Custom Favicon support (covered later in this document).
//初始化数据绑定
Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).
If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter instance to provide such components.
If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.
- 例1:自动配置了ViewResolver,就是我们之前学习的SpringMVC的视图解析器:即根据方法的返回值取得视图对象(View),然后由视图对象决定如何渲染(转发,重定向)。我们去看看这里的源码:我们找到WebMvcAutoConfiguration,然后搜索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;
}
- 说明如果没有配置自定义的ViewResolver,则将使用springboot默认的视图解析器。那么我们就可以在我们自己的MyMvcConfiguration配置类中注册自己的ViewResolver,通过这种方式来实现SpringBoot对SpringMVC的定制
package com.zk.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Locale;
/**
* @author CNCLUKZK
* @create 2022/7/31-20:19
*/
//扩展springmvc
//如果。你想diy一些定制化的功能,只要写这个组件,然后将它交给springboot,springboot就会帮我们自动装魔!
@Configuration
public class MyMvcConfiguration implements WebMvcConfigurer {
//注册一个自定义视图解析器到容器,然后springboot就可以加载
@Bean
public MyViewResolver getMyViewResolver(){
return new MyViewResolver();
}
//自定义视图解析器
public static class MyViewResolver implements ViewResolver{
@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
return null;
//参考ViewResolver实现类中此方法的规则
}
}
}
- 例2:自定义格式转换器:
@Bean
@Override
public FormattingConversionService mvcConversionService() {
Format format = this.mvcProperties.getFormat();
WebConversionService conversionService = new WebConversionService(new DateTimeFormatters()
.dateFormat(format.getDate()).timeFormat(format.getTime()).dateTimeFormat(format.getDateTime()));
addFormatters(conversionService);
return conversionService;
}
- mvcProperties对应的配置文件WebMvcProperties:application.yml配置时间格式化
mvc:
format:
date-time: yyyy-MM-dd HH:mm:ss:SSS
5.5 修改Spring Boot的默认配置
-
方式一:这么多的自动配置,原理都是一样的,通过这个WebMVC的自动配置原理分析,我们要学会种学习方式,通过源码探究,得出结论;这个结论一定是属于自己的,而且一通百通。SpringBoot的底层,大量用到了这些设计细节思想,所以,没事需要多阅读源码!得出结论;Spring Boot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(如果用户自己配置@bean),如果有就用用户配置的,如果没有就用自动配置的;如果有些组件可以存在多个,比如我们的视图解析器,就将用户配置的和自己默认的组合起来!
-
例子:
package com.zk.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Locale;
/**
* @author CNCLUKZK
* @create 2022/7/31-20:19
*/
//扩展springmvc
//如果。你想diy一些定制化的功能,只要写这个组件,然后将它交给springboot,springboot就会帮我们自动装魔!
//应为类型要求为WebMvcConfigurer,所以我们实现其接口
//可以使用自定义类扩展springmvc的功能
@Configuration
public class MyMvcConfiguration implements WebMvcConfigurer {
//视图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送特殊请求,就会跳转到特定页面
registry.addViewController("/zk").setViewName("test");
}
}
- 自定义的WebMvcConfigurer配置类为什么不能加@EnableWebMvc注解
@EnableWebMvc导入了一个类,@Import(DelegatingWebMvcConfiguration.class):作用从容器中获取所有webmvcconfig:但DelegatingWebMvcConfiguration会同时引入WebMvcConfigurationSupport而WebMvcAutoConfiguration自动配置的条件就是@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)所以配置了@EnableWebMvc注解导致WebMvc的自动配置失效了
- springboot中start如何开发,开发一个@Configuration加一个@EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class })配置类,供配置文件配置,将这两个类打jar包,放到spring-boot-autoconfigure这个jar包下的autoconfigure目录下
- 在springboot中,有非常多的自定义XxxConfiguration帮助我们进行扩展配置,只要看见了这个自定义类,我们就要注意了!
下一篇:SpringBoot-14-模块开发-环境和首页
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/123858.html