How does Spring Boot control Tomcat cache?

前端 未结 3 1087
余生分开走
余生分开走 2021-01-02 02:39

I\'m porting 5 years old Spring MVC application with JSPs to Spring Boot. Therefore, according to sample in http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference

相关标签:
3条回答
  • 2021-01-02 03:17

    You can configure the cache size using a context customizer to configure the context with a customized StandardRoot:

    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;
    }
    
    0 讨论(0)
  • I've been having the same problem for a while, but Andy Wilkinson's hint set me on the right track. What worked for me was to set the cache as Andy did, but then also explicitly set the resources in context.

    @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;
    }
    

    Hope this helps!

    0 讨论(0)
  • 2021-01-02 03:39

    Referring to there answer here, I created src/main/webapp/META-INF/context.xml and added the following.

    <?xml version="1.0" encoding="UTF-8"?>
    <Context>
      <Resources cachingAllowed="true" cacheMaxSize="204800" />
    </Context>
    
    0 讨论(0)
提交回复
热议问题