Where the embedded Tomcat extracts?

淺唱寂寞╮ 提交于 2019-12-25 06:45:14

问题


Where the embedded in executable jar Tomcat archive extracts?

In memory or in the some folder on hard drive?

How many resources it requires compared with ordinary Tomcat?


回答1:


This probably won't be a complete answer, but just as much as I can explain. I too was wondering, so I did a little digging.

tomcat-embed-core

With spring boot, we usually include spring-boot-starter-tomcat (through spring-boot-starter-web), which includes tomcat-embed-core.

If you were to create a normal main method, and add tomcat-embed-core-x.y.z.jar to your classpath, you could simply do this (source: http://people.apache.org/~markt/presentations/2010-11-04-Embedding-Tomcat.pdf):

Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);

// Create a context
File docBase = new File(System.getProperty("java.io.tmpdir"));
Context ctxt = tomcat.addContext("",docBase.getAbsolutePath());

// Add a Servlet
Tomcat.addServlet(ctxt, "HelloWorld", new HelloWorldServlet());
ctxt.addServletMapping("/*", "HelloWorld");

// Start the instance
tomcat.start();
// Loop to serve requests
while(true) {
    Thread.sleep(5000);
}

So, when spring-boot-maven-plugin packages the uber-jar, it includes tomcat-embed-core within the jar.

Autoconfiguration - new Tomcat()

Spring Boot starts its feature through autoconfiguration, one of them is the EmbeddedServletContainerAutoConfiguration:

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnWebApplication
@Import(EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar.class)
public class EmbeddedServletContainerAutoConfiguration {
...
@Configuration
    @ConditionalOnClass({ Servlet.class, Tomcat.class })
    @ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
    public static class EmbeddedTomcat {

        @Bean
        public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
            return new TomcatEmbeddedServletContainerFactory();
        }
    }

If tomcat and Servlet are found on the classpath, TomcatEmbeddedServletContainerFactory will be instantiated, and it all starts..

Somewhere in spring-boot (EmbeddedWebApplicationContext.refresh) a Tomcat instance is created by calling TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(), which does a new Tomcat();

From this point on

From this point, I can relate with the simple main method, doing a simple new Tomcat(). For me, that's what Spring Boot does and then registers all the Servlets. The rest is good old spring. Obviously, Spring Boot offers tons of AutoConfiguration and properties to configure just about everything!

Does that help a little, of perhaps you knew this and were hoping for more ?



来源:https://stackoverflow.com/questions/39320705/where-the-embedded-tomcat-extracts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!