我需要为弹簧启动项目单独安装 Tomcat 吗?



>我有一个弹簧启动项目,我有以下设置:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--<scope>provided</scope>-->
</dependency>

我问这个问题的原因是,有一次我可以在IntelliJ IDE和终端(mvn spring-boot:run(上运行我的web(war包(应用程序来启动应用程序,然后我可以使用localhost将http请求发送到restful服务。我没有单独安装Tomcat。

一段时间后,我仍然可以在 IntelliJ 中成功运行我的 Web 应用程序,但无法通过"mvn spring-boot:run"运行它。我想这是由于我的pom文件中的一些更改。错误消息似乎与雄猫有关:

[WARNING] 
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:506)
at java.lang.Thread.run (Thread.java:748)
Caused by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh (EmbeddedWebApplicationContext.java:137)
at org.springframework.context.support.AbstractApplicationContext.refresh (AbstractApplicationContext.java:536)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh (EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh (SpringApplication.java:761)
at org.springframework.boot.SpringApplication.refreshContext (SpringApplication.java:371)
at org.springframework.boot.SpringApplication.run (SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run (SpringApplication.java:1186)
at org.springframework.boot.SpringApplication.run (SpringApplication.java:1175)
at com.jd.jnlu.qe.boot.JnluQEWebStart.main (JnluQEWebStart.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:506)
at java.lang.Thread.run (Thread.java:748)
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat

如果我在我的机器上安装 Tomcat 以便能够通过"mvn spring-boot:run"运行它,它会有所帮助吗?另外,目前我没有安装Tomcat或Apache服务器,我应该可以在IntelliJ中成功使用Web应用程序吗?

您已经注释掉了为雄猫提供的范围。 在该状态下,它默认为编译范围,这意味着当您启动应用程序时,它将在类路径上可用(无论您使用哪种方法(。

当您取消注释它并将其置于提供的作用域时,这意味着它仅在编译时可用,并且您希望 JDK 或容器提供对类路径的依赖关系。如果将其部署到 tomcat 的独立实例,这是有意义的。

就像JB Nizet已经说过的那样,Spring-boot使用并启动了一个嵌入式tomcat容器。但为了做到这一点,它需要依赖!

正如其他人所建议的那样,您应该阅读 spring-boot 的文档以了解它是如何工作的。为了帮助您入门:这里有一个很好的操作方法,解释了使用 maven 运行 spring-boot 应用程序的基础知识 https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-first-application.html#getting-started-first-application-run 希望您能够从那里重现它。

请注意,弹簧-引导-启动器-网络取决于弹簧-引导-启动器-雄猫!

最新更新