将spring上下文从xml传递给java

时间:2015-05-22 11:06:08

标签: spring-mvc

我正在重写从xml到java类的spring上下文,但我不知道这部分:

<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>

我已经重写了这个,但我不知道拦截器和映射标签:

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

    return webContentInterceptor;
}

1 个答案:

答案 0 :(得分:1)

假设您有一个类似WebConfig的类,您已添加@EnableWebMvc,请将其修改为以下内容:

@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);
    }

}