Disabling cache in Spring MVC 3

Disabling cache on pages by the browser within a website is an important aspect since pages are reloaded from the server and their contents are always up to date. This is done using the three HTTP headers with the following values:

Pragma = no-cache
Expires = Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control = no-cache, no-store

Of course, Spring 3 supports this mechanim within its request processing but it isn’t enabled by defaut and the configuration of this aspect is a bit hidden. As a matter of fact, Spring MVC since Spring 2.5 uses an annotation-based programming model and everything is configured by default under the hood. Disabling cache is done using the property cacheSeconds of the AnnotationMethodHandlerAdapter. You simply need to add a bean of this class within the application context associated with the DispatcherServlet of Spring MVC. The following code describes the complete configuration of this aspect:

    <!-- Autodetecting annotated Spring components within packages -->
    <context:component-scan base-package="com.manning.sdmia.web"/>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="cacheSeconds" value="0" />



This entry was posted in Java, Spring, Web2. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s