0

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 ファイルの両方がプロパティを確認できるようになりました。

4

1 に答える 1

1

マーティン・デイナムの引用:

いいえ、それがどのように機能するかという構成の問題ではありません。は環境にプロパティを追加しません。@PropertySource が行う場所。

したがって<ctx:property-placeholder>、XML 構成から削除する必要があります。引き続き使用@PropertySource("classpath:config.properties")し、次の Bean 定義も追加します。

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

static同じ .xml 内の他のすべての Bean を熱心にロードしないようにする必要があることに注意してください@Configuration

于 2018-05-18T13:26:24.263 に答える