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

标签 java spring spring-boot configuration

我正在使用 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());
    }

}

还有那个……

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

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

最佳答案

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

spring.resources.chain.cache=false # Disable caching in the Resource chain.

您可能还想查看与 Static content served by Spring Boot 相关的更细粒度的配置选项(向下滚动到部分 # SPRING RESOURCES HANDLING)。

此外,基础设施可能缓存了一些静态资源,这些资源不由 Spring Boot 及其容器(例如 Web 浏览器)处理。如果您想克服这种类型的缓存,可以选择使用称为 cache busting 的技术。阅读this section of Spring Boot docs获取有关它的更多信息。

关于java - spring web development - 禁用静态内容的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36525797/

相关文章:

java - 使用 Hibernate 和 Spring 时连接过多

java - spring 使用的代理模式

java - Maven 项目中 8u162-jdk 的拉取访问被拒绝

将 JPEG 转换为 TIFF 的 Java API

java - 为什么此代码示例会出现 org.hibernate.MappingException 异常?

java - Spring Boot + Spring Data + Oracle

java - Spring JPA : Providing Schema Name Dynamically

java - 属性值的自定义类型转换

java: Statement.RETURN_GENERATED_KEYS Err:S1000 Exp: 在结果集开始之前

java - 来自变量的 Spring 动态 HttpStatus