开发者学堂课程【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 nu
ll
;
}
}
}
然后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 在底层大量用到了这个细节