springboot之springmvc自动配置

Spring Boot 自动配置好了 Spring MVC

以下是springboot对springmvc的默认配置

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

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

ContentNegotiatingViewResolver :组合所有的视图解析器

如何定制?我们可以自己给容器中添加一个视图解析器,ContentNegotiatingViewResolver 会自动的将其组合起来

  • Support for serving static resources, including support for WebJars (covered later in this document)).     静态资源文件夹路径,WebJars
  • 自动注册了ConverterGenericConverter, and Formatter beans.

Converter :转换器 public string hello(User user) 类型转换使用

Formatter  :格式化器    2017.12.12 -》date

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

registry.addConverter(converter);

HttpMessageConverter :springmvc用来转换http请求和响应的 User--json

HttpMessageConverters 是从容器中确定的,获取所有的HttpMessageConverter

自己给容器中添加HttpMessageConverter,只需要将自己的组件注册到容器中(@Bean,@Component)

  • Automatic registration of MessageCodesResolver (covered later in this document).                        定义错误代码生成规则
  • Static index.html support.                     静态首页访问
  • Custom Favicon support (covered later in this document).                                                favicon .ico
  • Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).

                         我们可以配置一个ConfigurableWebBindingInitializer 来替换默认的(添加到容器中)

                         初始化WebDataBinder  请求数据---》JavaBean

org.springframework.boot.autoconfigure.web:web的所有自动场景

ctrl+N打开springmvc的自动配置类WebMvcAutoConfiguration,ctrl+f搜索ContentNegotiatingViewResolver 

@Bean
@ConditionalOnBean(ViewResolver.class)
@ConditionalOnMissingBean(name = "viewResolver", value = ContentNegotiatingViewResolver.class)
public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
   ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
   resolver.setContentNegotiationManager(beanFactory.getBean(ContentNegotiationManager.class));
   // ContentNegotiatingViewResolver uses all the other view resolvers to locate
   // a view so it should have a high precedence
   resolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
   return resolver;
}

点进ContentNegotiatingViewResolver ,找到resolveViewName方法,点进getCandidateViews方法

ContentNegotiatingViewResolver 中的initServletContext方法

打开Springboot04WebRestfulcrudApplication启动类

package com.cnstrong.springboot04webrestfulcrud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

import java.util.Locale;

@SpringBootApplication
public class Springboot04WebRestfulcrudApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot04WebRestfulcrudApplication.class, args);
    }

    @Bean
    public ViewResolver myViewResolver(){
        return new MyViewResolver();
    }

    //alt+insert自动补齐实现类
    private static class MyViewResolver implements ViewResolver{

        @Override
        public View resolveViewName(String viewName, Locale locale) throws Exception {
            return null;
        }
    }
}

ctrl+N搜索DispatcherServlet找到doDispatch方法打个断点以debug的方法运行

=====================

在文件中配置日期格式化的规则

private <T> Collection<T> getBeansOfType(Class<T> type) {
   return this.beanFactory.getBeansOfType(type).values();
}

==========

Spring MVC uses the HttpMessageConverter interface to convert HTTP requests and responses. Sensible defaults are included out of the box. For example, objects can be automatically converted to JSON (by using the Jackson library) or XML (by using the Jackson XML extension, if available, or by using JAXB if the Jackson XML extension is not available). By default, strings are encoded in UTF-8.

If you need to add or customize converters, you can use Spring Boot’s HttpMessageConverters class, as shown in the following listing:

import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.*;
import org.springframework.http.converter.*;

@Configuration
public class MyConfiguration {

	@Bean
	public HttpMessageConverters customConverters() {
		HttpMessageConverter<?> additional = ...
		HttpMessageConverter<?> another = ...
		return new HttpMessageConverters(additional, another);
	}

}

Any HttpMessageConverter bean that is present in the context is added to the list of converters. You can also override default converters in the same way.

==============

=====================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值