java - Spring Boot或者Tomcat缓存静态资源,每次刷新都得重新构建

标签 java spring spring-mvc tomcat spring-boot

Spring Boot 或 Tomcat 在开发环境中缓存我的静态资源 .js 和 .css 文件,我每次都必须构建 -> 构建项目(在 IntelliJ 中)才能获取刷新的版本。

我尝试了这些应用程序属性,但它们不起作用。

spring.resources.cache-period=0
spring.resources.chain.cache=false
spring.resources.chain.html-application-cache=false
spring.resources.chain.strategy.content.enabled=false
spring.resources.chain.strategy.content.paths=/**

我尝试使用此 bean 来配置 Tomcat,但效果不佳:

@Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory() {

            @Override
            protected void postProcessContext(Context context) {
                final int cacheSize = 0;
                StandardRoot standardRoot = new StandardRoot(context);
                standardRoot.setCachingAllowed(false);
                standardRoot.setCacheMaxSize(cacheSize);
                context.setResources(standardRoot);    
            }
        };
        return tomcatFactory;
    }

当更改js和css文件时,我不想每次都进行构建项目,我什至可以使用BrowserSync之类的东西自动将更改应用到网页,但为此我需要Tomcat/Spring在开发中提供静态资源环境,无需我单击“构建项目”。

该文档并不是很有帮助,根据该文档,除了每次更改 css 或 js 文件时单击“构建”之外,没有其他办法。 http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-hotswapping

但我相信这应该是非常简单且完全可以实现的事情,不是吗。

有什么想法我下一步应该尝试什么吗?

谢谢

最佳答案

这在没有缓存中。当您在单独的文件夹(例如 target)中构建项目时,您的源文件(*.js*.css)将被复制它们被发送到浏览器。 Build Project 按钮复制了此文件,然后您会看到它们已更改。

您可以启用Build project automatically IntelliJ 中的选项或使用其他工具(例如 bower)在更改后复制文件。

关于java - Spring Boot或者Tomcat缓存静态资源,每次刷新都得重新构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43045487/

相关文章:

java - 根据表子集的最大值获取行

java - 没有Implement关键字,我们可以使用接口(interface)吗?

java - Spring : ClassNotFound exception

Java try catch NullPointerException

database - 带有数据库 : java. lang.ClassNotFoundException : org. springframework.security.core.userdetails.jdbc.JdbcDaoImpl 的 Spring Security 应用程序

SPRING:在 spring 中由工厂实例化创建的 bean 中使用 Autowiring

java - Spring - 使用 new 是一种不好的做法吗?

java - Spring 中的 @Qualifier 注解不起作用

java - 如何使用 JSch 的 SCP 支持复制服务器上的文件?

java - Thymeleaf 说 Model 属性为 null