java - 动态配置spring的集成int-http :inbound-gateway

标签 java spring jetty spring-integration

目前我有一个简单的 war ,其中包含一些 Spring 集成配置,并且使用以下代码将该 war 部署到 jetty 容器中:

protected void createWac(File file, ConnectorConfig config) throws Exception, InterruptedException {
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setWar(file.getAbsolutePath());
    startServer(webapp, inboundConnector.getPort());
}

在 Spring Integration 配置中,我有一个像这样的 int-http:inbound-gateway

<!-- External listener definition -->
<int-http:inbound-gateway id="entryHttpInboundGateway"
    request-channel="myRequestChannel"
    path="myPath" 
    reply-channel="myReplyChannel"
    request-payload-type="com.me.MyType"
    supported-methods="POST" message-converters="converters"
    reply-timeout="1000">
</int-http:inbound-gateway>

我的目标是动态设置 myPath 的值和支持的方法值,我采用的第一种方法是在我的 xml 文件中添加变量,正如一些人在这篇文章中建议的那样:how to read System environment variable in Spring applicationContext

为此我尝试使用此配置

<!-- External listener definition -->
<int-http:inbound-gateway id="entryHttpInboundGateway"
    request-channel="myRequestChannel"
    path="#{ systemProperties['HTTP_PATH'] }" 
    reply-channel="myReplyChannel"
    request-payload-type="com.me.MyType"
    supported-methods="POST" message-converters="converters"
    reply-timeout="1000">
</int-http:inbound-gateway>

并在我的机器上设置环境,奇怪的是,当我设置它时,没有任何反应,也没有配置错误, channel 的初始化,或者找不到属性,我知道,因为如果我删除 #签名,或者输入我在控制台上得到的实际网址:

IntegrationRequestMappingHandlerMapping - Mapped "{[/{ systemProperties['HTTP_PATH'] }],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public abstract void org.springframework.web.HttpRequestHandler.handleRequest(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws javax.servlet.ServletException,java.io.IOException

关于如何解决这个问题有什么想法,或者有其他方法可以在将 war 的上下文文件部署到 jetty 容器之前将值注入(inject)到 war 的上下文文件中吗?

最佳答案

您必须查看 Spring 中的环境抽象:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-environment

并考虑在 PropertySourcesPlaceholderConfigurer 方面为您的应用程序添加一些钩子(Hook),并且在您的 ServletConfig 案例中看起来像这样:

For a common StandardServletEnvironment, the full hierarchy looks as follows, with the highest-precedence entries at the top: * ServletConfig parameters (if applicable, e.g. in case of a DispatcherServlet context) * ServletContext parameters (web.xml context-param entries) * JNDI environment variables ("java:comp/env/" entries) * JVM system properties ("-D" command-line arguments) * JVM system environment (operating system environment variables)

从另一边考虑迁移到Spring Boot .

关于java - 动态配置spring的集成int-http :inbound-gateway,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36463759/

相关文章:

java - org.json.JSONObject 无法转换为 Serialized_JSONObject

java - Spring Security + LDAP + CustomLdapAuthoritiesPopulator + RememberMe

java - Web 环境 Spring/Wicket/Derby/Jetty 中的 NoClassDefFoundError 错误问题

java - Jetty 9 - 为域验证证书启用 OCSP 装订

java - 在 spring security login-processing-url (/j_spring_security_check) 之前做过滤 Action

java - Camel jetty-activemq 是否为每条消息创建新的 activemq 连接

java - Jira插件开发依赖

JavaFX 位置未设置错误消息

java - 如何测试 Weka 文本分类 (FilteredClassifier)

json - 无法读取 JSON : Unrecognized field (. ..),未标记为可忽略