如何配置嵌入式tomcat来运行springmvc应用程序?

9w11ddsr  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(338)

遵循此处给出的教程:https://www.codejava.net/servers/tomcat/how-to-embed-tomcat-server-into-java-web-applications,我试图在一个单独的嵌入式tomcat配置项目中部署一个springwar文件。以下是我的java配置和build.gradle配置:
嵌入式tomcat的java配置:

public static void main(String[] args) throws LifecycleException {
        Tomcat tomcat = new Tomcat();
        tomcat.setBaseDir("temp");
        tomcat.setPort(9000);

        String contextPath = "";
        String docBase = new File("spring.war").getAbsolutePath();

        tomcat.getHost().setAppBase(".");

        tomcat.addWebapp(contextPath, docBase);

        tomcat.start();
        tomcat.getServer().await();
}

渐变生成文件:

plugins {
    id 'java'
    id 'application'
}

group 'org.example'
version '1.0-SNAPSHOT'

mainClassName = 'TomcatDriver'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '9.0.37-dev'
    compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.37'
    compile group: 'org.apache.tomcat', name: 'tomcat-juli', version: '9.0.37'
    compile group: 'org.apache.tomcat', name: 'tomcat-annotations-api', version: '9.0.37'
}

当我从intellij idea(community)运行时,应用程序似乎确实进行了一些初始化,但访问浏览器上的指定地址会出现错误:
无法访问此网站localhost拒绝连接。尝试:检查连接检查代理和防火墙错误连接被拒绝
以下是我使用intellij idea(社区)运行应用程序时得到的日志:

17:13:32: Executing task 'TomcatDriver.main()'...

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes

> Task :TomcatDriver.main()
Jan 20, 2021 5:13:34 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Jan 20, 2021 5:13:34 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.37]
Jan 20, 2021 5:13:34 PM org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
INFO: No global web.xml found
Jan 20, 2021 5:13:37 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Jan 20, 2021 5:13:37 PM org.apache.catalina.core.ApplicationContext log
INFO: 1 Spring WebApplicationInitializers detected on classpath
Jan 20, 2021 5:13:37 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Jan 20, 2021 5:13:38 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
Jan 20, 2021 5:13:39 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.7.Final}
Jan 20, 2021 5:13:39 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 20, 2021 5:13:39 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
Jan 20, 2021 5:13:40 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Jan 20, 2021 5:13:42 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 6.1.7.Final

有人能指出我的配置有什么问题吗?同样的配置也适用于带有版本8的嵌入式tomcat的非spring项目https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core/8.0.48.
嵌入式tomcat8jar似乎不能运行spring项目。我在这里读到,springbootwar部署在tomcat中启动失败,springwar文件可能需要tomcat9进行部署。这就是为什么我要用tomcat 9jar。
更新
我认为这篇文章解决了我遇到的问题:代码可以与嵌入式ApacheTomcat8一起工作,但不能与9一起工作。有什么变化?
提前谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题