春ブーツ春のMVCの自動設定

公式サイトの説明

  • 含めるContentNegotiatingViewResolverBeanNameViewResolver豆。

ビューレンダラの自動設定。
WebMvcAutoConfigurationでこのクラスで直接検索。

@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クラスを入力します。

protected void initServletContext(ServletContext servletContext) {
    Collection<ViewResolver> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.obtainApplicationContext(), ViewResolver.class).values();
    ViewResolver viewResolver;
    if (this.viewResolvers == null) {
        this.viewResolvers = new ArrayList(matchingBeans.size());
        Iterator var3 = matchingBeans.iterator();

        while(var3.hasNext()) {
            viewResolver = (ViewResolver)var3.next();
            if (this != viewResolver) {
                this.viewResolvers.add(viewResolver);
            }
        }
    } else {
        for(int i = 0; i < this.viewResolvers.size(); ++i) {
            viewResolver = (ViewResolver)this.viewResolvers.get(i);
            if (!matchingBeans.contains(viewResolver)) {
                String name = viewResolver.getClass().getName() + i;
                this.obtainApplicationContext().getAutowireCapableBeanFactory().initializeBean(viewResolver, name);
            }
        }
    }

    AnnotationAwareOrderComparator.sort(this.viewResolvers);
    this.cnmFactoryBean.setServletContext(servletContext);
}

これは、ビューリゾルバのすべての項目が一緒にグループ化されていることがわかりました。
その後、我々は独自のビューリゾルバを書くことができ、ContentNegotiatingViewResolverは自動的に結合されます。ビューを解析するときに、ビューは、それが適切なビューリゾルバを見つけるまで解析するパーサを横断します。


これらの3つは静的なリソースを扱っています。静的リソースマッピングを通じて、ある原理、。


  • 自動登録ConverterGenericConverterおよびFormatter豆。

自動的に登録されたコンバータ、フォーマッタ。例えば、日付形式、カプセル化されたオブジェクトなどが挙げられます。

@Override
public void addFormatters(FormatterRegistry registry) {
    for (Converter<?, ?> converter : getBeansOfType(Converter.class)) {
        registry.addConverter(converter);
    }
    for (GenericConverter converter : getBeansOfType(GenericConverter.class)) {
        registry.addConverter(converter);
    }
    for (Formatter<?> formatter : getBeansOfType(Formatter.class)) {
        registry.addFormatter(formatter);
    }
}

httpMessageコンバータ。スプリングMVCの元のバージョンでは、応力解析、及び今JSONに変換、データを使用するパーサーを表示するために、ビューモデルです。

このカテゴリに見て:

public HttpMessageConverters(boolean addDefaultConverters,
        Collection<HttpMessageConverter<?>> converters) {
    List<HttpMessageConverter<?>> combined = getCombinedConverters(converters,
            addDefaultConverters ? getDefaultConverters() : Collections.emptyList());
    combined = postProcessConverters(combined);
    this.converters = Collections.unmodifiableList(combined);
}

また、どこでも、この考え方に、すべてのグループにまとめHttpMessageConverterにコンテナを置くので、私たちは、独自のコンバータを定義するのに役立ちます。


エラーコードコンバータが意味しています。


  • 自動使用ConfigurableWebBindingInitializer(カバー豆本書の後半)。
@Override
protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
    try {
        return this.beanFactory.getBean(ConfigurableWebBindingInitializer.class);
    }
    catch (NoSuchBeanDefinitionException ex) {
        return super.getConfigurableWebBindingInitializer();
    }
}

コンテナから取り、取得するには、親クラスのメソッドを呼び出します。これは、春ブーツが自動設定することができ、およびカスタムのサポートになり文言です。

public void initBinder(WebDataBinder binder) {
    binder.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
    if (this.directFieldAccess) {
        binder.initDirectFieldAccess();
    }

    if (this.messageCodesResolver != null) {
        binder.setMessageCodesResolver(this.messageCodesResolver);
    }

    if (this.bindingErrorProcessor != null) {
        binder.setBindingErrorProcessor(this.bindingErrorProcessor);
    }

    if (this.validator != null && binder.getTarget() != null && this.validator.supports(binder.getTarget().getClass())) {
        binder.setValidator(this.validator);
    }

    if (this.conversionService != null) {
        binder.setConversionService(this.conversionService);
    }

    if (this.propertyEditorRegistrars != null) {
        PropertyEditorRegistrar[] var2 = this.propertyEditorRegistrars;
        int var3 = var2.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            PropertyEditorRegistrar propertyEditorRegistrar = var2[var4];
            propertyEditorRegistrar.registerCustomEditors(binder);
        }
    }

}

バインダーデータは、データはフロントエンドに送信され、変換は、我々は、制御層パラメータ設定方法にバインドされました。


あなたは春ブーツMVCの機能を維持したいとあなたは追加追加したい場合はMVC構成(インターセプター、フォーマッタ、ビュー・コントローラ、およびその他の機能)を、あなた自身を追加することができます@ConfigurationタイプのクラスをWebMvcConfigurerしかしなし @EnableWebMvcあなたはのカスタムインスタンスを提供したい場合RequestMappingHandlerMappingRequestMappingHandlerAdapterまたはExceptionHandlerExceptionResolver、あなたが宣言することができWebMvcRegistrationsAdapter、そのようなコンポーネントを提供するために、インスタンスを。

また、他のいくつかの機能を追加したい、既存のプロパティを使用する場合は、設定WebMvcRegistrationsAdapterクラスをカスタマイズすることができますが、@EnableWebMvcコメントをキャンセルします。
@EnableWebMvcノートは追加した場合は、春ブーツ自動設定の一部は無効になります。

  1. コア@EnableWebMvc、DelegatingWebMvcConfigurationクラスを導入します。
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {

2)このコンフィギュレーションクラスはWebMvcConfigurationSupport継承します

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

3)、春ブーツ自動設定を有効にする一方で、そこにはWebMvcConfigurationSupportこのクラスではありませんので、自動設定が有効になりません。

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
        TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

4)@ EnableWebMvc WebMvcConfigurationSupportアセンブリが導入来る;
5)のみWebMvcConfigurationSupport SpringMVC基本的な機能を導入します。

要約:
1、場合コンポーネントの自動構成は、第一の対応するコンポーネントがあるか否かを判定し、存在する場合、負荷が存在しない場合、デフォルトが生成されます。複数のコンポーネントは、例えば、ビューリゾルバを可能にする場合、組み合わせ。私たちは、独自のコンポーネントを定義することができ、春ブーツは、私たちはコンフィギュレーションを展開するのに役立ちます。
2)SpringBootでxxxConfigurerの多くは、私たちはコンフィギュレーションを展開する手助けがあるでしょう。
3)、SpringBootに設定をカスタマイズするために私たちを助けるxxxCustomizerがたくさんあるでしょう。

ます。https://www.jianshu.com/p/2418f71011c0で再現

おすすめ

転載: blog.csdn.net/weixin_34255055/article/details/91273177