spring web development - 禁用静态内容缓存 [英] spring web developement - disable caching for static content

查看:46
本文介绍了spring web development - 禁用静态内容缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring 开发 angularjs 应用程序.

I'm developping a angularjs application with Spring.

我经常需要更改我的 html/javascript 文件,并且我注意到 spring 正在缓存静态内容.我怎样才能禁用它?

I often have to change my html/javascript file and I noticed that spring is caching static contents. How can I disable that?

我已经试过了...

@Configuration
@AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
class WebMvcConfig extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {

    @Autowired
    private Environment env;

    @Bean
    public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
        return new ResourceUrlEncodingFilter();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        boolean devMode = this.env.acceptsProfiles("dev");
        //boolean useResourceCache = !devMode;
        boolean useResourceCache = false;
        Integer cachePeriod = devMode ? 0 : null;

        registry.addResourceHandler("/public/**")
                .addResourceLocations("/public/", "classpath:/public/")
                .setCachePeriod(cachePeriod)
                .resourceChain(useResourceCache)
                .addResolver(new GzipResourceResolver())
                .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
                .addTransformer(new AppCacheManifestTransformer());
    }

}

那个……

WebContentInterceptor webContentInterceptor;
public @Bean WebContentInterceptor webContentInterceptor () {
    if (this.webContentInterceptor == null) {
        this.webContentInterceptor = new WebContentInterceptor();

        this.webContentInterceptor.setAlwaysUseFullPath (true);
        this.webContentInterceptor.setCacheSeconds (0);


        this.webContentInterceptor.setCacheMappings (new Properties() {
            private static final long serialVersionUID = 1L;

            {
                put ("/styles/**", "0");
                put ("/scripts/**", "0");
                put ("/images/**", "0");
                put ("/js/**", "0");
            }
        });
    }

    return this.webContentInterceptor;
}

这是我的 build.gradle 文件

this is my build.gradle file

group 'xyz'
version '1.0-SNAPSHOT'
buildscript{
    repositories{
        mavenCentral()
    }
    dependencies{
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repository.jboss.org/nexus/content/repositories/releases" }
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.security:spring-security-web:4.0.3.RELEASE'
    compile 'net.sf.dozer:dozer:5.4.0'

    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'com.h2database:h2'// For Testing purpose
    compile 'com.google.guava:guava:19.0' // google library for data collections

    testCompile("junit:junit")
    //testCompile group: 'junit', name: 'junit', version: '4.11'
}

task wrapper(type: Wrapper){
    gradleVersion = '2.3'
}

configurations.all {
    // https://stackoverflow.com/questions/14024756/slf4j-class-path-contains-multiple-slf4j-bindings/25694764#25694764
    exclude module: 'slf4j-log4j12'
}

推荐答案

这是一个愚蠢的错误.我正在编辑 HTML/CSS/JS 源文件,但编译和部署的版本不受此修改的影响.

It was a silly mistake. I was editing the HTML/CSS/JS source files but the compiled and deployed version was not affected from this modification.

这篇关于spring web development - 禁用静态内容缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆