วิธีตั้งค่า cacheControlMappings ใน WebContentInterceptor ใน Spring 5 Xml

Aug 24 2020

ฉันต้องการเพิ่มคำสั่งควบคุมแคช (ตั้งค่าทั้งวินาทีสาธารณะและอายุสูงสุด) สำหรับ URL สองสามรายการใน Spring MVC และฉันต้องการทำการเปลี่ยนแปลงเหล่านั้นผ่าน applicationContext.xml เท่านั้น

ฉันกำลังพยายามที่จะตั้งในแผนที่ของสถานที่ให้บริการcacheControlMappingsของorg.springframework.web.servlet.mvc.WebContentInterceptorแต่ปัญหาเดียวก็คือการออกแบบของชั้นเรียนซึ่งมีวิธีการตั้งค่าไม่มีทรัพย์สิน เพื่อเป็นการแก้ปัญหาฉันกำลังเรียกใช้addCacheMappingวิธีการโดยใช้org.springframework.beans.factory.config.MethodInvokingBean.

คอนฟิกูเรชันของฉันใน spring-mvc-config.xml มีดังนี้ - ฉันกำลังสร้างCacheControlbean ดังต่อไปนี้และฉันตรวจสอบโดยการดีบักว่า 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)ของซึ่งจะเพิ่มรายการลงในแผนที่WebContentInterceptorcacheControlMappings

ฉันตรวจสอบแล้วว่าการเรียกวิธีนี้โดยใช้โปรแกรมทำงานได้ดีดังนั้นจึงควรใช้งานได้ดีหากฉันเรียกใช้จาก 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ไม่ทำงาน ฉันตั้งค่าอาร์กิวเมนต์ผิดหรือไม่? varargs ต้องการการจัดการที่แตกต่างกันหรือไม่? ฉันไม่เห็นข้อผิดพลาดใด ๆ เกิดขึ้นระหว่างการเริ่มต้นเซิร์ฟเวอร์

นอกจากนี้ฉันทราบถึงเธรด SO นี้ ( วิธีตั้งค่าการควบคุมแคช: ส่วนตัวด้วย applicationContext.xml ในฤดูใบไม้ผลิ 4.2 หรือใหม่กว่า ) ซึ่งคำตอบที่ยอมรับกล่าวถึงไม่มีวิธีดำเนินการใน XML เอง ฉันต้องการยืนยันอีกครั้งว่าถูกต้องหรือไม่โดยพยายามใช้โซลูชันข้างต้น แต่ไม่ได้ผล แต่ฉันไม่เห็นสาเหตุ

คำตอบ

Anurag Aug 25 2020 at 04:52

ตามที่ฉันสงสัยมีปัญหาเกี่ยวกับวิธีการสร้างข้อโต้แย้ง จำเป็นต้องกำหนด varargs ในการฉีดสปริงเป็นรายการที่ชัดเจนแทนที่จะเป็นอาร์กิวเมนต์ที่มากเกินไป (เช่นเดียวกับที่ทำใน Java)

ดังนั้นวิธีที่ถูกต้องในการเรียกใช้วิธีการดังกล่าว -

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

ในแอปพลิเคชัน springContext.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ไม่ได้ผลสำหรับฉัน