Spring 5XmlのWebContentInterceptorでcacheControlMappingsを設定する方法

Aug 24 2020

Spring MVCのいくつかのURLにcache-controlディレクティブ(public秒とmax-age秒の両方を設定)を追加したいのですが、applicationContext.xmlを介してのみこれらの変更を行いたいと思います。

私は、マップのプロパティを設定しようとしているcacheControlMappingsのがorg.springframework.web.servlet.mvc.WebContentInterceptor、唯一の問題は、プロパティにはsetterメソッドを持っていないクラスのデザインです。回避策として、addCacheMappingを使用してメソッドを呼び出していorg.springframework.beans.factory.config.MethodInvokingBeanます。

spring-mvc-config.xmlでの構成は次のとおりです。CacheControl次のようにBeanを作成しています。デバッグによって、このBeanがアプリケーションコンテキストに入力された適切な値で正常に作成されることを確認しました。

<bean id="cacheControlFactory" class="org.springframework.http.CacheControl" factory-method="maxAge">
    <constructor-arg index="0" value="3600"/>
    <constructor-arg index="1">
        <value type="java.util.concurrent.TimeUnit">SECONDS</value>
    </constructor-arg>
</bean>

<bean id="myCacheControl" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject">
        <ref bean="cacheControlFactory"/>
    </property>
    <property name="targetMethod">
        <value>cachePublic</value>
    </property>
</bean>

-それから私は、このメソッドを呼び出したいpublic void addCacheMapping(CacheControl cacheControl, String... paths)WebContentInterceptorマップにエントリを追加しますましたcacheControlMappings

このメソッドをプログラムで呼び出すとうまく機能することを確認したので、XMLから呼び出すと正常に機能するはずです。しかし、以下に示すように、私は同じことをしようとしていますが、これは私には機能せず、最終的なマップに追加されたエントリはゼロになります。

<bean class="org.springframework.beans.factory.config.MethodInvokingBean">
    <property name="targetObject">
        <ref bean="webContentInterceptor"/>
    </property>
    <property name="targetMethod">
        <value>addCacheMapping</value>
    </property>
    <property name="arguments">
        <list>
            <ref bean="myCacheControl" />
            <value>/home</value>
            <value>/dp/**</value>
            <value>/**/b/*</value>
        </list>
    </property>
</bean>

上記の呼び出しが機能しMethodInvokingBeanないのはなぜですか?引数を間違って設定していますか?可変引数には別の処理が必要ですか?サーバーの起動中にもエラーがスローされることはありません。

また、私はこのSOスレッド(Spring 4.2以降のapplicationContext.xmlでCache-control:privateを設定する方法)を知っています。受け入れられた回答では、XML自体でこれを行う方法はないと述べています。上記のソリューションを実装して、それが正しいかどうかを再確認したかったのですが、機能していませんが、理由がわかりません。

回答

Anurag Aug 25 2020 at 04:52

私が疑っていたように、引数が入力される方法に問題がありました。スプリングインジェクションの可変引数は、引数のオーバーロードではなく、明示的なリストとして指定する必要があります(Javaで行われるように)。

したがって、そのようなメソッドを呼び出す正しい方法-

public void addCacheMapping(CacheControl cacheControl, String... paths)

SpringapplicationContext.xmlは次のとおりです-

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject">
        <ref bean="webContentInterceptor"/>
    </property>
    <property name="targetMethod">
        <value>addCacheMapping</value>
    </property>
    <property name="arguments">
        <list>
            <ref bean="myCacheControl" />
            <list>
                <value>/home</value>
                <value>/dp/**</value>
                <value>/**/b/*</value>
            </list>
        </list>
    </property>
</bean>

ご覧のとおり、私は今使用していMethodInvokingFactoryBeanます。どういうわけかMethodInvokingBean私のために働いていませんでした。