Spring-Integration:确保已初始化IntegrationRequestMappingHandlerMapping

时间:2018-10-28 14:27:30

标签: spring-integration springfox

IntegrationRequestMappingHandlerMapping有一个特殊要求,要在ContextRefreshedEvent上进行初始化。引用代码:

@Override
public void afterPropertiesSet() {
    // No-op in favor of onApplicationEvent
}

/**
 * {@link HttpRequestHandlingEndpointSupport}s may depend on auto-created
 * {@code requestChannel}s, so MVC Handlers detection should be postponed
 * as late as possible.
 * @see RequestMappingHandlerMapping#afterPropertiesSet()
 */
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    if (!this.initialized.getAndSet(true)) {
        super.afterPropertiesSet();
    }
}

结果是,当其他bean在应用程序启动期间尝试对其进行评估时,即使它们以阶段mappingRegistry实现SmartLifeCycle,其MAX_VALUE也为空。

就我而言,我正在尝试为Springfox实现一个spring-integration插件。其DocumentationPluginsBootstrapper需要访问请求映射以记录它们。

在开始询问其映射之前,如何确定IntegrationRequestMappingHandlerMapping已被初始化?监听ContextRefreshedEvent也是正确的方法,但是对@Order的价值很高吗?还是建议您使用其他事件?

更新:AbstractHandlerMapping已使用Order.LOWEST_PRECEDENCE。我想我不能使用上下文刷新事件来确保安全。

另请参阅related springfox issue

1 个答案:

答案 0 :(得分:1)

ContextRefreshedEvent实际上是应用程序上下文初始化的最后一步:

            // Allows post-processing of the bean factory in context subclasses.
            postProcessBeanFactory(beanFactory);

            // Invoke factory processors registered as beans in the context.
            invokeBeanFactoryPostProcessors(beanFactory);

            // Register bean processors that intercept bean creation.
            registerBeanPostProcessors(beanFactory);

            // Initialize message source for this context.
            initMessageSource();

            // Initialize event multicaster for this context.
            initApplicationEventMulticaster();

            // Initialize other special beans in specific context subclasses.
            onRefresh();

            // Check for listener beans and register them.
            registerListeners();

            // Instantiate all remaining (non-lazy-init) singletons.
            finishBeanFactoryInitialization(beanFactory);

            // Last step: publish corresponding event.
            finishRefresh();

在该finishRefresh()中被触发。

您确实应该为自己的OrderedApplicationListener<ContextRefreshedEvent>考虑一个Order.LOWEST_PRECEDENCE。同时Framework将IntegrationRequestMappingHandlerMapping注册到order == 0中:

private void registerRequestMappingHandlerMappingIfNecessary(BeanDefinitionRegistry registry) {
    if (HttpContextUtils.WEB_MVC_PRESENT &&
            !registry.containsBeanDefinition(HttpContextUtils.HANDLER_MAPPING_BEAN_NAME)) {
        BeanDefinitionBuilder requestMappingBuilder =
                BeanDefinitionBuilder.genericBeanDefinition(IntegrationRequestMappingHandlerMapping.class);
        requestMappingBuilder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        requestMappingBuilder.addPropertyValue(IntegrationNamespaceUtils.ORDER, 0);
        registry.registerBeanDefinition(HttpContextUtils.HANDLER_MAPPING_BEAN_NAME,
                requestMappingBuilder.getBeanDefinition());
    }
}

因此,您的确可以保存自己的侦听器中的映射。仅仅因为有了order = 0IntegrationRequestMappingHandlerMapping已经被初始化了。

WebFluxIntegrationRequestMappingHandlerMapping也是如此:

private void registerReactiveRequestMappingHandlerMappingIfNecessary(BeanDefinitionRegistry registry) {
    if (HttpContextUtils.WEB_FLUX_PRESENT &&
            !registry.containsBeanDefinition(WebFluxContextUtils.HANDLER_MAPPING_BEAN_NAME)) {
        BeanDefinitionBuilder requestMappingBuilder =
                BeanDefinitionBuilder.genericBeanDefinition(WebFluxIntegrationRequestMappingHandlerMapping.class);
        requestMappingBuilder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        requestMappingBuilder.addPropertyValue(IntegrationNamespaceUtils.ORDER, 0);
        registry.registerBeanDefinition(WebFluxContextUtils.HANDLER_MAPPING_BEAN_NAME,
                requestMappingBuilder.getBeanDefinition());

        BeanDefinitionReaderUtils.registerWithGeneratedName(
                new RootBeanDefinition(IntegrationHandlerResultHandler.class), registry);
    }
}