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。我想我不能使用上下文刷新事件来确保安全。
答案 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()
中被触发。
您确实应该为自己的Ordered
和ApplicationListener<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 = 0
,IntegrationRequestMappingHandlerMapping
已经被初始化了。
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);
}
}