java spring web development - 禁用静态内容缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36525797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring web developement - disable caching for static content
提问by robie2011
I'm developping a angularjs application with Spring.
我正在用 Spring 开发一个 angularjs 应用程序。
I often have to change my html/javascript file and I noticed that spring is caching static contents. How can I disable that?
我经常不得不更改我的 html/javascript 文件,我注意到 spring 正在缓存静态内容。我怎样才能禁用它?
I already tried this ...
我已经尝试过这个...
@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());
}
}
and that ...
然后 ...
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;
}
this is my build.gradle file
这是我的 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'
}
采纳答案by robie2011
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.
这是一个愚蠢的错误。我正在编辑 HTML/CSS/JS 源文件,但编译和部署的版本不受此修改的影响。
回答by luboskrnac
Just put this configuration option into your application.properties:
只需将此配置选项放入您的 application.properties 中:
spring.resources.chain.cache=false # Disable caching in the Resource chain.
You may want to also take a look at more fine grained config options related to Static content served by Spring Boot(scroll down to section # SPRING RESOURCES HANDLING
).
您可能还想查看与Spring Boot提供的静态内容相关的更细粒度的配置选项(向下滚动到部分# SPRING RESOURCES HANDLING
)。
Additionally, there may be static resources cached by infrastructure that is not handled by Spring Boot and it's container (e.g Web Browser). If you want to overcome this type of caching, there is option to use technique called cache busting
. Read this section of Spring Boot docsto get more info about it.
此外,可能存在由 Spring Boot 及其容器(例如 Web 浏览器)未处理的基础设施缓存的静态资源。如果您想克服这种类型的缓存,可以选择使用称为cache busting
. 阅读Spring Boot 文档的这一部分以获取有关它的更多信息。
回答by Sasha77ru
To see not compiled but the very version of static content you're editing set in application.yml something like:
要查看未编译但您在 application.yml 中编辑的静态内容版本,例如:
spring:
profiles: dev
resources:
static-locations: file:src/main/resources/static
But do so only in dev profile to avoid problem after deployment.
但只能在开发配置文件中这样做以避免部署后出现问题。