gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:28:41 26 4
gpt4 key购买 nike

目前我有一个简单的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com