SpringMVC 自动配置原理|学习笔记

简介: 快速学习 SpringMVC 自动配置原理

开发者学堂课程【SpringBoot快速掌握 - 核心技术 SpringMVC 自动配置原理】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址https://developer.aliyun.com/learning/course/612/detail/9244


SpringMVC 自动配置原理


内容介绍: 

一、 SpringMVC 默认配置

二、 如何修改  SpringMVC  的默认配置

 

一、 SpringMVC 默认配置

一、 SpringMVC 默认配置

如果要使用Spring MVC 就直接使用,因为Spring Boot自动配置好了Spring MVC

以下是SpringBoot对SpringMVC的默认配置:

l Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

自动配置了 ViewResolver  (视图解析器∶根据方法的返回值得到视图对象(View ),视图对象决定如何渲染(转发?重定向?))

ContentNegotiatingViewResolver :组合所有的视图解析器的;

如何定制:我们可以自己给容器中添加一个视图解析器;自动的将其组合进来;

测试:

主类:

@SpringBootApplication

public class SpringBoot04webRestfulcrudApplication{

public static void main(String[] args){

SpringApplication.run(SpringBootO4webRestfulcrudApplication.class,args);

@Bean

public ViewResolver myViewReolver(){

return new MyViewResolver();

private static class MyViewResolver implements ViewResolver{

@Override

public View resolveViewName(String viewName,Locale locale) throws Exception {

return null;

}

}

}

然后Ctrl+N 搜索DispatcherServlet进行查看doDispatch方法,打一个断点,进行debug,在网页进行请求localhost:8080/success

找到视图解析器,viewResolvers进行查看,可以看出已经自动导入了

l Support for serving static resources, including support for Weblars (see below).静态资源文件夹路径,webjars

l Static index.html support. 静态首页访问

l Custom| Favicon support (see below). favicon.ico

l 自动注册了of converter , GenericConverter , Formatter beans.

² Converter :转换器;假设public String hello(User user):类型转换使用Converter

² Formatter 格式化器;2017.12.17===Date ;

格式化注册条件:

@Bean

@ConditionalOnProperty(prefix = "spring. mvc”, name = "date-format"")//在文件中配置日期格式化的规则

public Formatter<Date> dateFormatter({

配了之后就会自动添加

return new DateFormatter(this.mvcProperties.getDateFormat());

//日期格式化组件

自己添加的格式化器转换器,我们只需要放在容器中即可

@Override

public void addFormatters(FormatterRegistry registry){

for (Converter<?, ?>converter :getBeansofType(Converter.c1ass)){

registry. addConverter(converter);

}

for (GenericConverter converter : getBeans0fType(GenericConverter.class)){

registry.addConverter(converter);

for (Formatter<?>formatter : getBeans0fType(Formatter.class)){

registry.addFormatter(formatter);

}

}

l Support for HttpMe ssageConverters(see belaw).

² HttpMessageConverter : SpringMVC 用来转换 Http 请求和响应的; User---Json 就需要用到 HttpMessageConverter

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

自己给容器中添加 HttpMessageConverter ,只需要将自己的组件注册容器中(@Bean, @Component )

HttpMessageConverter 用法:

import org. springframework . boot. autoconf igure . web. HttpMess ageConverters;

import org. springfr amework .context. annotation.*;

import org. springfr amework .http. converter.*;

@Configuration

public class MyConfiguration {

@Bean

public HttpMessageConverters cus tomC onverters() {

HttpMessageConverter<?> additional =

HttpMessageConverter<?) another = ...

return new HttpMessageConverters (additional, another);

}

}

l Automatic registration of HessageCodesResolver (see below)

l 定义错误代码生成规则

源码:

public enum Format imp1ements MessageCodeFormatter {

/**

* Prefix the error code at the beginning of the generated message code. e.g.;

{@code errorCode + "." + object name + "." + field}

*/

PREFIX_ ERROR CODE {

@Override

public String format(String errorCode, String objectName, String field) {

return toDel imi tedS tring (errorCode, objectName, field);

}

},

/**

*Postfix the error code at the end of the generated message code. e.g.:

* {@code object name + "." + field + "." + errorCode }

*/

POSTFIX_ ERROR_ CODE {

@Override

public String format(String errorCode, String objectName, String field) {

return toDel imitedString(objectName, field, errorCode);

}

};

l Automatic use of a ConfigurablewebBindingInitializer bean (see below).

源码:

@Override

protected ConfigurableWebBindingInitializer getConf igurableWebBindingInitializer() {

try {

return this . beanFactory . getBean(Configurabl eWebBindingInitializer.c1ass);

catch (NoSuchBeanDefinitionException ex) {

return super. getConfigurableWebBindingInitializer();

}

}

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

1初始化webDataBinder;

2请求数据-=-==JavaBean;

org.springframework.boot.autoconfigure.web : web的所有自动场景;

lf 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 typewebwvcConfigurerAdapter , but without @Enablewebve . lf you wish to provide custom instances of

RequestNappingHandlerNapping , RequestNappingHandlerAdapter or ExceptionHandlerExceptionResolver youcan declare a webwvcRegistrationsAdapter instance providing such components.

lf you want to take complete control of Spring MVC, you can add your own @configuration annotated with@EnablewebMvc .

 

二、如何修改 SpringBoot 的默认配置

模式;

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

因为 spring boot 在底层大量用到了这个细节

相关文章
|
自然语言处理 API iOS开发
IDEA 优质 插件 Translation 跟随 IDEA 2022.3 版 更新翻译引擎啦
IDEA 优质 插件 Translation 跟随 IDEA 2022.3 版 更新翻译引擎啦
3600 1
|
3月前
|
存储 安全 Java
2025 年一线互联网大厂最新高质量 Java 面试八股文整理带答案及实操要点
本文整理了一线互联网大厂最新的高质量Java面试八股文及答案,涵盖Java基础、集合、多线程等多个核心方面,帮助你高效备考。内容包括面向对象与面向过程的区别、`equals`与`==`的对比、`final`和`static`的用法,以及ArrayList与LinkedList的区别、HashMap的工作原理等。同时,深入探讨了多线程创建方式、生命周期、上下文切换及死锁问题,并附有实操代码示例。资源链接:[点此下载](https://pan.quark.cn/s/14fcf913bae6)。
864 1
|
移动开发 前端开发 JavaScript
Python 3+Django 3 结合Vue.js框架构建前后端分离Web开发平台实战
Python 3+Django 3 结合Vue.js框架构建前后端分离Web开发平台实战
21729 3
Python 3+Django 3 结合Vue.js框架构建前后端分离Web开发平台实战
|
存储 缓存 NoSQL
使用 Spring Cache 实现缓存,这种方式才叫优雅!
使用 Spring Cache 实现缓存,这种方式才叫优雅!
|
前端开发 Java 开发者
springmvc hello | 学习笔记
快速学习 springmvc hello。
126 0
springmvc hello | 学习笔记
|
1天前
|
人工智能 运维 安全
|
4天前
|
SpringCloudAlibaba 负载均衡 Dubbo
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
本文对比分析了SpringCloudAlibaba框架下Feign与Dubbo的服务调用性能及差异。Feign基于HTTP协议,使用简单,适合轻量级微服务架构;Dubbo采用RPC通信,性能更优,支持丰富的服务治理功能。通过实际测试,Dubbo在调用性能、负载均衡和服务发现方面表现更出色。两者各有适用场景,可根据项目需求灵活选择。
374 124
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
|
6天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
600 107

热门文章

最新文章