spring web development - 禁用静态内容缓存

rob*_*011 3 java configuration spring spring-boot

我正在用 Spring 开发一个 angularjs 应用程序。

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

我已经尝试过这个...

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

}
Run Code Online (Sandbox Code Playgroud)

然后 ...

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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的 build.gradle 文件

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 {
    // /sf/ask/981732951/#25694764
    exclude module: 'slf4j-log4j12'
}
Run Code Online (Sandbox Code Playgroud)

lub*_*nac 5

只需将此配置选项放入您的 application.properties 中:

spring.resources.chain.cache=false # Disable caching in the Resource chain.
Run Code Online (Sandbox Code Playgroud)

您可能还想查看与Spring Boot提供的静态内容相关的更细粒度的配置选项(向下滚动到部分# SPRING RESOURCES HANDLING)。

此外,可能存在由 Spring Boot 及其容器(例如 Web 浏览器)未处理的基础设施缓存的静态资源。如果您想克服这种类型的缓存,可以选择使用称为cache busting. 阅读Spring Boot 文档的这一部分以获取有关它的更多信息。