Eclipse 中的“mvn spring-boot:run”和“添加到 Tomcat 服务器”有啥区别?

Posted

技术标签:

【中文标题】Eclipse 中的“mvn spring-boot:run”和“添加到 Tomcat 服务器”有啥区别?【英文标题】:What's the difference between "mvn spring-boot:run" and "Add to Tomcat server" in Eclipse?Eclipse 中的“mvn spring-boot:run”和“添加到 Tomcat 服务器”有什么区别? 【发布时间】:2016-04-12 00:39:55 【问题描述】:

我从here 下载了一个 Spring-boot CXF Web 服务示例。我可以使用以下方法在 Eclipse 中成功运行它: 右键单击项目> Run as>Maven build...> 并设置目标:spring-boot:run

服务器启动,一切都登录到控制台。

接下来我尝试在 Eclipse 中的 Tomcat 服务器上部署它(请参阅 screenshot.png),但是当服务器启动时 Spring 没有启动。没有 Spring 信息记录到控制台中

根据下面日志中的行,它知道 Spring 应该启动。那么为什么它不能正常启动呢?

INFO: Spring WebApplicationInitializers detected on classpath: [org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration@4ed48c2]

整个日志:

Led 07, 2016 3:39:18 ODP. org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]Server/Service/Engine/Host/Context Setting property 'source' to 'org.eclipse.jst.jee.server:spring-boot-sample-ws-cxf' did not find a matching property.
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.28
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Oct 7 2015 18:25:21 UTC
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.28.0
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 10
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jdk1.8.0_60\jre
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_60-b27
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\Users\cerny\JavaProjects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Program Files\Tomcat 8.0
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\cerny\JavaProjects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Program Files\Tomcat 8.0
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\cerny\JavaProjects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Tomcat 8.0\endorsed
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1250
Led 07, 2016 3:39:18 ODP. org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.8.0_60\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_65/bin/server;C:/Program Files/Java/jre1.8.0_65/bin;C:/Program Files/Java/jre1.8.0_65/lib/amd64;C:\Program Files\apache-maven-3.3.3\bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\eclipse;;.
Led 07, 2016 3:39:18 ODP. org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Led 07, 2016 3:39:18 ODP. org.apache.tomcat.util.net.NioselectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Led 07, 2016 3:39:18 ODP. org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Led 07, 2016 3:39:18 ODP. org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Led 07, 2016 3:39:18 ODP. org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1036 ms
Led 07, 2016 3:39:18 ODP. org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Led 07, 2016 3:39:18 ODP. org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.28
Led 07, 2016 3:39:25 ODP. 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.
Led 07, 2016 3:39:25 ODP. org.apache.catalina.core.ApplicationContext log
INFO: Spring WebApplicationInitializers detected on classpath: [org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration@4ed48c2]
Led 07, 2016 3:39:25 ODP. org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Led 07, 2016 3:39:25 ODP. org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Led 07, 2016 3:39:25 ODP. org.apache.catalina.startup.Catalina start
INFO: Server startup in 7300 ms

【问题讨论】:

Spring boot 有一个嵌入式服务器。我记得那是码头。无需分离服务器即可运行 Spring Boot 应用程序。无论如何,您也可以将 tomcat 用作嵌入式服务器。 你的问题是什么,然后:“有什么区别”“为什么它不会启动?” 【参考方案1】:

我找到了它无法启动的原因。现在它对我有用。要在 Eclipse Tomcat 上运行,您需要将 extends SpringBootServletInitializer 添加到项目的主类 App.java

所以结果会是这样的

package com.foo.bar;

import some.imported.package
import another.imported.package

@SpringBootApplication
public class App extends SpringBootServletInitializer 

    public static void main(String[] args) 
        SpringApplication.run(App.class, args);
    


【讨论】:

【参考方案2】:

spring boot 项目生成的jar 里面会有tomcat 服务器。您可以使用以下命令启动服务器

java -jar XXX.jar

【讨论】:

以上是关于Eclipse 中的“mvn spring-boot:run”和“添加到 Tomcat 服务器”有啥区别?的主要内容,如果未能解决你的问题,请参考以下文章

怎样开发Eclipse中的插件

如何修改eclipse中的默认工作路径

如何使用eclipse中的debug工具,调试部署在weblogic中的应用?

清除Eclipse中的内置浏览器中的历史记录

保存到文件中的idea文件为啥是eclipse格式

Git在eclipse中的使用,克隆导入eclipse项目