05. spring-boot对spring-mvc的自动配置

spring-mvc的自动配置

viewResolver:

视图解析器:根据方法的返回值得到视图对象(View)。

//会将容器中已有的ViewResolver和自己定义的ViewResolver全部add进去
ContentNegotiatingViewResolver

自定义视图解析器:

//需要实现ViewResolver,并注入进容器
@Configuration
public class ViewConfig {
    @Bean
    public ViewResolver myViewResolver() {
        return new MyViewResolver();
    }
}

public class MyViewResolver implements ViewResolver {
    @Override
    public View resolveViewName(String s, Locale locale) throws Exception {
        return null;
    }
}

Converter:

转换器:前端数据和后端类的关系映射(数据类型转换)

//前端传的数据自动封装成User类
public String hello(User user)

Formatter:

格式化器:将前端传入的时间类型转化为Date类型(2017.12.17===Date)

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

//日期格式化组件
public Formatter dateFormatter() {
    return new DateFormatter(this.mvcProperties.getDateFormat());
}
修改spring boot默认配置
  1. boot在自动配置很多组件的时候,很多都会看用户中有没有自己配置,如果有,就用用户配置的,如果没有才自动配置。如果有些组件可以有多个,则可以将用户配置的和自己默认的组合起来。
  2. 在boot中会有很多的xxxConfigurer来帮助我们做扩展配置。

你可能感兴趣的:(05. spring-boot对spring-mvc的自动配置)