Spring Integration webapp 構成で、プロパティ プレースホルダーを追加しました。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
...
xsi:schemaLocation="http://www.springframework.org/schema/context
...
">
<ctx:component-scan ... />
<ctx:annotation-config />
<mvc:annotation-driven />
<ctx:property-placeholder location="classpath:config.properties" trim-values="true" />
これはそのファイルの内容です:
apiPath=/requests
http inbound-channel-adapter でその値を使用しようとしたため、この構成が機能すると確信しています。
<int-http:inbound-channel-adapter id="/api${apiPath}"
channel="httpRequestsChannel"
path="${apiPath}"
...>
</int-http:inbound-channel-adapter>
プロパティ値を変更すると、フロントエンド アプリケーションがエンドポイントに到達できなくなります。
ただし、さらにコンテキストでは、次のように構成されたエンドポイントがあります。
<int:header-value-router input-channel="httpRequestsChannel" ... >
<int:mapping value="POST" channel="httpRequestsPostChannel" />
...
</int:header-value-router>
<int:channel id="httpRequestsPostChannel" />
<int:chain input-channel="httpRequestsPostChannel">
<int:transformer method="transform">
<bean class="transformers.RequestToMessageFile" />
</int:transformer>
...
プロパティ値を読み取りたい場所:
public class RequestToMessageFile {
@Autowired
private Environment env;
// ...
public Message<?> transform(LinkedMultiValueMap<String, Object> multipartRequest) {
System.out.println("Value: " + env.getProperty("apiPath"));
しかし、コンソールには次のように表示されます。
Value: null
Web アプリ環境全体の一部となる XML でプロパティ ソースを宣言したことがあると思いますが、何が欠けているのでしょうか? 別の場所でソースを宣言する必要がありますか?
次の注釈を追加すると、次のことに気付きました。
@Configuration
@PropertySource("classpath:config.properties")
public class RequestToMessageFile {
プロパティは正しく見つかったので、これは単なる構成の問題だと思います。
重要な場合は、web.xml
統合を構成する部分を次に示します。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/META-INF/spring.integration/context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
アップデート
この回答の一部に従って、XML ファイルから削除<ctx:property-placeholder>
し、次の Bean を追加しました。
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:config.properties")
public class WebappConfig {
}
これで、Bean と XML ファイルの両方がプロパティを確認できるようになりました。