【问题标题】:Dynamically create HttpRequestHandling inside a spring application在 Spring 应用程序中动态创建 HttpRequestHandling
【发布时间】:2016-02-26 22:38:25
【问题描述】:

我正在处理能够在正在运行的 Web 应用程序上动态添加 Spring 集成 httpInbound 侦听器的要求,目前我正在通过以下代码创建一个 HttpRequestHandlingController bean:

            DefaultListableBeanFactory beanFactory = getBeanFactory();
            // first we crate the definition for the connection factory
            BeanDefinitionBuilder builder = getBeanBuilderForClass(HttpRequestHandlingController.class);
            builder.addConstructorArgValue(true);
            builder.addPropertyReference("requestChannel", channelName + "In");
            builder.addPropertyReference("replyChannel", channelName + "Out");
            builder.addPropertyValue("supportedMethods", methodNames);
            builder.addPropertyValue("requestPayloadType", payloadType);
            builder.addPropertyReference("messageConverters", "converters");
            builder.addPropertyValue("viewName", "jsonView");
            builder.addPropertyValue("path", path);

            beanFactory.registerBeanDefinition(name, builder.getBeanDefinition());

            getLogger().info("HTTP Inbound Gateway succesfully created: " + name);

            AbstractEndpoint comp =(HttpRequestHandlingController) getNewContextBeanFactory().getBean(name);
            comp.start();

根据日志,似乎一切都很好:

INFO  inbound-hce-rest-connector - Creating new HTTP Inbound Gateway: inbound-hce-rest-connectorInboundHttp
INFO  inbound-hce-rest-connector - HTTP Inbound Gateway succesfully created: inbound-hce-rest-connectorInboundHttp
DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'inbound-hce-rest-connectorInboundHttp'
DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'inbound-hce-rest-connectorInboundHttp'
DEBUG o.s.i.h.i.HttpRequestHandlingController - 'Jaxb2RootElementHttpMessageConverter' was added to the 'defaultMessageConverters'.
DEBUG o.s.i.h.i.HttpRequestHandlingController - 'MappingJackson2HttpMessageConverter' was added to the 'defaultMessageConverters'.
DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'inbound-hce-rest-connectorInboundHttp' to allow for resolving potential circular references
DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'inbound-hce-rest-connectorConvChannelIn'
DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'inbound-hce-rest-connectorConvChannelOut'
DEBUG o.s.b.BeanUtils - No property editor [org.springframework.http.HttpMethodEditor] found for type org.springframework.http.HttpMethod according to 'Editor' suffix convention
DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'converters'
DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'integrationGlobalProperties'
DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'inbound-hce-rest-connectorInboundHttp'
DEBUG o.s.i.h.i.HttpRequestHandlingController - Unable to locate MultipartResolver with name 'multipartResolver': no multipart request handling will be supported.
DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'integrationEvaluationContext'
DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'inbound-hce-rest-connectorInboundHttp'
INFO  o.s.i.h.i.HttpRequestHandlingController - started inbound-hce-rest-connectorInboundHttp

路径是在运行时设置的,在这里你可以看到调试的快照

此外,此上下文在此 servlet 定义之上运行:

<servlet>
    <servlet-name>InboundGCRestServices</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/applicationContext.xml</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>InboundGCRestServices</servlet-name>
    <url-pattern>/service/*</url-pattern>
</servlet-mapping>

但是当我发送请求时,我会在服务器上得到这个:

DEBUG o.s.w.s.DispatcherServlet - DispatcherServlet with name 'InboundGCRestServices' processing POST request for [/inbound-grand-central/service/hceServices/processIsoTransaction]
WARN  o.s.w.s.PageNotFound - No mapping found for HTTP request with URI [/inbound-grand-central/service/hceServices/processIsoTransaction] in DispatcherServlet with name 'InboundGCRestServices'
DEBUG o.s.w.s.DispatcherServlet - Successfully completed request

从这篇文章http://duckranger.com/2012/04/spring-mvc-dispatcherservlet/ 我了解到 servlet 在启动时初始化了映射,因为我的监听器的创建可以在 servlet 初始化之后的任何时间发生,我怎样才能让 servlet 刷新并“看到”新的定义,甚至这是正确的方法?嵌入容器是否希望码头更合适?请指教。

【问题讨论】:

    标签: spring spring-mvc spring-integration


    【解决方案1】:

    您不能在运行时修改DispatcherServlethandlerMappings

    我还没有这样做,但看起来您可以将 detectAllHandlerMappings 设置为 false 并编写自己的 HandlerMapping 并将一个实例添加到具有 bean 名称 handlerMapping 的上下文中。

    然后您可以将新 bean 添加到您的自定义处理程序映射类中。

    您也许可以使用 IntegrationRequestMappingHandlerMapping 中的逻辑 - 它是 final,因此您不能对其进行子类化。

    【讨论】:

    • 谢谢,这周我会试一试,让你知道它是否有效,但非常有意义
    猜你喜欢
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2020-06-25
    • 1970-01-01
    相关资源
    最近更新 更多