Spring引导如何控制Tomcat缓存?

Spring引导如何控制Tomcat缓存?,tomcat,spring-boot,Tomcat,Spring Boot,我正在用JSP将5年前的SpringMVC应用程序移植到SpringBoot。所以,根据我的样本,我使用的是“战争”包装 嵌入式tomcat启动。但是,日志中充满了缓存警告,如下面的示例所示 2016-08-25 14:59:01.442 INFO 28884 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2016-08-

我正在用JSP将5年前的SpringMVC应用程序移植到SpringBoot。所以,根据我的样本,我使用的是“战争”包装

嵌入式tomcat启动。但是,日志中充满了缓存警告,如下面的示例所示

2016-08-25 14:59:01.442 INFO 28884 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2016-08-25 14:59:01.456 INFO 28884 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2016-08-25 14:59:01.458 INFO 28884 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.4 2016-08-25 14:59:01.531 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/displaytag-1.2.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache 2016-08-25 14:59:01.531 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/decrex-maven-0.1.10-SNAPSHOT.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache 2016-08-25 14:59:01.531 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/spring-boot-actuator-1.4.0.RELEASE.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache 2016-08-25 14:59:01.531 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/validation-api-1.1.0.Final.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache 2016-08-25 14:59:01.532 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/lucene-backward-codecs-5.3.1.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache 2016-08-25 14:59:01.532 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/lucene-queries-5.3.1.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache ..... 2016-08-25 14:59:05.121 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/lib/jstl-1.2.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache 2016-08-25 14:59:05.139 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache : Unable to add the resource at [/WEB-INF/classes/commons-logging.properties] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache 2016-08-25 14:59:05.139 INFO 28884 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2016-08-25 14:59:05.139 INFO 28884 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 7117 ms ..... 2016-08-25 15:02:03.960 INFO 28884 --- [ndardContext[]]] org.apache.catalina.webresources.Cache : The background cache eviction process was unable to free [10] percent of the cache for Context [] - consider increasing the maximum size of the cache. After eviction approximately [9,251] KB of data remained in the cache.
有关更改缓存大小的消息在日志中,但上面的代码对警告没有影响

您可以使用上下文自定义程序配置缓存大小,以使用自定义的
标准根配置上下文:

Java 7:

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory();
    tomcatFactory.addContextCustomizers(new TomcatContextCustomizer() {

        @Override
        public void customize(Context context) {
            StandardRoot standardRoot = new StandardRoot(context);
            standardRoot.setCacheMaxSize(40 * 1024);
        }

    });
    return tomcatFactory;
}
Java 8:

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory();
    tomcatFactory.addContextCustomizers((context) -> {
        StandardRoot standardRoot = new StandardRoot(context);
        standardRoot.setCacheMaxSize(40 * 1024);
    });
    return tomcatFactory;
}

我也有同样的问题已经有一段时间了,但是安迪·威尔金森的暗示让我走上了正确的道路。 对我来说,有效的方法是像Andy那样设置缓存,但也在上下文中显式地设置资源

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            final int cacheSize = 40 * 1024;
            StandardRoot standardRoot = new StandardRoot(context);
            standardRoot.setCacheMaxSize(cacheSize);
            context.setResources(standardRoot); // This is what made it work in my case.

            logger.info(String.format("New cache size (KB): %d", context.getResources().getCacheMaxSize()));
        }
    };
    return tomcatFactory;
}
希望这有帮助

参考那里的答案,我创建了src/main/webapp/META-INF/context.xml并添加了以下内容

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Resources cachingAllowed="true" cacheMaxSize="204800" />
</Context>


关于此错误有一些信息:据我所知,要增加缓存大小,您必须定义,谢谢您的想法。我可以访问的实例。然而,没有任何方法,可以帮助控制大笑汉克很多!我尝试了你的建议(1024*1024)两种方式:创建工厂和稍后在EmbeddedServletContainerCustomizer中自定义工厂(代码添加到我的问题中)。但是,这两种方法都没有对结果产生任何影响-警告仍然存在
standardRoot.setCachingAllowed(false)也不禁用缓存。似乎
StandardRoot
不会影响任何事情听起来好像没有使用您的自定义程序。你确定它在组件扫描覆盖的包中吗?是的。为了确保这一点,我在定制程序中添加了
log.info(“…”)
语句,我在logsWell中看到了这条语句,它对我有效。没有一个完整的例子表明它不起作用,我很难堪。太好了,谢谢!我看到created
standardRoot
没有被传递到任何地方,但我确信它是可以的,因为它在构造函数中得到了
context
。谢谢,可以确认它在spring boot 1.4.2中工作。
<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Resources cachingAllowed="true" cacheMaxSize="204800" />
</Context>