SpringBoot扩展SpringMVC自动配置

SpringBoot中自动配置了

  •   ViewResolver(视图解析器)
  •   ContentNegotiatingViewResolver(组合所有的视图解析器)
  •   自动配置了静态资源文件夹、静态首页、favicon.ico及Webjars
  •   Converter(转换器,转换类型使用)
  •        Formatter(格式化器)
  •        HttpMessageConverter(对SpringMVC的请求和响应进行序列化)
  •        MessageCodesResolver(定义错误代码生成规则)
  •        ConfigurableWebBindingInitializer(Web数据绑定器)

我们可以自定义SpringMVC的配置

 1 package cn.coreqi.config;
 2 
 3 import org.springframework.context.annotation.Configuration;
 4 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 5 import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
 6 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 8 
 9 /**
10  * 扩展SpringMVC
11  * SpringBoot2使用的Spring5,因此将WebMvcConfigurerAdapter改为WebMvcConfigurer
12  * 使用WebMvcConfigurer扩展SpringMVC好处既保留了SpringBoot的自动配置,又能用到我们自己的配置
13  */
14 //@EnableWebMvc //如果我们需要全面接管SpringBoot中的SpringMVC配置则开启此注解,
15                 //开启后,SpringMVC的自动配置将会失效。
16 @Configuration
17 public class WebConfig implements WebMvcConfigurer {
18     @Override
19     public void addViewControllers(ViewControllerRegistry registry) {
20         //设置对“/”的请求映射到index
21         //如果没有数据返回到页面,没有必要用控制器方法对请求进行映射
22         registry.addViewController("/").setViewName("index");
23     }
24 }

猜你喜欢

转载自www.cnblogs.com/fanqisoft/p/10324465.html