文山做女主播的在哪个网站,上海市住房和城乡建设厅,校园网站设计方案,wordpress的40411.1 spring-boot为MVC提供的自动配置 1.ContentNegotiatingViewResolver视图解析器#xff1b; 2.静态资源或者支持WebJars#xff1b; 3.自动注册类型转换器#xff1a;比如说前台提交user的字段#xff0c;后台自动封装的意思#xff1b; 4.HttpMessageConverters…11.1 spring-boot为MVC提供的自动配置 1.ContentNegotiatingViewResolver视图解析器 2.静态资源或者支持WebJars 3.自动注册类型转换器比如说前台提交user的字段后台自动封装的意思 4.HttpMessageConverters转换http的请求和相应比如把一个user字符串转为一个json字符串 5.MessageCodesResolver定义错误消息 6.index.html 支持首页映射 7.Favicon支持图标映射; 8.ConfigurableWebBindingInitializer支持数据web的初始化绑定把请求和数据封装到javaBean中
11.2 扩展配置原理理解 先创建一个包叫做config自定义的配置都放在里面 然后再创建一个类叫MyMvcConfig的配置类加上Configuration注解用这个类来全面扩展springMVC的配置这个类必须实现WebMvcConfigurer接口实现里面的方法即可 首先要去实现视图解析器搜索ContentNegotiatingViewResolver这个类发现这个类实现了ViewResolver接口因此这个类就是一个视图解析器
public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport implements ViewResolver, Ordered, InitializingBean 自己在内部写一个静态内部类
package jiang.com.springbootstudy.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;
Configuration
public class MyMvcConfig implements WebMvcConfigurer {// 放接口这样写把bean放到接口里Beanpublic ViewResolver myViewResolver(){return new MyViewResolver();}// 自定义了一个试图解析器public static class MyViewResolver implements ViewResolver{Overridepublic View resolveViewName(String s, Locale locale) throws Exception {return null;}}
}所有请求包括视图解析器会经过DispatcherServlet里面有个方法是doDispatcher所有的请求和相应都会经过这因此打个断点。 点击DeBug刷新页面。然后会自动跳到断点这控制台内容如下 打开this 找到viewResolver点开发现刚刚创建的视图解析器已经被加载进来了并且经过了doDispatcher方法。 12.3 总结 在springboot中有非常多的xxxx Configuration帮助我们进行扩展配置只要看见了这个东西我们就要注意了!