springMVC自动配置

Spring_Boot专栏
上一篇主目录 下一篇

【前言】
https://docs.spring.io/spring-boot/docs/版本号/reference/htmlsingle/

springboot为我们自动配置了springMVC的某些配置,比如ViewResolver视图解析器、静态资源访问路径、Converter转换器和Formatter格式化器等,我们可以使用默认的配置,也可以自定义这些配置。


1 Spring MVC自动配置

Spring Boot 自动配置好了SpringMVC
以下是SpringBoot对SpringMVC的默认配置:(WebMvcAutoConfiguration)

双击shift,查找WebMvcAutoConfiguration文件,按Ctrl+F按关键字查找,输入ViewResolver、Converter等关键字就可以查找相关配置信息。

1.1 ViewResolver视图解析器

ContentNegotiatingViewResolver (内容协商视图解析器)、 BeanNameViewResolver (组件名称视图解析器)、InternalResourceViewResolver(内部资源视图解析器) beans.

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

内容协商视图解析器ContentNegotiatingViewResolver:组合所有的视图解析器

        @Bean
        @ConditionalOnBean({ViewResolver.class})
        @ConditionalOnMissingBean(
            name = {"viewResolver"},
            value = {ContentNegotiatingViewResolver.class}
        )
        public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
            ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
            resolver.setContentNegotiationManager((ContentNegotiationManager)beanFactory.getBean(ContentNegotiationManager.class));
            resolver.setOrder(-2147483648);
            return resolver;
        }
  • @Bean:这会告诉spring通过调用该方法并获取返回的实例来创建一个bean,这里返回的是ContentNegotiatingViewResolver.class的实例resolver,用resolver创建创建一个bean并注入到容器中。更多

  • @ConditionalOnBean({ViewResolver.class}):springBean resolver(ContentNegotiatingViewResolver)要依赖于springBean ViewResolver.class的存在而存在

  • @ConditionalOnMissingBean(
    name = {“viewResolver”},
    value = {ContentNegotiatingViewResolver.class}
    )
    value 是条件的类的Class对象数组,name 是spring容器中bean的名字。当value的值中的Class对象数组缺失时,使用容器中名为name的bean。本例中当ContentNegotiatingViewResolver.class即返回值resolver缺失时,使用viewResolver这个组件。

Ctrl+b/Ctrl+鼠标左键 点击ContentNegotiatingViewResolver:

    @Nullable
    public View resolveViewName(String viewName, Locale locale) throws Exception {
        RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
        Assert.state(attrs instanceof ServletRequestAttributes, "No current ServletRequestAttributes");
        List<MediaType> requestedMediaTypes = this.getMediaTypes(((ServletRequestAttributes)attrs).getRequest());
        if (requestedMediaTypes != null) {
            List<View> candidateViews = this.getCandidateViews(viewName, locale, requestedMediaTypes);
            View bestView = this.getBestView(candidateViews, requestedMediaTypes, attrs);
            if (bestView != null) {
                return bestView;
            }
        }

获得candidateViews 候选的视图,然后在候选的视图中取得bestView 最适合的视图,并返回

1.1.2 自定义视图解析器

如何定制?
我们可以自己给容器中添加一个视图解析器,容器会自动的将其组合进来
添加:
在这里插入图片描述
测试:
在DispatcherServlet的doDispatch()方法打断点debug
在这里插入图片描述

1.2 静态资源访问路径

springboot还自动配置了静态资源的访问映射路径,包括:webjarsindex.htmlFavicon

1.3 Converter转换器和Formatter格式化器

springboot自动注册了 of Converter, GenericConverter, Formatter beans.

  • Converter:转换器,如 public String hello(User user){},springboot将使用Converter来把/hello?name=shane&age=23&sex=male&…中的数据进行类型转换后和User对象一一绑定。

  • HttpMessageConverter:SpringMVC用来转换Http请求和响应的,如User—>Json。从容器中获取所有的HttpMessageConverter。自己给容器中添加HttpMessageConverter,只需要将自己的组件注册容器中(@Bean,@Component)
    在这里插入图片描述

  • Formatter 格式化器, 按照 一定格式(2020/05/20)将字符串转换成Date类型的数据。可在配置文件application.properties中配置日期格式属性spring.mvc.date-format=yyyy-MM-dd

		@Bean
		@ConditionalOnProperty(prefix = "spring.mvc", name = "date-format")//在文件中配置日期格式化的规则
		public Formatter<Date> dateFormatter() {
			return new DateFormatter(this.mvcProperties.getDateFormat());//日期格式化组件
		}

【注】
@ConditionalOnProperty(prefix = “spring.mvc”, name = “date-format”):这个注解配合@Bean,当配置文件中有前缀为spring.mvc并且名称为date-format的配置项时,将当前方法的返回值作为组件注入到容器中。

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

1.4 其他

MessageCodesResolver:定义错误代码生成规则

ConfigurableWebBindingInitializer: 初始化WebDataBinder(请求数据封装成JavaBean对象)。可以配置一个ConfigurableWebBindingInitializer添加到容器来替换默认的。

2 修改SpringBoot的默认配置

模式:

​ 1)SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)。如果有就用用户配置的,如果没有才自动配置(@ConditionalOnMissingBean);如果有些组件可以有多个(ViewResolver),会将用户配置的和springboot自己默认的组合起来
​ 2)在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置
​ 3)在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值