@EnableMvc的原理

发布时间:2024年01月20日
所以,加了这个注解,就会有一写初始化的操作
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
导入了DelegatingWebMvcConfiguration  extends WebMvcConfigurationSupport{
    // WebMvcConfigurerComposite implements WebMvcConfigurer
    // WebMvcConfigurerComposite实现了WebMvcConfigurer接口,同时还持有多个List<WebMvcConfigurer>
    private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();{
        // WebMvcConfigurer配置相关的类
        private final List<WebMvcConfigurer> delegates = new ArrayList<>();
        // 下列方法都是调用当前持有的所有WebMvcConfigurer对应的方法
        public void addWebMvcConfigurers(List<WebMvcConfigurer> configurers) {
            if (!CollectionUtils.isEmpty(configurers)) {
                this.delegates.addAll(configurers);
            }
        }
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            for (WebMvcConfigurer delegate : this.delegates) {
                delegate.addInterceptors(registry);
            }
        }
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            for (WebMvcConfigurer delegate : this.delegates) {
                delegate.configureMessageConverters(converters);
            }
        }
        @Override
        public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
            for (WebMvcConfigurer delegate : this.delegates) {
                delegate.addReturnValueHandlers(returnValueHandlers);
            }
        }
    }
    // WebMvcConfigurerComposite是通过set注入的方式,将Spring容器中WebMvcConfigurer类型的bean都注入
    @Autowired(required = false)
    public void setConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.configurers.addWebMvcConfigurers(configurers);
        }
    }

    // 注入bean,RequestMappingHandlerMapping
    public RequestMappingHandlerMapping requestMappingHandlerMapping(){
        RequestMappingHandlerMapping mapping = createRequestMappingHandlerMapping();
        mapping.setOrder(0);
        mapping.setInterceptors(getInterceptors(conversionService, resourceUrlProvider){
                                    if (this.interceptors == null) {
                                        InterceptorRegistry registry = new InterceptorRegistry();
                                        // 执行WebMvcConfigurerComposite的添加拦截器方法
                                        addInterceptors(registry);
                                        // 注册两个拦截器
                                        registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService));
                                        registry.addInterceptor(new ResourceUrlProviderExposingInterceptor(mvcResourceUrlProvider));
                                        // 将所有拦截器进行遍历,如果有设置了includePatterns或者excludePatterns,则包装成MappedInterceptor
                                        this.interceptors = registry.getInterceptors();{
                                            return this.registrations.stream().sorted(INTERCEPTOR_ORDER_COMPARATOR)
                                            				.map(InterceptorRegistration::getInterceptor){
                                            				    if (this.includePatterns == null && this.excludePatterns == null) {
                                                                    return this.interceptor;
                                                                }
                                                                MappedInterceptor mappedInterceptor = new MappedInterceptor(
                                                                        StringUtils.toStringArray(this.includePatterns),
                                                                        StringUtils.toStringArray(this.excludePatterns),
                                                                        this.interceptor);
                                                                if (this.pathMatcher != null) {
                                                                    mappedInterceptor.setPathMatcher(this.pathMatcher);
                                                                }
                                                                return mappedInterceptor;
                                            				}
                                            				.collect(Collectors.toList());
                                        }
                                    }
                                    return this.interceptors.toArray();
                                });
        // 设置跨域配置
        // 执行所有WebMvcConfigurer中的addCorsMappings
        mapping.setCorsConfigurations(getCorsConfigurations());
        // 其他设置
        return mapping;
    }

    @Bean
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter(){
        // 创建对象
        RequestMappingHandlerAdapter adapter = createRequestMappingHandlerAdapter();
        // 设置消息转换器
        // 同时执行所有WebMvcConfigurer的configureMessageConverters和extendMessageConverters
        // 如果没有,根据导包给一些默认的转换器
        adapter.setMessageConverters(getMessageConverters());
        // 设置WebBinder初始化器 ConfigurableWebBindingInitializer
        adapter.setWebBindingInitializer(getConfigurableWebBindingInitializer(conversionService, validator));
        // 设置自定义的参数解析器,同时执行所有WebMvcConfigurer的addArgumentResolvers
        adapter.setCustomArgumentResolvers(getArgumentResolvers());
        // 设置自定义的返回值处理器,同时执行所有WebMvcConfigurer的addReturnValueHandlers
        adapter.setCustomReturnValueHandlers(getReturnValueHandlers());
        // 如果存在json,添加两个JsonViewRequestBodyAdvice,JsonViewResponseBodyAdvice
        if (jackson2Present) {
            adapter.setRequestBodyAdvice(Collections.singletonList(new JsonViewRequestBodyAdvice()));
            adapter.setResponseBodyAdvice(Collections.singletonList(new JsonViewResponseBodyAdvice()));
        }
        // 添加两个返回异步结果的拦截器
        adapter.setCallableInterceptors(configurer.getCallableInterceptors());
        adapter.setDeferredResultInterceptors(configurer.getDeferredResultInterceptors());
        return adapter;
    }
    // 等等等等其他组件,视图解析器,语言解析器,异常解析器
}

文章来源:https://blog.csdn.net/JavaMyDream/article/details/135723610
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。