嵌入式Tomcat在哪里提取? [英] Where the embedded Tomcat extracts?

查看:47
本文介绍了嵌入式Tomcat在哪里提取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嵌入在可执行 jar Tomcat 存档中的位置在哪里?

Where the embedded in executable jar Tomcat archive extracts?

在内存中还是在硬盘上的某个文件夹中?

In memory or in the some folder on hard drive?

与普通Tomcat相比需要多少资源?

How many resources it requires compared with ordinary Tomcat?

推荐答案

这可能不是一个完整的答案,但我可以解释的尽可能多.我也想知道,所以我做了一些挖掘.

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

使用spring boot,我们通常包括spring-boot-starter-tomcat(通过spring-boot-starter-web),其中包括tomcat-embed-核心.

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

如果您要创建一个普通的 main 方法,并将 tomcat-embed-core-xyzjar 添加到您的类路径中,您可以简单地执行此操作(来源:http://people.apache.org/~markt/presentations/2010-11-04-Embedding-Tomcat.pdf):

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

因此,当 spring-boot-maven-plugin 打包 uber-jar 时,它会在 jar 中包含 tomcat-embed-core.

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

自动配置 - 新的 Tomcat()

Spring Boot 通过自动配置启动它的特性,其中之一是 EmbeddedServletContainerAutoConfiguration:

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();
        }
    }

如果在classpath中找到tomcatServletTomcatEmbeddedServletContainerFactory就会被实例化,一切就开始了..

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

在 spring-boot (EmbeddedWebApplicationContext.refresh) 的某个地方,通过调用 TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer() 创建了一个 Tomcat 实例,它执行了一个 new Tomcat();

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

从现在开始

从这点来说,我可以用简单的main方法来做一个简单的new Tomcat().对我来说,这就是 Spring Boot 所做的,然后注册所有的 Servlet.剩下的就是老春好.显然,Spring Boot 提供了大量的 AutoConfiguration 和属性来配置几乎所有内容!

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 ?

这篇关于嵌入式Tomcat在哪里提取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆