gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:07:57 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com