Juan Reina Pascual
Juan Reina Pascual

Reputation: 4588

pass spring context from xml to java

I´m rewriting a spring context from xml to java class but I don´t know this part:

<interceptors>
    <interceptor>
        <mapping path="/index.html"/>
        <beans:bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
            <beans:property name="cacheSeconds" value="0"/>
            <beans:property name="useExpiresHeader" value="true"/>
            <beans:property name="useCacheControlHeader" value="true"/>
            <beans:property name="useCacheControlNoStore" value="true"/>
        </beans:bean>
    </interceptor>
</interceptors>

I have rewritten this but the interceptors and mapping tags I don´t know:

@Bean
public WebContentInterceptor webContentInterceptor() {
    WebContentInterceptor webContentInterceptor = new WebContentInterceptor();
    webContentInterceptor.setCacheSeconds(0);
    webContentInterceptor.setUseExpiresHeader(true);
    webContentInterceptor.setUseCacheControlHeader(true);
    webContentInterceptor.setUseCacheControlNoStore(true);

    return webContentInterceptor;
}

Upvotes: 1

Views: 93

Answers (1)

geoand
geoand

Reputation: 63991

Assuming you have a class like WebConfig where you have added @EnableWebMvc, modify it to something like the following:

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        final WebContentInterceptor webContentInterceptor = new WebContentInterceptor();
        //the rest of the initialization here
        registry.addInterceptor(webContentInterceptor).addPathPatterns("/index.html);
    }

}

Upvotes: 1

Related Questions