在spring应用程序中动态创建HttpRequestHandling

时间:2016-02-26 22:38:26

标签: spring spring-mvc spring-integration

我正在努力要求能够在正在运行的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

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

Debug of set values

此外,此上下文在此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刷新和#34 ;参见&#34;新的定义,甚至是正确的方法?一个被挤的容器会让码头更合适吗?请咨询。

1 个答案:

答案 0 :(得分:0)

您无法在运行时修改handlerMappings detectAllHandlerMappings

我还没有完成它,但看起来您可以将false设置为HandlerMapping并编写自己的handlerMapping并使用bean名称{ {1。}}

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

您可以使用IntegrationRequestMappingHandlerMapping - final中的逻辑,这样您就无法对其进行子类化。