【问题标题】:Spring boot and tomcatSpring Boot 和 tomcat
【发布时间】:2014-01-11 03:19:28
【问题描述】:

我想看一个可以让我做的 Spring Boot 示例:

mvn tomcat:run

我尝试了现有的示例和以下教程,但没有成功。

【问题讨论】:

  • 您是否只需将 tomcat 集成 jar 放在类路径中并使用 @EnableAutoConfiguration 注释类调用 SpringApplication.run(..)
  • 有一个 spring-boot maven 插件,可以让你在命令行“mvn spring-boot:run”上从源代码运行。你试过了吗?

标签: java spring tomcat spring-boot


【解决方案1】:

我认为 vanilla tomcat 插件不支持 servlet 3.0 (tomcat 7)。不过,您可能可以将 tomcat7 插件与战争样本一起使用(例如 servletjspstatic)。

这是一个示例插件配置:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
</plugin>

【讨论】:

  • 你是对的。 tomcat (6) 不会处理它。但添加插件tomcat7:run 后也不起作用
  • 为我工作。我将上述插件配置添加到 JSP 示例中。
  • @piotrek 我还添加了&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt; 插件以及此答案中提到的tomcat7-maven-plugin。此处提到的 JSP 示例中的 pom [github.com/spring-projects/spring-boot/blob/master/… 不包含我发现使 mvn tomcat7:run 命令正常工作所需的 tomcat7-maven-plugin
【解决方案2】:

官方文档对此不是很清楚,但我发现了这个简洁易懂的如何将spring boot和tomcat maven插件放在一起。

https://gerrydevstory.com/2014/08/22/spring-boot-and-the-embedded-tomcat-container/

网站信息的要点:

  • 删除 pom.xml 上的 spring-boot-maven-plugin &lt;plugin&gt; 配置

  • 设置tomcat7-maven-plugin &lt;plugin&gt;

.

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.0</version>
</plugin>
  • 代替 SpringApplication.run(Application.class, args),使用 SpringBootServletInitializer 引导 Spring Boot,例如:

.

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
  }

}

并更改 POM.xml 中的范围

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

【讨论】:

  • 感谢 EpicPandaForce,他做了很长的解释,我的回答只是指出了如何做的链接
猜你喜欢
  • 2019-08-23
  • 2018-12-06
  • 1970-01-01
  • 2017-01-22
  • 1970-01-01
  • 2021-05-02
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多