11 MVC configuration principle of spring-boot

11.1 Automatic configuration provided by spring-boot for MVC

        1.ContentNegotiatingViewResolver view resolver;

        2. Static resources or support WebJars;

        3. Automatic registration type converter: For example, the field of the user submitted by the front desk means that the backend automatically encapsulates it;

        4. HttpMessageConverters: Convert http requests and responses, such as converting a user string to a json string;

        5. MessageCodesResolver: define error messages;

        6.index.html : support home page mapping;

        7.Favicon: support icon mapping;

        8. ConfigurableWebBindingInitializer: supports the initial binding of data web, and encapsulates requests and data into javaBean;

11.2 Extended configuration principle (understanding)

        First create a package called config, in which all custom configurations are placed;

        Then, create a configuration class called MyMvcConfig, add @Configuration annotation, use this class to fully expand the configuration of springMVC; this class must implement the WebMvcConfigurer interface, and implement the methods inside;

        First of all, to implement a view resolver, search for the ContentNegotiatingViewResolver class, and find that this class implements the ViewResolver interface, so this class is a view resolver;

public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport implements ViewResolver, Ordered, InitializingBean

        Write a static inner class yourself:

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放到接口里
    @Bean
    public ViewResolver myViewResolver(){
        return new MyViewResolver();
    }

    // 自定义了一个试图解析器
    public static class MyViewResolver implements ViewResolver{
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
        }
    }
}

         All requests (including view resolvers) will go through DispatcherServlet, which has a method called doDispatcher, all requests and responses will go through this, so make a breakpoint.

         Click Debug to refresh the page. Then it will automatically jump to the breakpoint, the content of the console is as follows:

        Open this, find viewResolver and click on it, and find that the view resolver just created has been loaded and passed the doDispatcher method.

12.3 Summary

        In springboot, there are a lot of xxxx Configuration to help us expand configuration, as long as we see this thing, we should pay attention!

        

        

Guess you like

Origin blog.csdn.net/no996yes885/article/details/131880226